how to create textboxes dynamically and insert textbox text
into sql database
Answers were Sorted based on User's Feedback
Answer / aravind
TextBox1 = new TextBox();
TextBox1.ID = "TextBox1";
TextBox1.Style["Position"] = "Absolute";
TextBox1.Style["Top"] = "25px";
TextBox1.Style["Left"] = "100px";
Form1.Controls.Add(TextBox1);
and using
insert into columuname value ("+ textbox1.text+")
Is This Answer Correct ? | 12 Yes | 9 No |
Answer / 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 |
How to set/get parent page values in child page in asp.net
When Cookies are expired in ASP.NET?
0 Answers Sans Pareil IT Services,
How to disable validator control by client side JavaScript?
If i have 1000 records in dataset, how to do paging?
What is the difference between excute query and excute nonquery.?
what is service contract,operation contract?
How to use a Master Database in Asp.net?
what is Satellite Assembly?
What is the mvc model?
what is the difference between assemblyinfo and webconfig files?
What does assemblyinfo.cs file consist of?
What do you mean by serialize?