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 convert any string to view state format in asp.net? §ÇÛ짬ªª³ö»Ç¾ö»ÂÇ¿¾»¼»¾ö¼ÉÇöÇÏǼ»Ãö®±ª§°ö«³ -- i want convert this string to view state.

1 Answers   iFlex,


what are the server controls used in sitepath navigation?

1 Answers  


How do you do validations. Whether client-side or server-side validations are better ?

2 Answers   InfoLinks, Microsoft,


What does this do? sn -t foo.dll

3 Answers  


Name the two properties are on every validation control?

0 Answers  






How to store a dataset in a viewstate?

3 Answers   Patni,


what is asp and asp.net ?

2 Answers   Inspira Technologies,


we are working in .net namespaces like using system.io,system.text. these namespace before we use "using " keyword what means of using?

7 Answers   CEI, CSC, HCL, Tech Mahindra,


How many Directives r in ASP.NET?

8 Answers  


What is the difference between application and cache variables?

6 Answers   Astadia,


What is HTTPContext and HTTPModule. What is the use of them?

1 Answers  


how we deploy the asp.net 2.0 application

2 Answers   Logistics,


Categories