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
How asp.net mvc differs from asp.net web forms? : asp.net mvc
What is a master page and what does it do?
What is application and session in asp.net?
Can one dll file contains the compiled code of more than one .net language?
What is the default timeout for a cookie?
What is the biggest disadvantage of “Other Return Types” in Web API?
How response object is related to asp's response object?
What is slidemaster?
What are the different validators in asp.net?
What is a uri query?
How can I have a particular web page in an asp.net application which displays its own error page?
How do you secure your configuration files to be accessed remotely by unauthorized users?
How does session state work in asp.net?
What is the concepts of globalization and localization in .net?
Explain the asp.net mvc request life cycle? : asp.net mvc