Start New Discussion within our Software Development Community

Hello,

I'm having some trouble with SQL CE.

The below code *should* insert a record into the 'maps' table. (table adapters all set up)

sometimes it says cannot insert as breaching primary key constraint, which is fine, but it otherwise fails with cannot insert NULL into iMapId. I have checked and txtNumber.Text.ToString is not null, and I've even tried explicitly setting newMap["iMapId"] equal to some integer and it still complains that it's null.

try
            {
                DataRow newMap = mapappdbDataSet.maps.NewRow();
                newMap["iRegionId"] = Convert.ToInt16(cmbRegion.SelectedValue);
                newMap["vMapName"] = txtMapNAme.Text.ToString();
                newMap["iMapId"] = txtNumber.Text.ToString();


                mapappdbDataSet.maps.Rows.Add(newMap);
                mapsTableAdapter.Update(newMap);
                mapappdbDataSet.AcceptChanges();

                this.Close();
            }
            catch (Exception excp)
            {
                MessageBox.Show("That didnt work. Heres the message: " + excp.Message.ToString());
            }

Any ideas at all?

Thanks

OK I just realised that the table USED to have an auto-incremental field for iMapId and I hadn't updated the tableadapter since.

what a silly billy....

This question has already been answered. Start a new discussion instead.