Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
HTML
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:
HTML
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:
HTML
[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
C#
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)
        {
            // connection to database
            // attach parameters
            // delete the record
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "DELETE from CART where ProdID=" + ProductID + "AND CartID=" + catID;
            //conn.ConnectionString = _strconn;
            cmd.Connection = conn;
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
Posted
Updated 29-Sep-12 13:20pm
v3
Comments
Zoltán Zörgő 29-Sep-12 16:07pm    
Nice. And what exactly is the statement you try to execute, how is cmd constructed?
Gracia Gabriella 29-Sep-12 17:01pm    
I am trying to delete data on my database using the cartID which is a 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)
{
// connection to database
// attach parameters
// delete the record
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "DELETE from CART where ProdID=" + ProductID + "AND CartID=" + catID;
//conn.ConnectionString = _strconn;
cmd.Connection = conn;
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
supernorb 29-Sep-12 16:31pm    
The error is so clear enough. You should try checking your query.
Gracia Gabriella 29-Sep-12 17:00pm    
Supernob, I am asking for help because i dont see it clearly,. might be something stupid, but i need help so that i will not make stupid mistake like this in the future.

1 solution

GUID-s are sort of strings, thus you need to use quotes in the statement, as you would do it with strings.
SQL
cmd.CommandText = "DELETE from CART where ProdID='" + ProductID + "' AND CartID=" + catID;

But you better use parameters, like described here: http://weblogs.asp.net/thiagosantos/archive/2009/01/13/adding-a-guid-parameter-into-sqlcommand.aspx[^]
 
Share this answer
 
Comments
supernorb 29-Sep-12 17:31pm    
This was what I thought about. Now the asker has a good solution!
Gracia Gabriella 29-Sep-12 19:06pm    
Thank you for the solution. Unfortunately I am still getting the same error after using this solution, as well as the one in the link. I even added the "'" after the catID variable. When using the link above, it says object must use IConvertible. What next?
Zoltán Zörgő 30-Sep-12 6:12am    
Because guid is not string in c# - thus you have to convert it to string before you can add it to your statement. And it has no parameterless ToString method. See: http://msdn.microsoft.com/en-us/library/97af8hh4.aspx
Gracia Gabriella 8-Mar-13 1:35am    
Wow!!It has been a while since I've been here. Thank you guys for your help though

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