0

So, I'm trying to make a record keeping app where user puts data through C# textboxes and is able to access them via DataGrid. When I insert data via textboxes and I click Submit (Unesi), data insertion goes well and all data is visible in Data Grid. However, after restarting appication, data is missing from both the DataGridView and Access Database even though I have Binding function.

This is my Binding function:

 private void BindDataGrid()
    {
        using (myConnection = new OleDbConnection(myConnectionString))
        {
            myConnection.Open();

            using (myOleDbDataAdapter = new OleDbDataAdapter())
            {
                DataSet myDataSet = new DataSet();

                myOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT * FROM SVIRKE", myConnection);
                myOleDbDataAdapter.Fill(myDataSet, "Svirke");

                bsSvirke.DataSource = myDataSet;
                bsSvirke.DataMember = "Svirke";
            }
            myConnection.Close();
        }
       // ClearTextBoxes();
    }

This is my Submit button code:

 private void txtSave_Click(object sender, EventArgs e)
    {
        //if (!String.IsNullOrEmpty(txtCompanyName.Text) && !String.IsNullOrEmpty(txtPhone.Text))
        //{
            using (myConnection = new OleDbConnection(myConnectionString))
            {
                myConnection.Open();
                try
                {
                    //string mySqlString = "UPDATE Shippers SET CompanyName = @CompanyName, Phone = @Phone WHERE ShipperID = @ID";
                    string mySqlString = "INSERT INTO Svirke (Naslov, Datum, Lokacija, [Kontakt osoba], [Kontakt broj], Pogodba, Zarada, Komentar) VALUES (@Naslov, @Datum, @Lokacija, @Kontaktosoba, @Kontaktbroj, @Pogodba, @Zarada, @Komentar)" ;
                  //SqlCommand MyCommand = new SqlCommand("INSERT INTO Products (Product_Name, Product_Price) Values (@ProductName, @productPrice)", myConnection);

                    OleDbCommand myCom = new OleDbCommand(mySqlString, myConnection);
                    myCom.Parameters.AddWithValue("@Naslov", textBox1.Text);
                    myCom.Parameters.AddWithValue("@Datum", dateTimePicker1.Text);
                    myCom.Parameters.AddWithValue("@Lokacija", textBox2.Text);
                    myCom.Parameters.AddWithValue("@Kontaktosoba",  textBox3.Text);
                    myCom.Parameters.AddWithValue("@Kontaktbroj",  textBox4.Text);
                    myCom.Parameters.AddWithValue("@Pogodba", textBox5.Text);
                    myCom.Parameters.AddWithValue("@Zarada", textBox6.Text);  //dataGridViewShippers.SelectedRows[0].Cells[0].Value.ToString()
                    myCom.Parameters.AddWithValue("@Komentar", richTextBox1.Text);
                    myCom.ExecuteNonQuery();
                    MessageBox.Show("Uspješan unos svirke");


                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "Svirka neuspješno unešena", MessageBoxButtons.OK);
                }
                myConnection.Close();
            }

            BindDataGrid();
        //}
    }

So, how can I make data stay in DataGrid and in Access database for good?

5
  • What does your connection string look like? Commented Jul 8, 2016 at 19:01
  • String myConnectionString = ConfigurationManager.ConnectionStrings["EviS.Properties.Settings.SvirkeConnectionString"].ConnectionString; Commented Jul 8, 2016 at 19:02
  • Ok, I meant what is the underlying connection string? Not the code to retrieve it from the app.config. What is the value? Are you connecting to the access database? Commented Jul 8, 2016 at 19:04
  • I have my Access database in the project folder and I'm connecting to it. Now I have changed 'Copy to output folder' to 'Copy if newer' and it keeps the data in DataGridView after restarting, but if I open Access database in the meantime, data is not there and dissapears from DataGridView also. Commented Jul 8, 2016 at 19:14
  • Focus on the words "Copy if"... Commented Jul 8, 2016 at 19:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.