I can't seem to figure out what is making it to not save it after i close the program. I'm using Windows forms and i can se the product in the program after i've added it, but it won't be put into the database and saved so i can close and then open the program and it should still be there. I'm not getting any errors either so i can't seem to find what code isn't working. I had this code before and it was working but the only thing i changed was the constring so it would be relative to where it's placed.
private void btnAdd_Click(object sender, EventArgs e)
{
string constring = $"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=" + Directory.GetCurrentDirectory().ToString() + "\\BarcodeDB.mdf;Integrated Security=True";
string Query = "INSERT INTO Products (Barcodes, Name, EDate, Quantity, Price) VALUES ('" + this.tbxBar.Text + "','" + this.tbxName.Text + "','" + this.dateDate.Value.Date + "','" + this.tbxQua.Text + "','" + this.tbxPrice.Text + "') ;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
MessageBox.Show(constring);
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
}
Fillcombo();
}
catch(Exception ex)
{
if (ex.Message.Contains("con_barcode"))
{
MessageBox.Show("Barcode Already exists");
} else if (ex.Message.Contains("con_name"))
{
MessageBox.Show("Name already exsits");
} else
{
MessageBox.Show("An error has occured");
}
}
conDataBase.Close();
}
Trying to get better at asking questions please tell me if i missed out any important information. EDIT:
string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf; Integrated Security = True";
When i use this code it all works fine, but i still get the exact same connection string when i use the first code but i just want it to be relative to where ever i put the program.