Insertion Error in Datagridview

I have a userdataTable with a "Code" column, which I installed as PK and automatically incremented "true" in MySql DB.

I want users to fill in values ​​for first name, last name, username, etc. in the datagrid view, but could not enter the code value.

I have this code to update / insert:

private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    try
    {
        if (dgvUsers.RowCount > 0)
        {
            for (int i = 1; i <= dgvUsers.RowCount; i++)
            {
                var code = dgvUsers.Rows[i].Cells[0].Value.ToString();

                if (code == string.Empty)
                {
                    // add users                         
                    this.usersTableAdapter.Insert(
                        dgvUsers.Rows[i].Cells[1].Value.ToString(),
                        dgvUsers.Rows[i].Cells[2].Value.ToString(),
                        dgvUsers.Rows[i].Cells[3].Value.ToString(),
                        GlobalClass.MD5Hash(dgvUsers.Rows[i].Cells[4].Value.ToString()),
                        DateTime.Now,
                        null
                        );
                }
                else
                {
                    // edit users                         
                    this.usersTableAdapter.Update(this.eko_payrollDataSet.users);
                }
            }
        }

        MessageBox.Show("Details Updated Successfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}     

Table structure:

Code      int        NN      PK   Autoincrement
firstName Varchar    NN
lastName  Varchar    NN
userName  Varchar    NN
password  varchar    NN
created   datetime   NN
modified  datetime   Null?

I dragged the datagridview into the form from the dataset that created the binding source. When I click the + button to add a new line, and when you finish typing the values, I get NoNullAllowedExeption for column Codewhen I move the cursor to another line or try to add a line below this.

What do I need to do to fix this? I did not add a verification code that could cause this.

, http://www.databaseforum.info/5/857494.aspx

+4
1

PK , DataSet AutoIncrement AutoIncrementSeed, .

, , , - .

autoincrement-values-in-autogenerated

, , /datatable gridview.

.

+5

All Articles