what is the solution for this?
-
sir, i made the page_load as public,then the results shown below. public partial class Default2 : System.Web.UI.Page { public void Page_Load(object sender, EventArgs e) { TextBox t1 = new TextBox(); t1.ID = "TextBox1"; form1.Controls.Add(t1); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con); SqlDataReader rr=com.ExecuteReader(); } } ---------------------------- CS0103: The name 'TextBox1' does not exist in the current context Line 24: { Line 25: SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); Line 26: SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con);//this line in Red Color(error portion) Line 27: SqlDataReader rr=com.ExecuteReader(); Line 28:
-
sir, i made the page_load as public,then the results shown below. public partial class Default2 : System.Web.UI.Page { public void Page_Load(object sender, EventArgs e) { TextBox t1 = new TextBox(); t1.ID = "TextBox1"; form1.Controls.Add(t1); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con); SqlDataReader rr=com.ExecuteReader(); } } ---------------------------- CS0103: The name 'TextBox1' does not exist in the current context Line 24: { Line 25: SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); Line 26: SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con);//this line in Red Color(error portion) Line 27: SqlDataReader rr=com.ExecuteReader(); Line 28:
-
the TextBox1 variable is private to the Page_Load function, you need to assign the object to a variable declared in the class and accessible to all functions.
How it should be Acheived? Please tell.
-
How it should be Acheived? Please tell.
Declare the TextBox as a class level control.e.g. just before Page_Load, declare as: private TextBox t1 = new TextBox(); It'll work fine then..... Note: Right now u r declaring TextBox in Page_Load , once the control goes out of Page_Load, the TextBox1 context is gone. Hence this error is coming. If u declare it on class level , it'll be solved.
-
How it should be Acheived? Please tell.
public partial class Default2 : System.Web.UI.Page { private TextBox TextBox1; public void Page_Load(object sender, EventArgs e) { TextBox1 = new TextBox(); TextBox1.ID = "TextBox1"; form1.Controls.Add(TextBox1); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Server=localhost;uid='sa';pwd='sa';DataBase=master"); SqlCommand com=new SqlCommand("insert into table1 values('"+TextBox1.Text+"')",con); SqlDataReader rr=com.ExecuteReader(); } }