Hey all,
so i am working on database queries. Add and delete are working fine. I am having trouble with Edit Query. This is what i am getting:
Incorrect syntax near 'Name'.
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 'Name'.

Source Error:

Line 63:             con.Open();
Line 64:             SqlDataReader dr;
Line 65:             dr = cmd.ExecuteReader();
Line 66:             con.Close();
Line 67:             show();

Stack Trace:

[SqlException (0x80131904): Incorrect syntax near 'Name'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1789294
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340642
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +377
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +137
   System.Data.SqlClient.SqlCommand.ExecuteReader() +99
   SMC.Tests.Button2_Click(Object sender, EventArgs e) in E:\final project\SMC with blood donor\SMC\SMC\Tests.aspx.cs:65
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628026
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

This is my Edit code:

string str = "update Tests set [Patient Name] = @PName, Age = @Age, [Contact Number]=@ContactNum, Gender=@Gender, Test Name=@TestName, [Requested Date]=@ReqDate ,[Report Date]=@RepDate, Consultant=@Consultant where ID = '" + TextBox7.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.Parameters.AddWithValue("@PName", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Age", TextBox2.Text);
            cmd.Parameters.AddWithValue("@ContactNum", TextBox3.Text);
            cmd.Parameters.AddWithValue("@Gender", DropDownList2.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@TestName", DropDownList1.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@ReqDate", TextBox4.Text.ToString());
            cmd.Parameters.AddWithValue("@RepDate", TextBox5.Text.ToString());
            cmd.Parameters.AddWithValue("@Consultant", TextBox6.Text);
            con.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            con.Close();
            show();
            Response.Write("Record Updated");

Show() is a function i am using, it is not causing trouble. Just a heads up.
Any kind of help is much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.