Hi there,
I am getting this error when i try to delete some data from my database. I am using GUID as my cartID, and when i try to delete using that cartID, i get the below error.
Please help me. Thank you
Server Error in '/' Application.
Incorrect syntax near 'bd7c7f'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'bd7c7f'.
Source Error:
Line 130: if (conn.State != ConnectionState.Open)
Line 131: conn.Open();
Line 132: cmd.ExecuteNonQuery();
Line 133: conn.Close();
Line 134: }
Source File: C:\Users\Mathula Magubane\Documents\Visual Studio 2010\Presentation\Checkout2.aspx.cs Line: 132
Stack Trace:
[SqlException (0x80131904): Incorrect syntax near 'bd7c7f'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2073486
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5064444
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.RunExecuteNonQueryTds(String methodName, Boolean async) +228
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +326
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
Presentation.Checkout2.DeleteRecordByIDs(Int32 ProductID, Guid catID) in C:\Users\Mathula Magubane\Documents\Visual Studio 2010\Presentation\Checkout2.aspx.cs:132
Presentation.Checkout2.GridView2_RowDeleting(Object sender, GridViewDeleteEventArgs e) in C:\Users\Mathula Magubane\Documents\Visual Studio 2010\Presentation\Checkout2.aspx.cs:118
System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +139
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +637
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1161
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
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
I am trying to delete from my database using the GUID and the productID
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int productID = (int)GridView2.DataKeys[e.RowIndex].Values["ProdID"];
Guid cartID = (Guid)GridView2.DataKeys[e.RowIndex].Values["CartID"];
DeleteRecordByIDs(productID, cartID);
}
private void DeleteRecordByIDs(int ProductID, Guid catID)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "DELETE from CART where ProdID=" + ProductID + "AND CartID=" + catID;
cmd.Connection = conn;
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}