0

I'm working first time with local data base of visual studio 2013.

  1. C# Form Application in visual studio 2013
  2. Visual Studio 2013 local data base.

"Select" query is working perfect in every situation/condition. But the "insert" query is not inserting the data into the data base and nor showing any king of error. There is not any kind of code error (object decleration ) etc. If i exactly execute the query in SQLQuery tab of visual studio every thing working fine. Here my app.config.

  <connectionStrings>
<add name ="connString" connectionString ="Data Source=(LocalDB)\v11.0;AttachDbFilename= |DataDirectory|\KhalidDatabase.mdf ;Integrated Security=True"/>

Here's the code:

try
{
    constring = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
    conDataBase = new SqlConnection(constring);

    SqlCommand cmdDataBase = new SqlCommand("INSERT INTO invoice (customer,date,type,totalBill,printedBy) VALUES ('2','12/12/18','Credit','6000','1');", conDataBase);
    conDataBase.Open();
    SqlDataReader reader = cmdDataBase.ExecuteReader();
    MessageBox.Show("Record Saved Successfully", "SavedSuccessfully");    
    conDataBase.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString(), "Error");
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Why `ExecuteReader` ? this is better to change it to `ExcuteNoneQuery` while using `insert into` – Aria Jan 06 '18 at 08:51
  • This is not MySql, (you can't have that problem in MySql) but Sql Server LocalDb with an attached database file using the DataDirectory folder. See the duplicate – Steve Jan 06 '18 at 09:09

0 Answers0