Im working on a Project right now I did the same thing in WinForms a couple months back, so im pretty new to WPF and now I need some help.
I've been struggeling the past 2 Weeks with a multiple row insert from a DataGrid in WPF to my MySql Database.
I tried a couple solutions from online but non of them worked. Since WPF Datagrids dont have a "Rows" feature (I think, correct me if im wrong).
My Code in WinForms
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
try
{
string query = "INSERT INTO database VALUES (@b, @sn, @mac, @fb, @bf, @o, @be, @date)";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@b", dr.Cells[2].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@sn", dr.Cells[3].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@mac", dr.Cells[4].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@fb", dr.Cells[5].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@bf", dr.Cells[6].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@o", dr.Cells[7].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@be", dr.Cells[8].Value ?? DBNull.Value);
cmd.Parameters.AddWithValue("@date", dateTimePicker1.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I would Like to get some help or solutions on how to do the same thing in WPF since I've been struggling for the past 2 - 3 weeks.