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 the good practice to implement validations in aspx page?
What is difference between Server.Transfer and Response.Redirect in ASP.NET?
Explain about asp.net 2.0 themes?
What is page request in asp.net?
Elaborate differentiation between Cache and Application?
What are the two types of web pages?
What are the different types of events are occured when a client requests an ASP.NET page from IIS server?
List some of the important session state modes of asp.net.
What is user control in asp.net?
What is state management technique?
What is enableviewstate?
In Data grid the question is below quantity price total these are 3 fields available in data grid if you enter quantity the total has to update automatically.Price field is already filled completely
How can we call webservices in Banking Applications? and where we are using it?
Explain Apache web servers ? How can you get ASP.NET running in Apache web servers - why should you do this?
Explain the code Access Security (CAS) in .net Framework?