syed  ali

syed ali

  • NA
  • 24
  • 6.6k

how to insert multiple textbox values on button click event

Jun 8 2015 3:50 AM
how to insert multiple textbox values on button click event in a single cloumn

Answers (10)

0
Rajeesh Menoth

Rajeesh Menoth

  • 67
  • 27k
  • 2.7m
Jun 12 2015 1:38 AM
Hi,
 
 Check the accepted answer in this link  : http://forums.asp.net/p/1447166/3296156.aspx
 
 Code:
 
using System.Collections.Specialized;
using System.Text;
using System.Data.SqlClient;
 
//At Button Click event
int numOfTextBox = 5;
StringCollection sc = new StringCollection();
for (int i = 1; i <= 5; i++)
//extract the TextBox values
TextBox txtQuestion =(TextBox)Page.FindControl("TextBoxQuestion" + i);
TextBox txtAnswer =(TextBox)Page.FindControl("TextBoxAnswer" + i);
//get the values here and add it in a collection
sc.Add(txtQuestion.Text + "," + txtAnswer.Text);
}
InsertRecords(sc);
//End
private void InsertRecords(StringCollection sc){
SqlConnection conn = new SqlConnection("Your Connection String");
StringBuilder sb = new StringBuilder(string.Empty);
string[] splitItems = null;
foreach (string item in sc)
{
const string sqlStatement = "INSERT INTO Table1 (Column1,Column2) VALUES";
if (item.Contains(","))
{
splitItems = item.Split(",".ToCharArray());
sb.AppendFormat("{0}('{1}','{2}'); ", sqlStatement, splitItems[0], splitItems[1]);
}
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
 
0
Saroj Kumar Sahu

Saroj Kumar Sahu

  • 0
  • 838
  • 8.3k
Jun 12 2015 1:30 AM
Hi Syed,
 
I think all of your textbox control is in the form not in master page. if so then find the below code for your reference.
 
protected void Button1_Click(object sender, EventArgs e)
{
//your sql connection
SqlConnection con = new SqlConnection("Data Source=yourserver;Initial Catalog=yourdatabase;User ID=user;Password=password");
int i = 1;
String All50Values = "";
string str = "insert into devicenumber(DeviceNumber) values (@devicenumber)";
while (i <= 50)
{
string id = "TextBox" + i;
TextBox txtobj = Page.FindControl(id) as TextBox;
if (txtobj.Text != "")
{
All50Values = All50Values + txtobj.Text + ","; //Note the delimter here is ,
}
else
{
Response.Redirect("Devicenumber.aspx");
}
i++;
}
//remove last comma from the string
All50Values = All50Values.Remove(All50Values.Length - 1);
try
{
//Do it Outside the While. Opening a connection is costly operation
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@devicenumber", All50Values);
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
}
}
 
Note:If all of your textbox control is in master page then replace the Line 
 
Page.FindControl(id) as TextBox
 
with 
 
Master.FindControl(id) as TextBox 
 
0
Inathi Gqola

Inathi Gqola

  • 0
  • 133
  • 1
Jun 11 2015 10:49 AM
Hi Syed,
 
This is how you would do your insert.  Of course you would have to include all your 50 parameters, I only included three.
 
private void yourButton_Click(object sender, EventArgs e)
    {
        string yourName =  txtName.Text;
        string yourSurname = txtSurname.Text;
        int sales = txtSales;
 
        yourDatabaseConn.Open();
 
        SqlCommand yourCommand = new Sql Command();
        yourCommand.Connection = yourDatabaseConn;
        yourCommand.CommandText = "INSERT INTO yourTable (yourName, yourSurname,           numOfSales)VALUES(@parameter1, @parameter2, @parameter3)";
 
         yourCommand.Parameters.AddWithValue("@parameter1",  yourName);
         yourCommand.Parameters.AddWithValue("@parameter2", yourSurname); 
         yourCommand.Parameters.AddWithValue("@parameter3", numOfSales); 
 
         yourCommand.ExecuteNonQuery();
 
         yourDatabaseConn.Close();
 
Happy Coding!
 
NB:  I have not tested the code.
 
0
Sivaraman Dhamodaran

Sivaraman Dhamodaran

  • 0
  • 36.3k
  • 3.4m
Jun 11 2015 10:20 AM
You should edit you code as follows (Note that it is not tested and I am leaving that to you)
 
protected void Button1_Click(object sender, EventArgs e) 
{
int i = 1; 
String All50Values = "";
string str = "insert into devicenumber(DeviceNumber) values (@devicenumber)"; 
while (i <= 50) 
string id = "TextBox" + i; 
TextBox txtobj = Master.FindControl(id) as TextBox; 
if(txtobj.Text!="") 
All50Values = All50Values + txtobj.Text + ","; //Note the delimter here is ,
else 
Response.Redirect("Devicenumber.aspx"); 
i++; 

//Do it Outside the While. Opening a connection is costly operation
con.Open(); 
cmd = new SqlCommand(str, con); 
cmd.Parameters.AddWithValue("@devicenumber", All50Values); 
cmd.ExecuteNonQuery();
con.Close(); 
0
syed  ali

syed ali

  • 0
  • 24
  • 6.6k
Jun 11 2015 10:02 AM
My question is i have one form which has around 50 textboxes, and i need to insert those textbox values into my one of the sql database table when the user click on the save button. 
0
syed  ali

syed ali

  • 0
  • 24
  • 6.6k
Jun 11 2015 9:56 AM
protected void Button1_Click(object sender, EventArgs e) { int i = 1; while (i <= 50) { con.Open(); string id = "TextBox" + i; TextBox txtobj = Master.FindControl(id) as TextBox; if(txtobj.Text!="") { string str = "insert into devicenumber(DeviceNumber) values (@devicenumber)"; cmd = new SqlCommand(str, con); cmd.Parameters.AddWithValue("@devicenumber", TextBox1.Text); cmd.ExecuteNonQuery(); } else { Response.Redirect("Devicenumber.aspx"); } i++; con.Close(); } } } sir please see my code
0
Pankaj  Kumar Choudhary

Pankaj Kumar Choudhary

  • 71
  • 26.6k
  • 13.5m
Jun 9 2015 10:36 AM
hello @Syed if you want to insert insert multiple textbox values on button click event in a single cloumn   then you can create a string that hold all textbox value with comma seprated.
 
then use a stored procedure in database that seprate all these value and insert each value into column separately 
0
Sivaraman Dhamodaran

Sivaraman Dhamodaran

  • 0
  • 36.3k
  • 3.4m
Jun 9 2015 8:11 AM
1) Collect the values from the text boxes and Append the values like:
string OneColumn = Textbox1.text + Textbox2.text + .... + textboxN.text 
 
2) Then insert the above OnEColumn String value to the database 
0
Saroj Kumar Sahu

Saroj Kumar Sahu

  • 0
  • 838
  • 8.3k
Jun 9 2015 7:52 AM
Hi Ali,
Can you please elaborate your question clearly  means in which format you want the data in that column ?
 
If you want all these vaue in one column without any separator/delimeter then Collect all the textboxes values into one string and update this into database column.
 
find the sample in below link