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

    How to save values of dynamic textboxes in database via asp.net

    Hi friends
    i m creating dynamic textboxes and the values of those textboxes want to save in db.
    how can i do it ?
    please anybody any source or solution or link.

    protected void Page_Load(object sender, EventArgs e)
    {
    Literal lt;
    Label lb;

    //Dynamic TextBox Panel
    pnlTextBox = new Panel();
    pnlTextBox.ID = "pnlTextBox";
    pnlTextBox.BorderWidth = 1;
    pnlTextBox.Width = 300;
    this.form1.Controls.Add(pnlTextBox);
    lt = new Literal();
    lt.Text = "<br />";
    this.form1.Controls.Add(lt);
    lb = new Label();
    lb.Text = "Dynamic TextBox<br />";
    pnlTextBox.Controls.Add(lb);

    //Button To add TextBoxes
    Button btnAddTxt = new Button();
    btnAddTxt.ID = "btnAddTxt";
    btnAddTxt.Text = "Add Features";
    btnAddTxt.Click += new System.EventHandler(btn1Add_Click);
    this.form1.Controls.Add(btnAddTxt);
    if (IsPostBack)
    {
    RecreateControls("txtDynamic", "TextBox");
    // RecreateControls("ddlDynamic", "DropDownList");
    }


    }

    protected void btn1Add_Click(object sender, EventArgs e)
    {
    Button btn = (Button)sender;
    int cnt = FindOccurence("txtDynamic");
    TextBox txt = new TextBox();
    txt.ID = "txtDynamic-" + Convert.ToString(cnt + 1);
    pnlTextBox.Controls.Add(txt);

    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlTextBox.Controls.Add(lt);
    }
    private int FindOccurence(string substr)
    {
    string reqstr = Request.Form.ToString();
    return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
    }
    private void RecreateControls(string ctrlPrefix, string ctrlType)
    {
    string[] ctrls = Request.Form.ToString().Split('&');
    int cnt = FindOccurence(ctrlPrefix);
    if (cnt > 0)
    {
    Literal lt;
    for (int k = 1; k <= cnt; k++)
    {
    for (int i = 0; i < ctrls.Length; i++)
    {
    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()))
    {
    string ctrlName = ctrls[i].Split('=')[0];
    string ctrlValue = ctrls[i].Split('=')[1];

    //Decode the Value
    ctrlValue = Server.UrlDecode(ctrlValue);

    if (ctrlType == "TextBox")
    {
    TextBox txt = new TextBox();
    txt.ID = ctrlName;
    txt.Text = ctrlValue;
    pnlTextBox.Controls.Add(txt);
    lt = new Literal();
    lt.Text = "<br />";
    pnlTextBox.Controls.Add(lt);
    }


    break;
    }
    }
    }
    }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
    ClsSubcategories1 obj = new ClsSubcategories1();
    int result = 0;
    try
    {
    obj.FeatureDesc = Convert.ToString(pnlTextBox.GroupingText[0].ToString());

    result = obj.AddFeeture();

    }
    catch (Exception ex)
    {
    throw ex;
    }
    if (result != 0)
    {
    lblresult1.Text = "Saved Successfully";
    }
    else
    {
    lblresult1.Text = "Not Saved Please try again";
    }
    }

    Server Error

    Server Error in '/' Application.

    Index was outside the bounds of the array.

    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.IndexOutOfRangeException: Index was outside the bounds of the array.

    Source Error:


    Line 293: catch (Exception ex)
    Line 294: {
    Line 295: throw ex;
    Line 296: }
    Line 297: if (result != 0)

    Source File: d:\C#Practice\StoreInventory\StoreInventory\Subcategories.aspx.cs Line: 295

    Stack Trace:


    [IndexOutOfRangeException: Index was outside the bounds of the array.]
    StoreInventory.Subcategory.btnAdd_Click(Object sender, EventArgs e) in d:\C#Practice\StoreInventory\StoreInventory\Subcategories.aspx.cs:295
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9750842
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +196
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
  • #766163
    Hi,
    Line pnlTextBox.GroupingText[0].ToString() is giving this error so replace your btnAdd_Click() event with this one:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
    ClsSubcategories1 obj = new ClsSubcategories1();
    int result = 0;
    try
    {
    //string sz= Convert.ToString(pnlTextBox.GroupingText[0].ToString());
    foreach (Control cCont in pnlTextBox.Controls)
    {
    if (cCont is TextBox)
    {
    TextBox txtDynamic = pnlTextBox.FindControl(cCont.ClientID) as TextBox;
    obj.FeatureDesc = txtDynamic.Text;
    Console.WriteLine(obj.FeatureDesc);
    }
    }
    result = obj.AddFeeture();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    if (result != 0)
    {
    lblresult1.Text = "Saved Successfully";
    }
    else
    {
    lblresult1.Text = "Not Saved Please try again";
    }
    }

  • #766167
    Hi,

    As per error details, if you try to access out side range value then you will get this type of error, for example in your array object you have 5 records but you are trying to access 6th record; since you don't have 6th record it will throw "Index was outside the bounds of the array" error.

    So, I suggest you to debug the code and check in which line you got this error and check the index value in that line.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766171
    Hi

    Index was outside the bounds of the array

    If you using looping or array concept in your code?

    there index not coming proper index value

    so keep debug and trace in your code step by step
    some index not coming that is the issue.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766172
    see below snippet in which we have Dynamically creating and adding TextBoxes on Button Click
    When the Add Button is clicked, first the count of all the TextBoxes present in the Panel control is determined and then it is incremented to generate an Index which will be used for generating a unique ID for the TextBox control.
    The unique ID is passed to the CreateTextBox method, which creates a dynamic TextBox and appends it to the Panel control.
    see below snippet

    protected void AddTextBox(object sender, EventArgs e)
    {
    int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
    this.CreateTextBox("txtDynamic" + index);
    }

    private void CreateTextBox(string id)
    {
    TextBox txt = new TextBox();
    txt.ID = id;
    pnlTextBoxes.Controls.Add(txt);
    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlTextBoxes.Controls.Add(lt);
    }

    protected void GetTextBoxValues(object sender, EventArgs e)
    {
    string message = "";
    foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
    {
    message += textBox.ID + ": " + textBox.Text + "\\n";
    }
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments