1

Very frustrating one... I have tried many combinations of ', " and so on but my insert command just refreshing the page.

What am I doing wrong?

Simple two text fields form with button. Under button I have this:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["przychodniaConnectionString1"].ConnectionString);
con.Open();

string cmdStr = "INSERT INTO specyfik(speNazwa, speIlosc) values ('" + speNazwa.Text + "', '" + speIlosc.Text + "')";
SqlCommand insertCmd = new SqlCommand(cmdStr, con);
con.Close();

Zero errors while compiling and when testing, it seems like refreshed page. Nothing appears in db.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dargod
  • 334
  • 1
  • 15

3 Answers3

4

Don't you need to call insertCmd.ExecuteNonQuery() ?

...
SqlCommand insertCmd = new SqlCommand(cmdStr, con); 
int row_affected = insertCmd.ExecuteNonQuery();
...
a1ex07
  • 36,826
  • 12
  • 90
  • 103
4

You need to execute your SqlCommand:

insertCmd.ExecuteNonQuery();

Also, you should look into parameterizing that query: http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html

Mark S. Rasmussen
  • 34,696
  • 4
  • 39
  • 58
0

Will you like to make more improvements in your code using stored Proc and improvemnet in your code behind file ? Take a look at this answer...

https://stackoverflow.com/a/9595501/1209450

Community
  • 1
  • 1
Pankaj
  • 9,749
  • 32
  • 139
  • 283