How to save values of dynamic textboxes in database via asp.net
Hi friendsi 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