T
Trevor
Hey i have the following code below and i get an error
back saying: "An unhandled exception of
type 'System.Data.NoNullAllowedException' occurred in
system.data.dll
Additional information: Column 'ProductID' does not allow
nulls."
I have the same sort of code for an update feature and
delete feature and both work just fine.
The Code:
Private Sub DataAdd()
Dim oAdapter As SqlClient.SqlDataAdapter
Dim oBuild As SqlClient.SqlCommandBuilder
Dim oDR As DataRow
Dim strSQL As String
Dim strConn As String
' Create New DataRow Object From DataSet
oDR = moDS.Tables("Products").NewRow()
oDR.BeginEdit()
' Load new data into row
oDR("ProductName") = txtName.Text
oDR("SupplierID") = CType
(cboSupplier.SelectedItem, _
PDSAListItemNumeric).ID
oDR("CategoryID") = CType
(cboCategory.SelectedItem, _
PDSAListItemNumeric).ID
oDR("QuantityPerUnit") = cboSupplier.Text
oDR("UnitPrice") = CDec(txtPrice.Text)
oDR("UnitsInStock") = CShort(txtInStock.Text)
oDR("UnitsOnOrder") = CShort(txtOnOrder.Text)
oDR("ReorderLevel") = CShort(txtReorder.Text)
oDR("Discontinued") = CBool(chkDisc.Checked)
' Tell DataRow you are done adding data
oDR.EndEdit()
' Add DataRow to DataSet
moDS.Tables("Products").Rows.Add(oDR)
Try
' Get Connection String
strConn = ConnectStringBuild()
' Build SQL String
strSQL = "SELECT * FROM Products "
' Create New DataAdapter
oAdapter = _
New SqlClient.SqlDataAdapter(strSQL, strConn)
' Create CommandBuilder for Adapter
' This will build INSERT, UPDATE and DELETE
SQL
oBuild = New SqlClient.SqlCommandBuilder
(oAdapter)
' Get Insert Command Object
oAdapter.InsertCommand =
oBuild.GetInsertCommand()
' Submit INSERT statement through Adapter
oAdapter.Update(moDS, "Products")
' Tell DataSet changes to data source are
complete
moDS.AcceptChanges()
' Reload the list box
ListLoad()
Catch oException As Exception
MessageBox.Show(oException.Message)
End Try
End Sub
back saying: "An unhandled exception of
type 'System.Data.NoNullAllowedException' occurred in
system.data.dll
Additional information: Column 'ProductID' does not allow
nulls."
I have the same sort of code for an update feature and
delete feature and both work just fine.
The Code:
Private Sub DataAdd()
Dim oAdapter As SqlClient.SqlDataAdapter
Dim oBuild As SqlClient.SqlCommandBuilder
Dim oDR As DataRow
Dim strSQL As String
Dim strConn As String
' Create New DataRow Object From DataSet
oDR = moDS.Tables("Products").NewRow()
oDR.BeginEdit()
' Load new data into row
oDR("ProductName") = txtName.Text
oDR("SupplierID") = CType
(cboSupplier.SelectedItem, _
PDSAListItemNumeric).ID
oDR("CategoryID") = CType
(cboCategory.SelectedItem, _
PDSAListItemNumeric).ID
oDR("QuantityPerUnit") = cboSupplier.Text
oDR("UnitPrice") = CDec(txtPrice.Text)
oDR("UnitsInStock") = CShort(txtInStock.Text)
oDR("UnitsOnOrder") = CShort(txtOnOrder.Text)
oDR("ReorderLevel") = CShort(txtReorder.Text)
oDR("Discontinued") = CBool(chkDisc.Checked)
' Tell DataRow you are done adding data
oDR.EndEdit()
' Add DataRow to DataSet
moDS.Tables("Products").Rows.Add(oDR)
Try
' Get Connection String
strConn = ConnectStringBuild()
' Build SQL String
strSQL = "SELECT * FROM Products "
' Create New DataAdapter
oAdapter = _
New SqlClient.SqlDataAdapter(strSQL, strConn)
' Create CommandBuilder for Adapter
' This will build INSERT, UPDATE and DELETE
SQL
oBuild = New SqlClient.SqlCommandBuilder
(oAdapter)
' Get Insert Command Object
oAdapter.InsertCommand =
oBuild.GetInsertCommand()
' Submit INSERT statement through Adapter
oAdapter.Update(moDS, "Products")
' Tell DataSet changes to data source are
complete
moDS.AcceptChanges()
' Reload the list box
ListLoad()
Catch oException As Exception
MessageBox.Show(oException.Message)
End Try
End Sub