how to create textboxes dynamically and insert textbox text
into sql database

Answers were Sorted based on User's Feedback



how to create textboxes dynamically and insert textbox text into sql database ..

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

how to create textboxes dynamically and insert textbox text into sql database ..

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

Post New Answer

More ASP.NET Interview Questions

How to set/get parent page values in child page in asp.net

3 Answers   Wipro,


When Cookies are expired in ASP.NET?

0 Answers   Sans Pareil IT Services,


How to disable validator control by client side JavaScript?

0 Answers  


If i have 1000 records in dataset, how to do paging?

2 Answers   Mind Tree,


What is the difference between excute query and excute nonquery.?

4 Answers  






what is service contract,operation contract?

2 Answers  


How to use a Master Database in Asp.net?

0 Answers   MCN Solutions,


what is Satellite Assembly?

2 Answers  


What is the mvc model?

0 Answers  


what is the difference between assemblyinfo and webconfig files?

1 Answers  


What does assemblyinfo.cs file consist of?

2 Answers   Accenture, HCL,


What do you mean by serialize?

0 Answers  


Categories