Click here to Skip to main content
15,889,728 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hey guys, im trying different ways of deleting from a gridview

So i have used the autogenerate functions on the data source and the gridview,
now this is the error im getting:

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'int'.

an this is the stack trace:

VB
[SqlException (0x80131904): Incorrect syntax near 'int'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2073550
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5064508
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +215
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +178
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +394
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary keys, IDictionary oldValues) +576
   System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +92
   System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +946
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1161
   System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +210
   System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563





OK. so where it says incorrect syntax near int i cannot see an error with the delete command this is the string i am using:
DeleteCommand="DELETE FROM Customer WHERE ([Customer ID] = @ID)">


i keep getting the error and its very frustrating, i have even tried removing the [] and the space near = and still no joy.


the other problem i have is as this was not working i thought i would hardcode some button and text box to do the same function but it is not deleting either.

this is the code i have used for delete button:

C#
protected void deleteButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TravelShopConnectionString1"].ConnectionString);
            SqlCommand deleteUser = new SqlCommand("DELETE FROM Customer WHERE [Customer ID] = @ID", conn);
            deleteUser.CommandType = CommandType.Text;
            try
            {
                conn.Open();
                deleteUser.ExecuteNonQuery();
                Response.Redirect("AdminRegistered.aspx");
            }
            catch (Exception)
            {
                Response.Write("ERROR delete failed");
            }
        }


any help would be greatly appreciated

Regards Matt.
Posted

huge problem

where you are passing value of @ID parameter in your code.

I think once you pass the value of @ID parameter issue will resolve easily...


Happy coding...
 
Share this answer
 
v2
Comments
BBCokeley 10-Apr-12 12:04pm    
yup, that was it, string was missing "'" at the end
plus i had no delete parameter.
try it, this may help You...

SqlConnection objcon = new SqlConnection(StrCon);
objcon.Open();
string StrDelete = "Delete from Company Where CompanyID='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand(StrDelete, objcon);
cmd.ExecuteNonQuery();
messageBox("Deleted Sucessfully");
objcon.Close();
objcon.Dispose();
 
Share this answer
 
Comments
Pranay Rana 10-Apr-12 11:57am    
this will cause sql injection attack so please pass the parameter in using SqlParameter
BBCokeley 10-Apr-12 12:04pm    
LEGEND

had the string all wrong, thank you very much friend

I hate string lol.

still miffed with the auto generated delete tho, but that doesnt matter now.

Thanks again
Regards Matt.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900