Update function in web form using SQL expresshttp://forums.asp.net/t/1796613.aspx/1?Update+function+in+web+form+using+SQL+expressThu, 26 Apr 2012 14:22:25 -040017966134949731http://forums.asp.net/p/1796613/4949731.aspx/1?Update+function+in+web+form+using+SQL+expressUpdate function in web form using SQL express <p>Hello I am making a web form in visual 2010 ultimate. <br> <br> I am using the follwoing system<br> Webform: has form where the buttons have an onclick function to the behind .cs sheet.The cs. sheet then refrences to my web service where the SQL database (express is connected) is refrenced.&nbsp; <br> <br> My webform basically asks for the user to enter a unique ID (primary key, error comes up if ID is already taken) a firstname and surname. <br> <br> I then have a&nbsp; add, update and delete button. <br> <br> I already added the add function and it succesfully adds a user to the database if the ID is unique (otherwise error message to user). So that is all good!!</p> <p>However, now I am trying to add my update function and am having trouble making this work. Everytime I try to make a user update I get an error (IMO the error indicates wrong command in SQL). </p> <p>This is my web method for the update.</p> <pre class="prettyprint">[WebMethod] public bool UpdatePerson(int ID, string FIRSTNAME, string SURNAME) { // Connect to the Database SqlConnection connection = new SqlConnection( @&quot;Data Source=.\SQLEXPRESS; AttachDbFilename='|DataDirectory|\Database.mdf'; Integrated Security=True; User Instance=True&quot;); // Open the connection connection.Open(); // Create a SQL command object. SqlCommand command = new SqlCommand( String.Format( &quot;UPDATE PersonalDetails Values('{1}','{2}')&quot; &#43; &quot;SELECT COUNT(ID) FROM PersonalDetails WHERE ID = '{0}'&quot;, ID, FIRSTNAME, SURNAME), connection); // Execute the SQL command and store the returned integer. int response = (int)command.ExecuteScalar(); // Close the connection connection.Close(); // Return the result. return (response &gt; 0); }</pre> <p>I believe I am making a mistake here;<br /><br /></p> <pre class="prettyprint"> // Create a SQL command object. SqlCommand command = new SqlCommand( String.Format( "UPDATE PersonalDetails Values('{1}','{2}')" + "SELECT COUNT(ID) FROM PersonalDetails WHERE ID = '{0}'", ID, FIRSTNAME, SURNAME), connection);</pre> <p>I know my aspx page is fine, I am confident my .cs sheet is right also (but will post if requested), but I think this web method is the problem. Not sure how to do UPDATE function and just tried a different t hings with my prior coding knowledge (which is somewhat limited, as I am in involved in networking). <br> <br> Any help would be greatly appreciated, I do enjoy programming but struggle with it and this particular problem is giving me a headache :) <br> <br> <br> </p> <p></p> <p></p> 2012-04-25T04:15:35-04:004949740http://forums.asp.net/p/1796613/4949740.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>hi,</p> <p>update query syntax problem</p> <p>it should be</p> <p>&nbsp;</p> <p><strong>&nbsp;</strong>&nbsp;</p> <p>&nbsp;<span class="typ">SqlCommand</span><span class="pln"> command </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">SqlCommand</span><span class="pun">(</span><span class="pln"> &quot;update personaldetails set firstname = '&quot; &#43; FIRSTNAME &#43; &quot;',surname = '&quot; &#43; SURNAME &#43; &quot;' where id = &quot; &#43; ID.ToString(), con);</span></p> 2012-04-25T04:22:20-04:004949743http://forums.asp.net/p/1796613/4949743.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>You should add a ; between your two SQL statementslik this:</p> <p>;SELECT COUNT(ID) FROM ...</p> <p>You should use&nbsp;parameters for your query instead of string concatenation.</p> 2012-04-25T04:24:12-04:004949763http://forums.asp.net/p/1796613/4949763.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>@tushars</p> <p>i tried this but I got an error still. I think a different error tho. But I beleieve it still has something to do with the SQL command</p> <p></p> <p>@limno what is the difference? Sorry, again not very familar with programming. <br> The system Im using at the moment is the way I got taught.</p> <p>anywho I added the ; but still got the error.</p> <p></p> <p>ps. thanks for the pompt replies btw</p> 2012-04-25T04:48:00-04:004949782http://forums.asp.net/p/1796613/4949782.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>It seems you mixed insert, update and select sytax together. Ytu this for your update:</p> <pre class="prettyprint">// Open the connection connection.Open(); // Create a SQL command object. SqlCommand command = new SqlCommand(&quot;UPDATE PersonalDetails Set FIRSTNAME =@FIRSTNAME, SURNAME=@SURNAME WHERE ID=@ID&quot;); //Add parameter value to SQL command command.Parameters.AddWithValue(&quot;@FIRSTNAME&quot;, FIRSTNAME); command.Parameters.AddWithValue(&quot;@SURNAME&quot;, SURNAME); command.Parameters.AddWithValue(&quot;@ID&quot;, ID); command.ExecuteNonQuery(); // Close the connection connection.Close();</pre> <p></p> 2012-04-25T05:01:23-04:004949795http://forums.asp.net/p/1796613/4949795.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>How do I give a response with this? <br> My cs. code uses a bool.</p> <p></p> <p>this is my cs function for the update (which teh button uses on click)</p> <p></p> <pre class="prettyprint">protected void cmdUpdate_Click(object sender, EventArgs e) { // Create a reference to the Web service DbWebService.WebService1 proxy = new DbWebService.WebService1(); // Create a person details object to send to the Web service. string SURNAME; string FIRSTNAME; string ID; SURNAME = txtSurname.Text; FIRSTNAME = txtFirstname.Text; ID = txtID.Text; // Attempt to store in the Web service bool rsp = proxy.UpdatePerson(int.Parse(ID), FIRSTNAME, SURNAME); // Inform the user if (rsp) { lblOutcome.Text = &quot;Successfully updated new user.&quot;; } else { lblOutcome.Text = &quot;Failed to update user!&quot;; } }</pre> <p><br> <br> </p> 2012-04-25T05:16:51-04:004949886http://forums.asp.net/p/1796613/4949886.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>so I used this (with the above code in .cs ) code in the web service, but still getting the same error :(<br> really dunno what I am doing wrong.</p> <p></p> <pre class="prettyprint">[WebMethod] public bool UpdatePerson(int ID, string FIRSTNAME, string SURNAME) { // Connect to the Database SqlConnection connection = new SqlConnection( @&quot;Data Source=.\SQLEXPRESS; AttachDbFilename='|DataDirectory|\Database.mdf'; Integrated Security=True; User Instance=True&quot;); // Open the connection connection.Open(); // Create a SQL command object. SqlCommand command = new SqlCommand(&quot;UPDATE PersonalDetails Set FIRSTNAME =@FIRSTNAME, SURNAME=@SURNAME WHERE ID=@ID&quot;); //Add parameter value to SQL command command.Parameters.AddWithValue(&quot;@FIRSTNAME&quot;, FIRSTNAME); command.Parameters.AddWithValue(&quot;@SURNAME&quot;, SURNAME); command.Parameters.AddWithValue(&quot;@ID&quot;, ID); command.ExecuteNonQuery(); // Execute the SQL command and store the returned integer. int response = (int)command.ExecuteScalar(); // Close the connection connection.Close(); // Return the result. return (response &gt; 0); }</pre> <p><br> <br> </p> <p></p> <p></p> 2012-04-25T06:13:14-04:004949979http://forums.asp.net/p/1796613/4949979.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>Yes. you have commited mistake in updat statement. Your statement should have</p> <p>String.Format(<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;UPDATE PersonalDetails set FirstName = '{1}', SurName = '{2}' WHERE ID = '{0}'&quot;,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ID, FIRSTNAME, SURNAME);</p> <p>I don't know the fields name of table. I just assumed that it is FirstName and SurName. Use as your field name. You can also use. Following code snippet and it is tested.</p> <p>&nbsp; &quot;UPDATE&nbsp;PersonalDetails SET FirstName = '&quot; &#43;&nbsp;FIRSTNAME &#43;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;',SurName = '&quot; &#43;&nbsp;SURNAME &#43; &quot;'<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#43; &quot;' WHERE ID= &quot; &#43; ID;</p> <p>If it helped you, please mark it as answer</p> 2012-04-25T06:47:53-04:004950114http://forums.asp.net/p/1796613/4950114.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>Hi,</p> <p>I tried this, and it should work but still I get the error, I have no idea why, so frustrating!!</p> <p>With the 2nd snippet, I get an &quot;Newline in constant&quot; error in the description list. </p> <p></p> <p></p> 2012-04-25T07:40:00-04:004950198http://forums.asp.net/p/1796613/4950198.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>Could you please tell me, what is the error? For more information about updating table please read the following topic <a href="http://mahedee.blogspot.com/2012/04/simple-demonstration-with-aspnet.html"> http://mahedee.blogspot.com/2012/04/simple-demonstration-with-aspnet.html</a><span style="font-size:small"> take a closer look on the<b> EmployeeInfoDAL</b></span><span style="font-family:Consolas; font-size:small">. Hopefully you will get an idea how to update a table.</span></p> <p><span style="font-family:Consolas; font-size:small"></span></p> 2012-04-25T08:17:51-04:004950239http://forums.asp.net/p/1796613/4950239.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>hi,</p> <p>1) check the connection string, is it proper</p> <p>2) check the query by executing in sql server</p> <p>3) check the field datattypes are they matching</p> <p>&nbsp;</p> 2012-04-25T08:36:43-04:004950284http://forums.asp.net/p/1796613/4950284.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>No worries, here is the error in full detail:</p> <p>----------------------------------------------------------------------</p> <p>My webform, the interface the user sees in browser.</p> <p><img src="http://i1131.photobucket.com/albums/m548/tiiimmmy151/mywebform.jpg" height="326" width="580"></p> <p>Basically here the ID 1 is already entered in the database (using the add button). So when the user selects update with new info in first/surname the database should be updated, However when I click update I get the following error in Visual:</p> <p><img src="http://i1131.photobucket.com/albums/m548/tiiimmmy151/theerror.jpg" height="579" width="1022"></p> <p>and when I stop debugging, the web browser gives me this error:</p> <h1>Server Error in '/' Application.</h1> <hr color="silver" size="1" width="100%"> <h2><i>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.ExecuteScalar()<br> &nbsp;&nbsp;at WebServices.WebService1.UpdatePerson(Int32 ID, String FIRSTNAME, String SURNAME) in C:\Users\Timothy\Desktop\sit322_ass2\assignment2\WebServices\WebService.asmx.cs:line 118<br> &nbsp;&nbsp;--- End of inner exception stack trace ---</i></h2> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description: </b>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. <br> <br> <b>Exception Details: </b>System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)<br> &nbsp;&nbsp;at System.Data.SqlClient.SqlCommand.ExecuteScalar()<br> &nbsp;&nbsp;at WebServices.WebService1.UpdatePerson(Int32 ID, String FIRSTNAME, String SURNAME) in C:\Users\Timothy\Desktop\database\WebServices\WebService.asmx.cs:line 118<br> &nbsp;&nbsp;--- End of inner exception stack trace ---<br> <br> <b>Source Error:</b> <br> <br> </span></span></p> <table bgcolor="#ffffcc" width="100%"> <tbody> <tr> <td> <pre>Line 153: [System.Web.Services.Protocols.SoapDocumentMethodAttribute(&quot;http://tempuri.org/UpdatePerson&quot;, RequestNamespace=&quot;http://tempuri.org/&quot;, ResponseNamespace=&quot;http://tempuri.org/&quot;, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] Line 154: public bool UpdatePerson(int ID, string FIRSTNAME, string SURNAME) { <span color="red" style="color:red">Line 155: object[] results = this.Invoke(&quot;UpdatePerson&quot;, new object[] { </span>Line 156: ID, Line 157: FIRSTNAME,</pre> </td> </tr> </tbody> </table> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><br> <b>Source File: </b>C:\Users\Timothy\Desktop\database\PersonalDetails\Web References\DbWebService\Reference.cs<b> &nbsp;&nbsp; Line: </b>155 </span></p> <p>---</p> <p>I'm not sure why this is happening!?Really confused...</p> <p>Here are the two relevant codes again:</p> <p>personal details webform cs. code, function for update</p> <pre class="prettyprint">protected void cmdUpdate_Click(object sender, EventArgs e) { // Create a reference to the Web service DbWebService.WebService1 proxy = new DbWebService.WebService1(); // Create a person details object to send to the Web service. string SURNAME; string FIRSTNAME; string ID; SURNAME = txtSurname.Text; FIRSTNAME = txtFirstname.Text; ID = txtID.Text; // Attempt to store in the Web service bool rsp = proxy.UpdatePerson(int.Parse(ID), FIRSTNAME, SURNAME); // Inform the user if (rsp) { lblOutcome.Text = &quot;Successfully updated new user.&quot;; } else { lblOutcome.Text = &quot;Failed to update user!&quot;; } }</pre> <p>webservice cs. update SQL function</p> <pre class="prettyprint"> [WebMethod] public bool UpdatePerson(int ID, string FIRSTNAME, string SURNAME) { // Connect to the Database SqlConnection connection = new SqlConnection( @"Data Source=.\SQLEXPRESS; AttachDbFilename='|DataDirectory|\Database.mdf'; Integrated Security=True; User Instance=True"); // Open the connection connection.Open(); // Create a SQL command object. SqlCommand command = new SqlCommand( String.Format("UPDATE PersonalDetails set FirstName = '{1}', SurName = '{2}' WHERE ID = '{0}'", ID, FIRSTNAME, SURNAME)); // Execute the SQL command and store the returned integer. int response = (int)command.ExecuteScalar(); // Close the connection connection.Close(); // Return the result. return (response &gt; 0); }</pre> <p><br> <br> <br> </p> 2012-04-25T08:53:39-04:004950292http://forums.asp.net/p/1796613/4950292.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p></p> <blockquote><span class="icon-blockquote"></span> <h4>tusharrs</h4> <p></p> <p>1) check the connection string, is it proper</p> <p>2) check the query by executing in sql server</p> <p>3) check the field datattypes are they matching</p> <p></p> </blockquote> <p></p> <p>sorry to sound a little amature-ish</p> <p>But how do I do this?</p> <p>I think the data types are matching.</p> <p>when I was adding my add function I also had the same error, but that was because I had a typo in the SQL command, I accidently misspelled personaldetails (the database refrence). Thus why I think this is an SQL command error.</p> <p>The add function currently works perfect.</p> <p></p> <p>&nbsp;</p> <p></p> 2012-04-25T08:56:50-04:004950420http://forums.asp.net/p/1796613/4950420.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>hi,</p> <p>set connection to sqlcommand object</p> <p>&nbsp;i.e.</p> <p><strong>command.SqlConnection = connection;</strong></p> <p>&nbsp;</p> <p><span class="pun"><strong>or</strong> </span></p> <p><span class="pun"><span class="typ">SqlCommand</span><span class="pln"> command </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">SqlCommand</span><span class="pun">(</span><span class="pln"> </span><span class="typ">String</span><span class="pun">.</span><span class="typ">Format</span><span class="pun">(</span><span class="str">&quot;UPDATE PersonalDetails set FirstName = '{1}', SurName = '{2}' WHERE ID = '{0}'&quot;</span><span class="pun">,</span><span class="pln"> ID</span><span class="pun">,</span><span class="pln"> FIRSTNAME</span><span class="pun">,</span><span class="pln"> SURNAME</span><span class="pun">) <strong>, connection</strong> );</span><span class="pln"> </span></span></p> <p></p> 2012-04-25T09:56:29-04:004951687http://forums.asp.net/p/1796613/4951687.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>Thanks again for the reply.<br> <br> I believe I tried this at one stage and still got error, but shall try again. Just at work at the moment so will try it when I get home, and mark as answer if it solves my problem. <br> <br> Any chance (using the provided code) that I made an error somewhere else in the code?</p> <p>regards, tim. </p> 2012-04-25T23:39:24-04:004951870http://forums.asp.net/p/1796613/4951870.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>You didn't initialize connection in sql command. Use the following before execute the query.</p> <p><strong>command.Connection = <span class="pln">connection </span><span class="pun"></span>;</strong></p> 2012-04-26T04:22:06-04:004951891http://forums.asp.net/p/1796613/4951891.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express You can also use cmd.ExecuteNonQuery(); for insert also. 2012-04-26T04:35:29-04:004952267http://forums.asp.net/p/1796613/4952267.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>yep fixed that and still get the error, this is inreasingly frustrating. <br> <br> I fixed it to this<br> <br> </p> <pre class="prettyprint">// Create a SQL command object. SqlCommand command = new SqlCommand( String.Format(&quot;UPDATE PersonalDetails set FIRSTNAME = '{1}', SURNAME = '{2}' WHERE ID = '{0}'&quot;, FIRSTNAME, SURNAME, ID), connection);</pre> <p>still get the same error.</p> <p><blockquote><span class="icon-blockquote"></span><h4>mahedee</h4>You can also use cmd.ExecuteNonQuery(); for insert also. </blockquote></p> <p>sorry but what do you mean by this, really confused? <br /><br />Do I remove any of these lines: ? <br /><br /></p> <pre class="prettyprint"> // Execute the SQL command and store the returned integer. int response = (int)command.ExecuteScalar(); // Close the connection PS: I KNOW I DONT REMOVE THIS LINE connection.Close(); // Return the result. return (response &gt; 0);</pre> <p><br> <br> </p> 2012-04-26T07:52:29-04:004952452http://forums.asp.net/p/1796613/4952452.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>I often use this way to connect, insert, update i,e acess database.</p> <p><strong>Step - 1: Simply create a class DBConnector to&nbsp; connect to the database.</strong></p> <pre class="prettyprint">public class DBConnector { private string connectionString = null; private SqlConnection sqlConn = null; private SqlCommand cmd = null; public DBConnector() { connectionString = ConfigurationManager.ConnectionStrings[&quot;application&quot;].ToString(); //User your connection string here. } public SqlCommand GetCommand() { cmd = new SqlCommand(); cmd.Connection = sqlConn; return cmd; } public SqlConnection GetConn() { sqlConn = new SqlConnection(connectionString); return sqlConn; } }</pre> <p></p> <p><strong>Step 2: Insert, Update and delete database using DBConnector class</strong></p> <p></p> <pre class="prettyprint">public class EmployeeInfoDAL { private SqlConnection sqlConn; private SqlCommand cmd; private readonly DBConnector objDBConnector; public EmployeeInfoDAL() { objDBConnector = new DBConnector(); sqlConn = objDBConnector.GetConn(); cmd = objDBConnector.GetCommand(); } //Insert data public string InsertEmployeeUserInfo(EmployeeUserInfo objEmployeeUserInfo) { int noOfRowEffected = 0; try { sqlConn.Open(); cmd.CommandType = CommandType.StoredProcedure; //If you use query instead of storprocedure. User CommandType.Text cmd.CommandText = "[fsp_Insert_employee_userInfo]"; //for query use query instead of store procedure cmd.Parameters.AddWithValue("@user_id", objEmployeeUserInfo.UserId); // dont use it if you don't need parameter cmd.Parameters.AddWithValue("@emp_id", objEmployeeUserInfo.EmployeeId);// dont use it if you don't need parameter noOfRowEffected = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } catch (Exception exp) { throw (exp); } finally { sqlConn.Close(); } if (noOfRowEffected &gt; 0) return "Employee User information saved successfully!"; else return "Employee User information didn't save"; } }</pre> <p></p> <p>This is tested and I am use its in my project. Please try this way. Hopefully it will work. Becarefule about your connection string.<br> <br> </p> <p></p> <p></p> 2012-04-26T09:33:38-04:004953093http://forums.asp.net/p/1796613/4953093.aspx/1?Re+Update+function+in+web+form+using+SQL+expressRe: Update function in web form using SQL express <p>Hi, <br> <br> Sorry but one of my requirements is to not use a class I believe, hoping to make the gormat im using work rather then a full workover.</p> <p>I think I have reduced the problem a little, I changed the web form to do this.</p> <pre class="prettyprint">[WebMethod] public bool UpdatePerson(int ID, string FIRSTNAME, string SURNAME) { // In case of failure failure first bool rtn = false; // Connect to the Database SqlConnection connection = new SqlConnection( @&quot;Data Source=.\SQLEXPRESS; AttachDbFilename='|DataDirectory|\Database.mdf'; Integrated Security=True; User Instance=True&quot;); // Open the connection connection.Open(); // Create a SQL command object. SqlCommand command = new SqlCommand( String.Format(&quot;UPDATE PersonalDetails set FIRSTNAME = '{1}', SURNAME = '{2}' WHERE ID = '{0}'&quot;, FIRSTNAME, SURNAME, ID), connection); // Execute the command. command.ExecuteNonQuery(); // Close the connection. connection.Close(); rtn = true; // Return the outcome return (rtn); }</pre> <p>However the error I am getting now says the following (in visual when I attempt to update on click)</p> <p>Conversion failed when converting the varchar value 'xx' to data type int.<br> <br> ANy idea the issue here? The thing is I have ID as a string in the webform cs. (as you can see above) but I do this to read the user input from text field. And I then change it to an int with the int.Parse(ID) statement. Then in the web service cs. sheet the ID is an int, not sure the issue here, anyone can notice the error. I feel like the code I posted is really close to working. <br> <br> </p> 2012-04-26T14:13:45-04:00