title: How to save data from dynamically created text box to database
body: <pre><code>&lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
    &lt;div&gt;
        &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
        &lt;asp:GridView runat="server" ID="gv"       AutoGenerateColumns="false" class="table table-striped table-advance table-hover" BorderWidth="2px"&gt;
              &lt;Columns&gt;
                  &lt;asp:BoundField DataField="#" HeaderText="#"  HeaderStyle-Height="50px" HeaderStyle-Font-Bold="true" HeaderStyle-Width="50px" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                  &lt;asp:BoundField DataField="Student_id" HeaderText="Student_id" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
  
                    &lt;asp:BoundField DataField="RegNo" HeaderText="RegNo" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                  &lt;asp:BoundField DataField="Student Name" HeaderText="Student Name" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                  &lt;asp:BoundField DataField="PhoneNO" HeaderText="PhoneNO" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                  &lt;asp:BoundField DataField="EmailId" HeaderText="EmailId" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                   &lt;asp:BoundField DataField="DOB" HeaderText="DOB" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                   &lt;asp:BoundField DataField="DOJ" HeaderText="DOJ" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="#66ccff" /&gt;
                  &lt;asp:TemplateField HeaderText="Marks Obt" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="160px" HeaderStyle-BackColor="#66ccff" &gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:TextBox ID="tb1" runat="server" class="form-control-smalltxtBox"&gt;&lt;/asp:TextBox&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                   &lt;asp:TemplateField HeaderText="Max Marks" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="160px" HeaderStyle-BackColor="#66ccff"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:TextBox ID="tb2" runat="server" class="form-control-smalltxtBox"&gt;&lt;/asp:TextBox&gt;
                      &lt;/ItemTemplate&gt;
                   &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
        &lt;/asp:GridView&gt;
        &lt;br /&gt;&lt;br /&gt;        
        &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;/div&gt;
    &lt;asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /&gt;
  &lt;/div&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p></p>
  
  <p>I have created one text box in </p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="Marks Obt" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="160px" HeaderStyle-BackColor="#66ccff" &gt;
      &lt;ItemTemplate&gt;
          &lt;asp:TextBox ID="tb1" runat="server" class="form-control-smalltxtBox"&gt;&lt;/asp:TextBox&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>And .cs class</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if(! IsPostBack)
      {
  
      }
      DataTable dt = Teacher_DataLayer.getStudent(3);
      gvPop(dt);
  }
  
  public void gvPop(DataTable eList)
  {
      try
      {
          DataTable dtt = new DataTable();
          DataRow drw = null;
          int i = 0;
          string id1 = "t1r";
          string id2 = "t2r";
          string sN = string.Empty;
          string sId = string.Empty;
          string sub = string.Empty;
          string mG = string.Empty;
          string mT = string.Empty;
          string ed = string.Empty;
          string et = string.Empty;
          string eaid = string.Empty;
          string regno = string.Empty;
          string phoneno = string.Empty;
          string emai = string.Empty;
          string dt1=string.Empty;
          string dt2=string.Empty;
          dtt.Columns.Add(new DataColumn("#", typeof(string)));
          dtt.Columns.Add(new DataColumn("Student_id", typeof(int)));
          dtt.Columns.Add(new DataColumn("RegNo", typeof(string)));
          dtt.Columns.Add(new DataColumn("Student Name", typeof(string)));
          dtt.Columns.Add(new DataColumn("PhoneNO", typeof(string)));
          dtt.Columns.Add(new DataColumn("EmailId", typeof(string)));
          dtt.Columns.Add(new DataColumn("DOB", typeof(string)));
          dtt.Columns.Add(new DataColumn("DOJ", typeof(string)));
          dtt.Columns.Add(new DataColumn("Marks Obt", typeof(string)));
          dtt.Columns.Add(new DataColumn("Max Marks", typeof(string)));
          foreach (DataRow dr in eList.Rows)
          {
              int cc = 1;
  
              foreach (object dc in dr.ItemArray)
              {
                  String cd = null;
                  if (dc == null)
                  {
                      cd = "null";
                  }
                  else
                  {
                      cd = dc.ToString();
                  }
  
                  if (cc == 1)
                  {
                      sId = cd;
                  }
                  else if (cc == 2)
                  {
                      regno = cd;
                  }
                  else if (cc == 3)
                  {
                      sN = cd;
                  }
                  else if (cc == 4)
                  {
                      phoneno = cd;
                  }
                  else if (cc == 5)
                  {
                      emai = cd;
                  }
                  else if (cc == 6)
                  {
                      dt1 = cd;
                  }
                  else if (cc == 7)
                  {
                      dt2 = cd;
                  }
                  else if (cc == 8)
                  {
                      mG = cd;
                  }
  
                  cc = cc + 1;
  
              }
  
              i = i + 1;
              drw = dtt.NewRow();
         drw["#"] = i;
              drw["Student_id"] = Convert.ToInt32(sId);
              drw["RegNo"] = regno;
              drw["Student Name"] = sN;
              drw["PhoneNO"] = phoneno;
              drw["EmailId"] = emai;
              drw["DOB"] = dt1;
              drw["DOJ"] = dt2;
  
              drw["Marks Obt"] = mG;
              //drw["Max Marks"] = maxM;
              dtt.Rows.Add(drw);
           //   dtt.Columns.Clear();
          }
  
         ViewState["CurrentTable"] = dtt;
          gv.DataSource = dtt;
          gv.DataBind();
          SetPreviousData();
  
      }
      catch (Exception ex) { }
  }
  
  public void SetPreviousData()
  {
      try
      {
          int rowIndex = 0;
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dt1 = (DataTable)ViewState["CurrentTable"];
              if (dt1.Rows.Count &gt; 0)
              {
  
                  for (int i = 0; i &lt; dt1.Rows.Count; i++)
                  {
                      TextBox box1 = (TextBox)gv.Rows[rowIndex].Cells[8].FindControl("tb1");
                      TextBox box2 = (TextBox)gv.Rows[rowIndex].Cells[9].FindControl("tb2");
  
                      box1.Text = dt1.Rows[i]["Marks Obt"].ToString();
                      //box2.Text = maxM;
  
                      rowIndex++;
                  }
              }
          }
      }
      catch (Exception ep) { }
  }
  
  protected void Button1_Click(object sender, EventArgs e)
  {
      try
      {
          DataTable dtN = new DataTable();
          DataRow drw = null;
          int rowIndex = 0;
          //dtN.Columns.Add(new DataColumn("#", typeof(string)));
          //dtN.Columns.Add(new DataColumn("Student_id", typeof(int)));
          //dtN.Columns.Add(new DataColumn("RegNo", typeof(string)));
          //dtN.Columns.Add(new DataColumn("Student Name", typeof(string)));
          //dtN.Columns.Add(new DataColumn("PhoneNO", typeof(string)));
          //dtN.Columns.Add(new DataColumn("EmailId", typeof(string)));
          //dtN.Columns.Add(new DataColumn("DOB", typeof(string)));
          //dtN.Columns.Add(new DataColumn("DOJ", typeof(string)));
          //dtN.Columns.Add(new DataColumn("Marks Obt", typeof(string)));
          //dtN.Columns.Add(new DataColumn("Max Marks", typeof(string)));
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dtV = (DataTable)ViewState["CurrentTable"];
              int ww= dtV.Rows.Count;
              if (dtV.Rows.Count &gt; 0)
              {
                  for (int i = 0; i &lt; dtV.Rows.Count; i++)
                  {
                      string sid = gv.Rows[rowIndex].Cells[1].Text;
                      int ss = Convert.ToInt32(sid);
                      string nn= gv.Rows[rowIndex].Cells[3].Text;
                      string ttt= gv.Rows[rowIndex].Cells[8].Text;
                  TextBox box1 = (TextBox)gv.Rows[rowIndex].Cells[8].FindControl("tb1");
                      string mo = box1.Text;
                  }
              }
          }
      }
      catch (Exception ex)
      {
  
      }
  }
  </code></pre>
  
  <p>On button click it is not able to find the control of text box I need to save the data entered in text box and student_id.</p>