ExecuteNonQuery errorhttp://forums.asp.net/t/1790031.aspx/1?ExecuteNonQuery+errorFri, 06 Apr 2012 18:28:24 -040017900314919857http://forums.asp.net/p/1790031/4919857.aspx/1?ExecuteNonQuery+errorExecuteNonQuery error <p>Error 102 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'ExecuteNonQuery' and no extension method 'ExecuteNonQuery' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?) C:\Users\pawii\Downloads\Compressed\ProfileSample\ProfileSample\insertdata.aspx.cs 31 24 C:\...\ProfileSample\</p> 2012-04-06T15:17:07-04:004919878http://forums.asp.net/p/1790031/4919878.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>Error 102 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'ExecuteNonQuery' and no extension method 'ExecuteNonQuery' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?) C:\Users\pawii\Downloenter code hereads\Compressed\ProfileSample\ProfileSample\insertdata.aspx.cs 31 24 C:\...\ProfileSample\</p> <p></p> <p>using System;`enter code here`<br> using System.Collections;<br> using System.Configuration;<br> using System.Data;<br> using System.Data.SqlClient;<br> using System.Linq;<br> using System.Web;<br> using System.Web.Security;<br> using System.Web.UI;<br> using System.Web.UI.HtmlControls;<br> using System.Web.UI.WebControls;<br> using System.Web.UI.WebControls.WebParts;<br> using System.Xml.Linq;</p> <p>public partial class insertdata : System.Web.UI.Page<br> {<br> protected void Page_Load(object sender, EventArgs e)<br> {</p> <p>}<br> protected void Button1_Click(object sender, EventArgs e)<br> {<br> SqlConnection con = new SqlConnection();<br> con.ConnectionString=&quot;Data Source=SQLEXPRESS;AttachDbFilename=|DataDirectory|stu.mdf;Integrated Security=True;User Instance=True&quot;;<br> con.Open();<br> String st = &quot;Insert into info values(@first,@second)&quot;;<br> SqlCommand cmd = new SqlCommand(st, con);<br> cmd.Parameters.AddWithValue(&quot;@first&quot;, TextBox1.Text);<br> cmd.Parameters.AddWithValue(&quot;@second&quot;, TextBox2.Text);<br> SqlCommand myCom=new SqlCommand(st,con);<br> int numrow=con.ExecuteNonQuery();<br> con.Close();<br> }<br> }</p> 2012-04-06T15:41:25-04:004919916http://forums.asp.net/p/1790031/4919916.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>Change this:</p> <p>int numrow=con.ExecuteNonQuery();</p> <p>To this:</p> <p>int numrow=cmd.ExecuteNonQuery();</p> <p>And get rid of this (even though it has nothing to do with the error... it isn't doing anything in fact, which is why you should get rid of it):</p> <p>SqlCommand myCom=new SqlCommand(st,con);</p> 2012-04-06T16:08:14-04:004919924http://forums.asp.net/p/1790031/4919924.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>String st = &quot;Insert into info values(@first,@second)&quot;;<br> SqlCommand cmd = new SqlCommand(st, con);<br> cmd.Parameters.AddWithValue(&quot;@first&quot;, TextBox1.Text);<br> cmd.Parameters.AddWithValue(&quot;@second&quot;, TextBox2.Text);<br> SqlCommand myCom=new SqlCommand(st,con);<br> int numrow=<strong>myCom</strong>.ExecuteNonQuery();&nbsp;&nbsp;&nbsp; //replace con with myCom<br> con.Close();</p> 2012-04-06T16:12:11-04:004920038http://forums.asp.net/p/1790031/4920038.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>&nbsp;tdmca<br> your help removed the error but<br> it now created a run time error<br> <br> <br> </p> <h1>Server Error in '/ProfileSample' Application.</h1> <hr width="100%" size="1" color="silver"> <h2><i>Must declare the scalar variable &quot;@first&quot;.</i></h2> <p><span></span><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description:&nbsp;</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.&nbsp;<br> <br> <b>Exception Details:&nbsp;</b>System.Data.SqlClient.SqlException: Must declare the scalar variable &quot;@first&quot;.<br> </span>&nbsp;<br> <br> &nbsp;</p> <pre> cmd.Parameters.AddWithValue(&quot;@second&quot;, TextBox2.Text); Line 30: SqlCommand myCom=new SqlCommand(st,con); <span color="red" style="color:red">Line 31: int numrow = myCom.ExecuteNonQuery(); </span>Line 32: con.Close(); Line 33: }</pre> 2012-04-06T17:59:05-04:004920049http://forums.asp.net/p/1790031/4920049.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>ur code also removed the error but it also gave me a runtime error<br> <br> </p> <h1>Server Error in '/ProfileSample' Application.</h1> <hr width="100%" size="1" color="silver"> <h2><i>Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.<br> The statement has been terminated.</i></h2> <p><span></span><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:&nbsp;</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.&nbsp;<br> <br> <b>Exception Details:&nbsp;</b>System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.<br> The statement has been terminated.<br> <br> <b>Source Error:</b>&nbsp;<br> <br> </span></span></p> <table width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td> <pre>Line 29: cmd.Parameters.AddWithValue(&quot;@second&quot;, TextBox2.Text); Line 30: //SqlCommand myCom=new SqlCommand(st,con); <span color="red" style="color:red">Line 31: int numrow = cmd.ExecuteNonQuery(); </span>Line 32: con.Close(); Line 33: }</pre> </td> </tr> </tbody> </table> <p>&nbsp;</p> 2012-04-06T18:07:35-04:004920051http://forums.asp.net/p/1790031/4920051.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>If you had followed my post, you wouldn't have had this error.&nbsp;&nbsp; He had you use the wrong command object.&nbsp;&nbsp; You assigned the parameters to the other command object:&nbsp; the one I told you to use.</p> 2012-04-06T18:07:54-04:004920054http://forums.asp.net/p/1790031/4920054.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p></p> <blockquote><span class="icon-blockquote"></span> <h4>kpawii</h4> <p></p> <h2><i>Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.<br> The statement has been terminated.</i></h2> <p></p> </blockquote> <p></p> <p>This isn't a code error.&nbsp;&nbsp; It is correctly telling you that you can't insert a duplicate value into a Primary Key column.&nbsp;&nbsp; Change the values that you are trying to insert.</p> <p>&nbsp;</p> 2012-04-06T18:10:10-04:004920065http://forums.asp.net/p/1790031/4920065.aspx/1?Re+ExecuteNonQuery+errorRe: ExecuteNonQuery error <p>yeah<br> but data is not going in the table<br> when i first enter the data in text box and click on submit then page is postback immediately<br> but nothing hppnd and when i again click on it it comes up with an error<br> but if data is going then where is it???<br> u solved my problem but wheres the data&nbsp;</p> 2012-04-06T18:28:24-04:00