1

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

    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.");
    }

2 Answers 2

1

It looks like you have an empty row in the DataGridView, probably the last one, which is used for a new row. That row has null values for each column and if you dont check for it, it would insert the NULL's.

You can use row.IsNewRow, to check for the new row.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks buddy it worked.

Here is the Correct code along with 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.");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.