Update failure (ID Does Not Allow Null Value)

alander

Well-known member
Joined
Jun 26, 2007
Messages
120
Location
Singapore
Programming Experience
5-10
Eh? I dun understand y i get this error..

This is my code for my new button

VB.NET:
Me.EmployeesInfoBindingSource.AddNew()
Me.txtEmpId.Text = maxIdValue + 1
Me.EmployeesInfoBindingSource.EndEdit()


I do a endedit so that my child table can see the FK

However, this code isnt committed to the database yet, the primary key ISNT a autonumber its a number i assigned to in this case..

and so after user enter all the data for this the user clicks save, after passing all the validation it goes here
VB.NET:
Me.Validate()
EmployeesInfoBindingSource.EndEdit()
EmployeesInfoTableAdapter.Update(Me.DsEmployee.EmployeesInfo)

Exception occured at
EmployeesInfoTableAdapter.Update(Me.DsEmployee.EmployeesInfo)
PK cannot be a null value.. ??? Ain't it there already?
 
Last edited:
Eh? I dun understand y i get this error..

This is my code for my new button

VB.NET:
Me.EmployeesInfoBindingSource.AddNew()
Me.txtEmpId.Text = maxIdValue + 1
Me.EmployeesInfoBindingSource.EndEdit()
Try:

VB.NET:
Me.MyDataSet.EmployeesInfo.EmployeeIDColumn.DefaultValue = maxIdValue + 1
Me.EmployeesInfoBindingSource.AddNew()
Me.EmployeesInfoBindingSource.EndEdit()

I do a endedit so that my child table can see the FK
Wise move. Though you can do this:

ParentBindSOurce.AddNew()
ChildBindSOurce.AddNew()

the relation will work, until you EndEdit either of them. If you end the child first you get "parent record not found". if you end the parent first, it destroys the related child.


PK cannot be a null value.. ??? Ain't it there already?
Maybe not. I would never rely on setting the text of a bound control to update the datasource. Update the datasource directly (or in this case, program a default value before you perform the add
 
oic.. any tutorial on how to set a default value, via the dataset designer or via the form that is involved
 
It's in my post?

cjard said:
VB.NET:
[B]Me.MyDataSet.EmployeesInfo.EmployeeIDColumn.DefaultValue = maxIdValue + 1[/B]

What more advice do you need than:

DataSetInstance.DataTableName.ColumnNameColumn.DefaultValue = Something
 
ye i did that, but i still get that error..

i checked my dataset designer, it shows <dbNull> as default value however when i click new in the form, the value does show up as what i desired, when it saves i suppose for some reason goes back to null..
 
Last edited:
my current code now is
VB.NET:
Private Sub btnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNew.Click
        
Me.DsEmployee.EmployeesInfo.EmployeeIDColumn.DefaultValue = maxIdValue + 1
Me.EmployeesInfoBindingSource.AddNew()
Me.EmployeesChild1BindingSource.AddNew()
...

VB.NET:
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

EmployeesInfoBindingSource.EndEdit()
EmployeesInfoTableAdapter.Update(Me.DsEmployee.EmployeesInfo) 'Exception here Index or primary key cannot contain a null value
EmployeesChild1BindingSource.EndEdit()
EmployeesChild1EmployeesInfoTableAdapter.Update(Me.DsEmployee.EmployeesChild1)

Using MS Access 2003 btw..
 
Last edited:
do this, otherwise youll encounter problems I mentioned before near the text "wise move"

VB.NET:
Private Sub btnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNew.Click
        
Me.DsEmployee.EmployeesInfo.EmployeeIDColumn.DefaultValue = maxIdValue + 1
Me.EmployeesInfoBindingSource.AddNew()
Me.EmployeesInfoBindingSource.EndEdit()
Me.EmployeesChild1BindingSource.AddNew()
Me.EmployeesChild1BindingSource.EndEdit()

Also, if you encounter errors, break after you AddNew and investigate the contents of the BindingSource.List : those are the values that will be sent into the table.
 
i tested, b4 my app updates the access db i did

BindingSource.List.count = the correct number

so my dataset was updated,

however for some reason when it updates, the access database throws me an error.. any ideas?

i fixed it zzz i deleted my dataset and reloaded it, i guess my dataset was corrupted =s
 
Last edited:
Back
Top