0

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.

  • 2
    In short you're doing an insert but trying to read data, pick one or the other. See the duplicate link, it solves this issue and one you didn't know you had about SQL injection. – Equalsk Oct 12 '17 at 09:23
  • @Equalsk i've come so far to know that it's something wrong with the connection string not the reding/inputing of the data – Hannes Corbett Oct 13 '17 at 09:32

1 Answers1

0

remove

myReader = cmdDataBase.ExecuteReader();
            while (myReader.Read())
            {

            }

put this code part

using(SqlConnection connection = new SqlConnection(_connectionString))
{
    String query = "INSERT INTO Products (Barcodes, Name, EDate, Quantity, Price) VALUES (@Barcodes, @Name, @EDate, @Quantity,@Price)";

    using(SqlCommand command = new SqlCommand(query, connection))
    {

        SqlCommand command = new SqlCommand(query, db.Connection);
        command.Parameters.AddWithValue("@Barcodes", this.tbxBar.Text )
        command.Parameters.AddWithValue("@Name", this.tbxName.Text )
        command.Parameters.AddWithValue("@EDate",this.dateDate.Value.Date)
        command.Parameters.AddWithValue("@Quantity",this.tbxQua.Text)
        command.Parameters.AddWithValue("@Price",this.tbxPrice.Text)

        int result = command.ExecuteNonQuery();

        // Check Error
        if(result < 0)
            // data insert error
    }
}

you have to execute your insert query. then only values get inserted to database

Kasup Sri
  • 159
  • 6
  • "command." doesn't exsist – Hannes Corbett Oct 12 '17 at 09:38
  • updated and correct the code – Kasup Sri Oct 12 '17 at 09:40
  • Tested it out but it works exactely like the code did so it saves it for the duration of the program being open but doesn't save it so i can close and open the program so i don't think i would have to change the query string at all it's probably something wrong with the connection string or so – Hannes Corbett Oct 13 '17 at 09:24
  • Tested it out with `string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf; Integrated Security = True";` And it worked but i mean i'm still getting the exact same constring if i use the other code so i can't figure out the problem – Hannes Corbett Oct 13 '17 at 09:29