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

What is the purpose of App_Code folder in ASP.NET? Why we this?

0 Answers   Sans Pareil IT Services,


What are Master Pages in ASP.NET?

0 Answers  


1.what is the application pool. 2.what is the HttpModile and Http Handler. 3.C# 3.0 Features ? 4.Anonoymous Type,methopd and claas in 3.0? 5.difference between statsic and const ? 6.session vs application 7.state management clint side and server side ? 8.Genric list 9.c# 3.0 vs 3.5

0 Answers   TCS,


Why is it important to maintain session state?

1 Answers  


How does session work in asp net?

0 Answers  






What is caching? What are the different types of caching?

6 Answers  


What are the different authentication modes in asp.net?

0 Answers  


Why session is necessary in web application?

0 Answers  


About friend and Protected friend ?

3 Answers   MMTS,


How to reduce the width of textbox in editcommandcolumn of datagrid?

0 Answers  


What is the good practice to implement validations in aspx page?

0 Answers  


What is a runtime version?

0 Answers  


Categories