0

I have a C# application with a datetime picker and datagrid. but cannot display the output from mysql database. my purpose is to gather the range within seleted dates.

private void loadbtn_Click(object sender, EventArgs e)
{
    LoadStockInHistory();
}

private void LoadStockInHistory()
{
    int i = 0;
    dataGridView1.Rows.Clear();
    con.conDB.Open();
    cmd = new MySqlCommand("SELECT * FROM vwstockin where sdate between '" + date1.Value.ToShortDateString() + "' and '" + date2.Value.ToShortDateString() + "' and status like 'Done'", con.conDB);
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        i++;
        dataGridView1.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString());
    }
    dr.Close();
    con.conDB.Close();
}

picture 2 picture 3

picure1

KennyTan
  • 95
  • 1
  • 8
  • Your code is vulnerable to **sql injection**, so use **prepared statements with oparameters** if you are sure that there is a row that fits the vulnerable criteria try to update the datgridview – nbk Jun 06 '20 at 08:51
  • @nbk could you suggest me the right codes? because im watching a tutorial with same codes on youtube – KennyTan Jun 06 '20 at 08:54
  • prepraed statements are explained in the manual. https://dev.mysql.com/doc/connector-net/en/connector-net-programming-prepared.html add a debug.write(i) after the while loop, to see if you got any rows at all. finally dataGridView1.update(); and dataGridView1.refresh();see https://stackoverflow.com/a/57889219/5193536 – nbk Jun 06 '20 at 09:01
  • Check whether your generated query is returning results in sql side (db side) – MBB Jun 06 '20 at 09:01
  • ToShortDateString() method will not return dates in a format recognised by mysql. The standard format in mysql is yyyy-mm-dd, but yyyy/mm/dd also works, this is what you need to generate. See the accepted answer in the duplicate question. – Shadow Jun 06 '20 at 09:02
  • I change my code to suggest answer from other post. https://imgur.com/a/vJqYIrl – KennyTan Jun 06 '20 at 09:23
  • but still no display in datagrid – KennyTan Jun 06 '20 at 09:24

0 Answers0