Add Data to SQL

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
 
C

Cor

Hi Trevor,
Additional information: Column 'ProductID' does not allow
nulls."
I think that the reason for the error is given, you have made your database
in a way that the "ProductID" has to be given in a row, and I thought to see
in your code that you did not.

I hope this helps a 1/8 byte?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top