0

I am generating dynamic textbox for that I have 2 tables:

  1. dynamic
  2. empdetail

Here empdetail is a master table and dynamically generated column is inserted in that table and after that I also want to store data in database. But the problem is when i save the dynamic textbox value in database, the value will be insert null in database i cant see the dynamic textbox value...

Pls help me to solve my problem. My code is below...... My Dynamic TextBox id is="TxtDynamic"

protected void Button3_Click(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=vaio\\sqlexpress;Initial Catalog=emp;User ID=sa;Password=administrator";
    con.Open();
    //TextBox tb = (TextBox)Panel1.FindControl("TxtDynamic" + i.ToString());

    string query = "update empdetail set " + TextBox1.Text + " = '"+ Panel1.FindControl("TxtDynamic1" + i.ToString()) + "' where id=(select Max(id) from empdetail )";

    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();        
    con.Close();
}
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
Janak
  • 17
  • 3
  • Have you tried to debug your program ? What query is generated ? On witch button do you have problem ? – Max Sep 19 '14 at 20:51
  • on button 3 when i insert dynamic textbox data......... – Janak Sep 20 '14 at 14:47
  • PLease help me to solve my problem.. – Janak Sep 21 '14 at 09:45
  • 1
    Class work for the school ? Add breakpoint on button3_click at line **SqlCommand cmd = ..** launch the application and when hit the breakpoint watch the query value, try the query from sqlserver managment console to see what's up. – Max Sep 21 '14 at 10:00
  • i have find exact that my query can not find dynamic text box id.. thats y data cant be inserted. see.... in my button3 query nd help me sort out.....coz when i have insert direct value in query row updated in databae.... – Janak Sep 22 '14 at 19:16
  • I'm Italian and I do not understand the English short forms, you may explain in a better way? Can you post the query in the button 3 ? – Max Sep 23 '14 at 07:46
  • Dear Max, thanks for your replay...I am explaining you in brief... that my Button3 has query, in that the dynamic TextBox identity I have passed but query can not get Dynamic TextBox Identity and Button3 can not insert data into database. When I Click on Button3 it saves blank value in database. That is my problem...If it is possible then you can give me your E-mail Id so that I can communicate briefly with u....? If you want so.... – Janak Sep 23 '14 at 18:24
  • Please don't concatenate strings into a SQL statement. Avoid the [Bobby Tables problem](http://codeblog.jonskeet.uk/2014/08/08/the-bobbytables-culture/) with parameterized queries. – Rick Liddle Sep 23 '14 at 18:57
  • Your problem is connected primarily with ASP.NET not with SQL, which you completely forgot to mention and did not use proper tags. So you can hardly expect correct answers. See this answer http://stackoverflow.com/a/19709167/2224701 – Vojtěch Dohnal Sep 23 '14 at 19:18

1 Answers1

0

Try calling the Prepare method on the SqlCommand objects ( cmd.Prepare()).

See SQL Command Prepare.

dburner
  • 1,007
  • 7
  • 22
  • I have tried to use sql prepare method..But I can not get the solution... Look on button3 query there is the main problem and help me to sort out... – Janak Sep 23 '14 at 18:52
  • His problem is probably rather that `Panel1.FindControl("TxtDynamic1" + i.ToString())` returns blank. – Vojtěch Dohnal Sep 23 '14 at 19:24