Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. what is the solution for this?

what is the solution for this?

Scheduled Pinned Locked Moved ASP.NET
questiondatabasecomdesignsysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    senthil_rajesh_kavin
    wrote on last edited by
    #1

    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:

    B 1 Reply Last reply
    0
    • S senthil_rajesh_kavin

      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:

      B Offline
      B Offline
      badgrs
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • B badgrs

        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.

        S Offline
        S Offline
        senthil_rajesh_kavin
        wrote on last edited by
        #3

        How it should be Acheived? Please tell.

        I B 2 Replies Last reply
        0
        • S senthil_rajesh_kavin

          How it should be Acheived? Please tell.

          I Offline
          I Offline
          Irshad Sulaimani
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S senthil_rajesh_kavin

            How it should be Acheived? Please tell.

            B Offline
            B Offline
            badgrs
            wrote on last edited by
            #5
            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();
            
            }
            }
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups