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


Please Help Members By Posting Answers For Below Questions

Explain Features in ASP.NET

765


What is the use of the tag in the web.config file?

738


Explain different types of Caching techniques in ASP.NET?

730


What is Model-View-View Model?

775


What describes a query?

711


What is enableviewstate?

723


Can we make activex dll also ti execute in some process as that of client ? How can we do?

4449


can we remote debug applications with the remote debugger installed with vs.net 2002, with vs.net 2003?

1932


If you are using two select queries and retrieving data. how do you access second query's result set using data reader?

5142


What is _viewstart?

684


Can we have multiple web config files for an asp.net application?

709


What is application state?

680


How will you do Redo and Undo in a TextControl?

854


How is the asp.net mvc architecture different from others? : asp.net mvc

677


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?

1608