Search code examples
c#sqlwinformsdatagridviewms-access-2016

Cannot update Access Database DataGridView


I'm trying to somehow update or retrieve again data from Access database (Random piece of data with column names)

I'm using Microsoft.ACE.OLEDB.12.0.

I created a query which should return records where a Column has a value between two dates:

SQL example:

SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki  
FROM Arkusz1 
WHERE( 
     ((Arkusz1.Umówione) = Yes) // or 1 or <> 0
 AND ((Arkusz1.Zakończone) = No)  // or 0 doesen't provide right answer
 AND ((Arkusz1.Data_umówienia) BETWEEN 24/07/2021 AND 26/07/2021) ) 
 ORDER BY Arkusz1.Data_umówienia;

My code:

void fillGrid()
{
    string teraz = DateTime.Today.AddDays(-1).ToShortDateString().Replace('.', '/');
    string jutro = DateTime.Today.AddDays(1).ToShortDateString().Replace('.', '/');
    // AddDays +-1 because of my data issue, that's not important

    string dzisiejsze = "SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia," 
        + " Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki  FROM Arkusz1" 
        + " WHERE( ((Arkusz1.Umówione) = Yes) AND ((Arkusz1.Zakończone) = No) AND " 
        + "((Arkusz1.Data_umówienia) BETWEEN "
        + teraz + " AND " + jutro + ") )" 
        + " ORDER BY Arkusz1.Data_umówienia;";
        //selecting almost every column from my only sheet (Arkusz1), 
        //if Umówione (appointment is arranged) has True value AND issue is not ended (Zakończone) 
        //AND appointment date is between yesterday and tomorrow 
        //(as I said, that's all because my stupid ideas in creating DB)

    connection.Open();

    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dzisiejsze, connection);
    DataTable dt = new DataTable();
    dataAdapter.Fill(dt);
    TodaysPlan_dataGridView.DataSource = dt;
    TodaysPlan_dataGridView.Refresh();

    connection.Close();
}

I even added a special button to update the whole grid (firstly it was supposed to update after a second Form is closed):

private void refreshGrid_Click(object sender, EventArgs e)
{
    Application.DoEvents(); // because I saw something on stack, didn't work
    fillGrid(); //start filling again, didn't work too
    TodaysPlan_dataGridView.Refresh(); // same, didn't work
}

I don't get any error, execution (even in debug-mode) just passes by the code in refreshGrid_Click.

Edit: Tried to execute that SQL code for random data, didn't work too Ofc that's not the most important part of my question but I could ask that in addition :)

I'll also provide code for saving button from second form, but in my opinion it won't be really helpful:

private void save_button_Click(object sender, EventArgs e)
{
    if ((Tick_label.Visible == true) && (IDInfo_label.Text == "All good!") && (street_textBox.TextLength > 0))
    {
        connection.Open();
        string cmd_string = "INSERT INTO Arkusz1 (ID, Adres, Przydzielenie) VALUES ('" + ID_maskedTextBox.Text + "', '" + street_textBox.Text + "','" + startDate_dateTimePicker.Value + "')";
        OleDbCommand command = new OleDbCommand(cmd_string, connection);
        command.ExecuteNonQuery();
        connection.Close();

        this.DialogResult = DialogResult.OK;
        this.Close();
    }
    else
    {
        MessageBox.Show("error");
    }
}

That's my first question on Stack, any suggestions and constructive criticism are welcome!

BTW, I was using this YT tutorial, hope it helps someone :)


Solution

  • All right, informations provided by @Steve and @Jimi were really helpful, firstly I changed string formatting using String.Format() and resolved SQL problem - which was (#date#) formatting:

    DateTime tmp_teraz = DateTime.Today.AddDays(-1);
    string teraz = String.Format("{0}/{1}/{2}", tmp_teraz.Month.ToString(), 
    tmp_teraz.Day.ToString(), tmp_teraz.Year.ToString());
    
    DateTime tmp_jutro = DateTime.Today.AddDays(1);
    string jutro = String.Format("{0}/{1}/{2}", tmp_jutro.Month.ToString(), tmp_jutro.Day.ToString(), tmp_jutro.Year.ToString());
    
    string dzisiejsze = String.Format("SELECT Arkusz1.ID, Arkusz1.Adres, Arkusz1.Umówione, Arkusz1.Data_umówienia, Arkusz1.Zakończone, Arkusz1.[Spisano?], Arkusz1.Notatki  FROM Arkusz1 WHERE( ((Arkusz1.Umówione) = true) AND ((Arkusz1.Zakończone) = false) AND ((Arkusz1.Data_umówienia) " +
                "BETWEEN (#{0}#) AND (#{1}#)) ) ORDER BY Arkusz1.Data_umówienia;", teraz, jutro);
    

    At the end, grid looks like this.

    Refreshing or inserting data with dataGrid doesen't work, but that's a thing I'll develop over time.

    Thanks for help and have a nice day guys!