how to create textboxes dynamically and insert textbox text
into sql database
Answer Posted / harsh
private void buttongenerate_Click(object sender, EventArgs
e)
{
int inputnumber = Int32.Parse
(TextboxInput.Text);
inputtextboxes = new List<TextBox>();
for (int i = 1; i <= inputnumber; i++)
{
Label labelinput = new Label();
TextBox textboxnewinpute = new TextBox();
labelinput.Text = "Input" + i;
labelinput.Location = new Point(30,
TextboxInput.Bottom + (i * 30));
labelinput.AutoSize = true;
textboxnewinpute.Text = "";
textboxnewinpute.Name = "MyTextbox";
textboxnewinpute.Location = new Point
(labelinput.Width, labelinput.Top - 3);
inputtextboxes.Add(textboxnewinpute);
this.Controls.Add(labelinput);
this.Controls.Add(textboxnewinpute);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
foreach (Control ctrl in Controls)
{
if (ctrl.Name == "MyTextbox")
{
if (con != null)
{
using (con)
{
int a = Convert.ToInt32
(TextboxInput.Text);
for (int i = 1; i <= a; i++)
{
cmd = new SqlCommand
("insert into DynamicDBdemo values ('" + ctrl.Text + "')",
con);
cmd.CommandType =
System.Data.CommandType.Text;
if
(cmd.Connection.State == System.Data.ConnectionState.Closed)
{
cmd.Connection.Open
();
}
}
if
(cmd.Connection.State == ConnectionState.Open)
{
cmd.ExecuteNonQuery
();
MessageBox.Show
("data added successfully");
}
}
}
}
}
}
catch (SqlException sx)
{
MessageBox.Show(sx.Message);
}
finally
{
con.Close();
}
}
| Is This Answer Correct ? | 3 Yes | 9 No |
Post New Answer View All Answers
Explain the overview of asp.net?
What are session state modes?
What is Web Server Control Templates.?
What is difference between inproc and outproc?
What is the benefit of WebAPI over WCF?
What is a query string in a url?
Explain program to call the js function when the change is being made in the dropdown list made in asp.net mvc? : asp.net mvc
Explain the differences between managed and unmanaged code?
What r the asp.net list controls and diff. Between them?
Where is session data stored in asp net?
Explain the use of duration attribute of @outputcache page directive.
Is asp.net core faster?
What is cas?
What are the advantages and disadvantages of session?
What are the asp.net 2.0 features?