tags:

views:

122

answers:

2

I have this DataGridView and a BindingNavigator.. Whenever I add a new entry, it returns an error that says: 'Column 'newsID' does not allow nulls.'

But in my MySQL database, the newsID column is set to AutoIncrement. How will I solve this problem?

Thank you!

Here's my code:

Public Class FormNews
    Private Sub FormNews_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'UCWebDataSet.news' table. You can move, or remove it, as needed.
         Me.NewsTableAdapter.Fill(Me.UCWebDataSet.news)
    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        Me.Validate()
        Me.NewsBindingSource.EndEdit()
        Me.NewsTableAdapter.Update(Me.UCWebDataSet)
    End Sub
End Class
A: 

Are you using linq or datasets?

If so did you use the designer to create the dataset? What you need to do is specify that the query to insert to return the generated ID output.

This link should help you update your dataadapters to fix this issue:

MSDN Forum Post

Apologies if you are using linq, my answer is irrelevant.

EDIT: Just realised you're using MySql. You will need to do something different to get the auto increment value, and I can't remember if mysql works with the designers...

Spence
A: 

Problem is that the auto increment value used by the database is not known to you when adding the new record. I had the same problem with an underlying SQL Server 2005 database.

There must, however, be some way of configuring the typed dataset (I suppose in the properties for the field), because when I tried it on another project, all of a sudden new records where numbered automatically with -1, -2, etc IDs.

Upon inserting into the database, the real values were obtained and the temporary ones were replaced.

This might, however, apply only because I was using LINQ to SQL in that other project...

You might try to fill it with dummy values (e.g. negative tick count or something) and then set/retrieve the "real" ID yourself after inserting into the database.

Thorsten Dittmar