[英]How to insert datagridview record into mysql database using foreach
This code runs fine Values from the dataGridview are also saved succesfully but after saving the entered values in database a null row is also added in the tabl 这段代码运行良好,dataGridview中的值也可以成功保存,但是将输入的值保存在数据库中后,表行中还会添加一个空行
private void save_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)// picks data from dataGridview
{
try // MySql connection
{
string MyConnectionString = "Server=localhost; Database=markcreations; Uid=root; Pwd=admin";
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd = new MySqlCommand();
cmd = connection.CreateCommand();
cmd.Parameters.AddWithValue("@invoice", row.Cells["Invoice"].Value);
cmd.Parameters.AddWithValue("@jobOrder", row.Cells["jobOrder"].Value);
cmd.Parameters.AddWithValue("@dateTime", row.Cells["Date"].Value);
cmd.Parameters.AddWithValue("@clientCode", row.Cells["Client Code"].Value);
cmd.Parameters.AddWithValue("@clientName", row.Cells["Client Name"].Value);
cmd.CommandText = "INSERT INTO record(invoice, jobOrder, dateTime, clientCode, clientName)VALUES(@invoice, @jobOrder, @dateTime, @clientCode, @clientName)";
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MessageBox.Show("Records inserted.");
}
It looks like you have an empty row in the DataGridView, probably the last one, which is used for a new row. 看起来您在DataGridView中有一个空行,可能是最后一行,用于新行。 That row has null values for each column and if you dont check for it, it would insert the NULL's. 该行的每一列都有空值,如果您不检查它,它将插入NULL。
You can use row.IsNewRow
, to check for the new row. 您可以使用row.IsNewRow
来检查新行。
Thanks buddy it worked. 谢谢伙伴,它的工作。
Here is the Correct code along with row.IsNewRow 这是正确的代码以及row.IsNewRow
private void button5_Click(object sender, EventArgs e)
{
string MyConnectionString = "Server=localhost; Database=markcreations; Uid=root; Pwd=admin";
MySqlConnection connection = new MySqlConnection(MyConnectionString);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
MySqlCommand cmd = new MySqlCommand();
cmd = connection.CreateCommand();
if (row.IsNewRow) continue;
cmd.Parameters.AddWithValue("@invoice", row.Cells["Invoice"].Value);
cmd.Parameters.AddWithValue("@jobOrder", row.Cells["jobOrder"].Value);
cmd.Parameters.AddWithValue("@dateTime", row.Cells["Date"].Value);
cmd.Parameters.AddWithValue("@clientCode", row.Cells["Client Code"].Value);
cmd.Parameters.AddWithValue("@clientName", row.Cells["Client Name"].Value);
cmd.Parameters.AddWithValue("@jobName", row.Cells["Job Name"].Value);
cmd.Parameters.AddWithValue("@flexQuality", row.Cells["Flex Quality"].Value);
cmd.Parameters.AddWithValue("@sizeLength", row.Cells["Size Length"].Value);
cmd.Parameters.AddWithValue("@sizeWidth", row.Cells["Size Width"].Value);
cmd.Parameters.AddWithValue("@rate", row.Cells["Rate"].Value);
cmd.CommandText = "INSERT INTO record(invoice, jobOrder, dateTime, clientCode, clientName, jobName, flexQuality, sizeLength, sizeWidth, rate)VALUES(@invoice, @jobOrder, @dateTime, @clientCode, @clientName, @jobName, @flexQuality, @sizeLength, @sizeWidth, @rate)";
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MessageBox.Show("Records inserted.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.