Kinyanjui Kamau
Kinyanjui Kamau

Reputation: 1986

Error with Insert in Datagridview

I have a usersdataTable with an "Code" column I set as PK and auto-increment "true" in MySql DB.

I want users to fill in values for first name, last name, username etc on a datagrid view but cannot enter the Code value.

I have this code for 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 to the form from a dataset that created a binding source. When I press the + button to add a new row and when finished entering the values, I get a NoNullAllowedExeption for column Code when I move the cursor to another row or attempt to add a row below this.

What do I need to do to fix this? I have not added validation code that would cause this.

I have seen the same problem I am experiencing here http://www.databaseforum.info/5/857494.aspx

Upvotes: 4

Views: 1148

Answers (1)

Swift
Swift

Reputation: 1881

When your PK is an auto-increment column, the associated column in the DataSet should have its proper AutoIncrement and AutoIncrementSeed, if not you should be able to set them in the dataset designer.

Here is an issue similar to yours, look if you can find something useful.

autoincrement-values-in-autogenerated

I think there is little about your problem, can you put an example or put all the properties of the dataset/datatable and gridview.

Good luck.

Upvotes: 5

Related Questions