Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
*i created a textbox,button dynamically when i click on general button,now those two dynamic controls are appeared.

*but now i want code for
inserting dynamic textbox values into sqlserver when i click on dynamic button control
Posted
Updated 7-Nov-11 20:32pm
v2
Comments
Orcun Iyigun 8-Nov-11 2:23am    
What have you tried so far? what is making you stop to do that?

 
Share this answer
 
Comments
thatraja 8-Nov-11 9:32am    
5!
uspatel 8-Nov-11 23:53pm    
thanks
Use this link, and write your code for inserting value in SQL using appropriate event.


http://forums.asp.net/t/1286956.aspx/1?How+to+add+event+handlers+to+dynamic+controls[^]
 
Share this answer
 
Comments
thatraja 8-Nov-11 9:32am    
5!
You may follow below steps to work with your Dynamic Button and Textboxes.

1) Create an Event for your Dynamic Button and assign it to EventHandler of your Button.
C#
btnMyDynamicButton.Click += new System.EventHandler(this.btnMyDynamicButton_Click);

void btnMyDynamicButton_Click(object sender, System.EventArgs e)
{
  //Your Data Insert code here.
}

2) Try as below code for you Data-Insertion.
C#
SqlConnection conn = new SqlConnection("Your connection string here.");
string sql = "INSERT INTO connect_com (col1,col2,col3) VALUES (@Val1,@Val2,@Val3)";
try
{
  conn.Open();
  SqlCommand cmd = new SqlCommand(sql, conn);
  cmd.Parameters.AddWithValue("@Val1", myDynamicTextbox1.Text);
  cmd.Parameters.AddWithValue("@Val2", myDynamicTextbox2.Text);
  cmd.Parameters.AddWithValue("@Val3", myDynamicTextbox3.Text);
  cmd.CommandType = CommandType.Text;
  cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
  //Exception handling here
}
finally
{
  conn.Close();
}
 
Share this answer
 
Comments
thatraja 8-Nov-11 9:31am    
5! for the effort
If you have created text-boxes dynamically then what's the big deal about getting values from those controls?

C#
private void CreateTextBoxes()
  {

      for (int counter = 0; counter <= NumberOfControls; counter++)
      {
          TextBox tb = new TextBox();
          tb.Width = 150;
          tb.Height = 18;
          tb.TextMode = TextBoxMode.SingleLine;
          tb.ID = "TextBoxID" + (counter + 1).ToString();
          // add some dummy data to textboxes
          tb.Text = "Enter Title " + counter;
          phTextBoxes.Controls.Add(tb);
          phTextBoxes.Controls.Add(new LiteralControl("<br />"));
      }
  }
Also see this Creating Dynamic TextBox Controls in ASP.Net[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900