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
What is skin in asp.net?
Where the assembly is stored in asp.net?
What are the types of session in asp.net?
1.can we add connection string in global.asax?????????? 2.what are the default files included when we create new web application????
Describe a bubbled event and how it is used ?
Explain the difference between mvc (model-view-controller) and mvp (model-view-presenter)? : asp.net mvc
What is data grid view in asp.net?
What are the new login controls in asp.net 2.0?
What is the best Macanism to clear the Cache in asp.net
Describe paging in asp.net?
Types of instancing properties and explain each. Tell the difference between multiuse,singleuse and globalmultiuse and which is default ?
What's the use of formatters in .net?
Which library is used by the testers and developers to develop automated tests and create testing tools?
Explain a program using code nuggets to create a simple application? : asp.net mvc
What are the features of asp.net mvc?