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 Features in ASP.NET
What is the use of the tag in the web.config file?
Explain different types of Caching techniques in ASP.NET?
What is Model-View-View Model?
What describes a query?
What is enableviewstate?
Can we make activex dll also ti execute in some process as that of client ? How can we do?
can we remote debug applications with the remote debugger installed with vs.net 2002, with vs.net 2003?
If you are using two select queries and retrieving data. how do you access second query's result set using data reader?
What is _viewstart?
Can we have multiple web config files for an asp.net application?
What is application state?
How will you do Redo and Undo in a TextControl?
How is the asp.net mvc architecture different from others? : asp.net mvc
We are using Jscriopt validations and at clint site javascript is not running that time validation would work? if yes then how it would behave?