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\
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\
using System;`enter code here`
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class insertdata : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString="Data Source=SQLEXPRESS;AttachDbFilename=|DataDirectory|stu.mdf;Integrated Security=True;User Instance=True";
con.Open();
String st = "Insert into info values(@first,@second)";
SqlCommand cmd = new SqlCommand(st, con);
cmd.Parameters.AddWithValue("@first", TextBox1.Text);
cmd.Parameters.AddWithValue("@second", TextBox2.Text);
SqlCommand myCom=new SqlCommand(st,con);
int numrow=con.ExecuteNonQuery();
con.Close();
}
}
String st = "Insert into info values(@first,@second)";
SqlCommand cmd = new SqlCommand(st, con);
cmd.Parameters.AddWithValue("@first", TextBox1.Text);
cmd.Parameters.AddWithValue("@second", TextBox2.Text);
SqlCommand myCom=new SqlCommand(st,con);
int numrow=myCom.ExecuteNonQuery(); //replace con with myCom
con.Close();
If you feel helped. Please mark this post as Answer
tdmca
your help removed the error but
it now created a run time error
Server Error in '/ProfileSample' Application.
Must declare the scalar variable "@first".
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: Must declare the scalar variable "@first".
cmd.Parameters.AddWithValue("@second", TextBox2.Text);
Line 30: SqlCommand myCom=new SqlCommand(st,con);
Line 31: int numrow = myCom.ExecuteNonQuery(); Line 32: con.Close();
Line 33: }
ur code also removed the error but it also gave me a runtime error
Server Error in '/ProfileSample' Application.
Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.
The statement has been terminated.
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: Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.
The statement has been terminated.
Source Error:
Line 29: cmd.Parameters.AddWithValue("@second", TextBox2.Text);
Line 30: //SqlCommand myCom=new SqlCommand(st,con);
Line 31: int numrow = cmd.ExecuteNonQuery(); Line 32: con.Close();
Line 33: }
If you had followed my post, you wouldn't have had this error. He had you use the wrong command object. You assigned the parameters to the other command object: the one I told you to use.
Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.
The statement has been terminated.
This isn't a code error. It is correctly telling you that you can't insert a duplicate value into a Primary Key column. Change the values that you are trying to insert.
-Tab Alleman
Marked as answer by kpawii on Apr 06, 2012 06:22 PM
yeah
but data is not going in the table
when i first enter the data in text box and click on submit then page is postback immediately
but nothing hppnd and when i again click on it it comes up with an error
but if data is going then where is it???
u solved my problem but wheres the data
kpawii
Member
9 Points
25 Posts
ExecuteNonQuery error
Apr 06, 2012 03:17 PM|LINK
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\
kpawii
Member
9 Points
25 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 03:41 PM|LINK
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\
using System;`enter code here`
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class insertdata : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString="Data Source=SQLEXPRESS;AttachDbFilename=|DataDirectory|stu.mdf;Integrated Security=True;User Instance=True";
con.Open();
String st = "Insert into info values(@first,@second)";
SqlCommand cmd = new SqlCommand(st, con);
cmd.Parameters.AddWithValue("@first", TextBox1.Text);
cmd.Parameters.AddWithValue("@second", TextBox2.Text);
SqlCommand myCom=new SqlCommand(st,con);
int numrow=con.ExecuteNonQuery();
con.Close();
}
}
TabAlleman
All-Star
15727 Points
2721 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 04:08 PM|LINK
Change this:
int numrow=con.ExecuteNonQuery();
To this:
int numrow=cmd.ExecuteNonQuery();
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):
SqlCommand myCom=new SqlCommand(st,con);
tdmca
Contributor
2396 Points
661 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 04:12 PM|LINK
String st = "Insert into info values(@first,@second)";
SqlCommand cmd = new SqlCommand(st, con);
cmd.Parameters.AddWithValue("@first", TextBox1.Text);
cmd.Parameters.AddWithValue("@second", TextBox2.Text);
SqlCommand myCom=new SqlCommand(st,con);
int numrow=myCom.ExecuteNonQuery(); //replace con with myCom
con.Close();
kpawii
Member
9 Points
25 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 05:59 PM|LINK
tdmca
your help removed the error but
it now created a run time error
Server Error in '/ProfileSample' Application.
Must declare the scalar variable "@first".
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: Must declare the scalar variable "@first".
cmd.Parameters.AddWithValue("@second", TextBox2.Text); Line 30: SqlCommand myCom=new SqlCommand(st,con); Line 31: int numrow = myCom.ExecuteNonQuery(); Line 32: con.Close(); Line 33: }kpawii
Member
9 Points
25 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 06:07 PM|LINK
ur code also removed the error but it also gave me a runtime error
Server Error in '/ProfileSample' Application.
Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.
The statement has been terminated.
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: Violation of PRIMARY KEY constraint 'PK_info'. Cannot insert duplicate key in object 'dbo.info'.
The statement has been terminated.
Source Error:
Line 29: cmd.Parameters.AddWithValue("@second", TextBox2.Text); Line 30: //SqlCommand myCom=new SqlCommand(st,con); Line 31: int numrow = cmd.ExecuteNonQuery(); Line 32: con.Close(); Line 33: }TabAlleman
All-Star
15727 Points
2721 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 06:07 PM|LINK
If you had followed my post, you wouldn't have had this error. He had you use the wrong command object. You assigned the parameters to the other command object: the one I told you to use.
TabAlleman
All-Star
15727 Points
2721 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 06:10 PM|LINK
This isn't a code error. It is correctly telling you that you can't insert a duplicate value into a Primary Key column. Change the values that you are trying to insert.
kpawii
Member
9 Points
25 Posts
Re: ExecuteNonQuery error
Apr 06, 2012 06:28 PM|LINK
yeah
but data is not going in the table
when i first enter the data in text box and click on submit then page is postback immediately
but nothing hppnd and when i again click on it it comes up with an error
but if data is going then where is it???
u solved my problem but wheres the data