1

I have no idea what seems to be the problem, please help if possible, any one can ?

try
{
     string Connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True; User Instance=True";

     SqlConnection cnn = new SqlConnection(Connectionstring);
     cnn.Open();

     SqlCommand cmd1 = new SqlCommand("insert into users values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text  + "','" + textBox4.Text + "',' " + int.Parse(textBox5.Text) +  " ')", cnn);
     SqlDataReader dr1 = cmd1.ExecuteReader();
     dr1.Close();
     cmd1.ExecuteNonQuery();

     int count = cmd1.ExecuteNonQuery();

     if (count > 0)
           MessageBox.Show(" Record inserted " + count );

     cnn.Close();
 }
  • Do you get any errors? – Christian Phillips May 25 '14 at 10:07
  • 5
    1) Why you use `ExecuteReader` here? That's seems pointless. 2) Why you execute your `cmd1` twice? 3) Use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attakcs. 4) Use [`using` statement](http://msdn.microsoft.com/en-us/library/yh598w02.aspx) to dispose your `SqlConnection` and `SqlCommand` – Soner Gönül May 25 '14 at 10:08
  • i did that just in case, because i couldn't find where is my problem – user3647102 May 25 '14 at 10:09
  • So you can't see the entry in the DB or you're going by the fact you don't see a message box? As @SonerGönül points out, you only need one `ExecuteNonQuery` here and no `SqlDataReader` is required. – Christian Phillips May 25 '14 at 10:10
  • 1
    Also: [Exploits of a Mom](http://xkcd.com/327/). – Filburt May 25 '14 at 10:11
  • I see the message box, but when i go to check the insertion process in my table, i find out that nothing is changed – user3647102 May 25 '14 at 10:11
  • Where is your `catch` block? – Christian Phillips May 25 '14 at 10:12
  • Look at [this answer](http://stackoverflow.com/questions/17147249/why-dont-changes-to-database-save/17147460#17147460) an tell me if this is your problem – Steve May 25 '14 at 10:12
  • 1
    Indeed, it it does. It is another "I never learned how to use my IDE, now it does things I do not understand" question. Standard problem, somehow. – TomTom May 25 '14 at 10:15
  • Steve, how can i find This "Copy" Property ? – user3647102 May 25 '14 at 10:20
  • Search on your Solution Explorer window for the file of your database. Click on it and look at the Properties Window (As an aside. really don't do a string concatenation to build a sql command text) – Steve May 25 '14 at 10:33

0 Answers0