You must Sign In to post a response.
  • Category: ASP.NET

    Store dynamic textbox values in sql server database

    hi frinds
    if i enter the no 10 in textbox .i want to disply the 20 textboxes for regno and mark..and at same time that all textbox values will store the sql server database at button click .that database fields like regno and mark..how to solve this problm.if any code pls..
  • #769864
    You can create tax boxes dynamically and show on screen thats it

  • #769865
    that only a problm rajanikant..i disply the boxes but how to store that dynamic textbox values in database?if any code pls?

  • #769866
    Hi,

    You can refer below code.
    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:TextBox runat="server" ID="txt1"></asp:TextBox>
    <asp:Button runat="server" ID="Button1" Text="Geneate" OnClick="Button1_Click"/>
    <asp:Button runat="server" ID="btnSave" Text="Save Data" OnClick="btnSave_Click"/>
    <asp:Panel ID="Panel1" runat="server" EnableViewState="true"></asp:Panel>
    </form>

    </body>
    </html>

    Code behind:
    private void Generate(int count)
    {
    try
    {
    for (int i = 1; i <= count; i++)
    {
    TextBox txt = new TextBox();
    txt.Attributes.Add("runat", "server");
    txt.ID = "textbox" + i;
    txt.Text = "";
    Panel1.Controls.Add(txt);
    }
    }
    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    int count = Convert.ToInt32(txt1.Text);
    count = count * 2;
    Generate(count);
    Session["TotalCount"] = count;
    }
    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
    try
    {
    int totalcount = Convert.ToInt32(Session["TotalCount"]);
    StringBuilder sb = new StringBuilder();
    for(int i = 1; i <= totalcount; i++)
    {
    sb.AppendLine("textbox" + i + " Value: " + Request.Form["textbox" + i] + Environment.NewLine);
    }
    Response.Write(sb.ToString());
    }
    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }
    }

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


  • Sign In to post your comments