Adding new row to SQL

  • Thread starter Thread starter Trev
  • Start date Start date
T

Trev

Hey,

I have a working front end to a northwind database but
when i add a row, i add the productID field as null so
when i try and update it, i get an error, but if i click
add then then close the program and then start it again
it will give it a unique number (next in the sequence).

How can i get it to automatically add a unique number
(next in sequence) when i click on add.

i have this code for the productID text.

oDR("ProductID") = txtID.Text = "0"

Can i just add a refresh command somewhere?
If so what format do i use.

Thanks
 
Yeha that is sorta what i need but i cant seem to
understand where to add that to my already built program?

It will give it an auto number once i close it and open
it (refresh), so i thought i could use some sort of
refresh command to refresh the data on the form again?

Does anyone know how to do this?

I have the following for the DataAdd() Procedure:

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
Dim strID As String

' Create New DataRow Object From DataSet
oDR = moDS.Tables("Products").NewRow()
oDR.BeginEdit()

' Load new data into row
oDR("ProductID") = txtID.Text = "100000"
oDR("ProductName") = txtName.Text = "New"
oDR("SupplierID") = CType
(cboSupplier.SelectedItem, _
PDSAListItemNumeric).ID
oDR("CategoryID") = CType
(cboCategory.SelectedItem, _
PDSAListItemNumeric).ID
oDR("QuantityPerUnit") = cboSupplier.Text.Empty
oDR("UnitPrice") = CDec(txtPrice.Text.Empty = "0")
oDR("UnitsInStock") = CShort
(txtInStock.Text.Empty = "0")
oDR("UnitsOnOrder") = CShort
(txtOnOrder.Text.Empty = "0")
oDR("ReorderLevel") = CShort
(txtReOrder.Text.Empty = "0")
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
 
I just add a new feature to the Add button.

I added to load the form again when i click on it.

And it comes up with the correct autonumber that it is
supposed to. so is there a way i can use a refresh
command or do i need to load the form again like i just
did and hide or close the previous form.

Thanks
 
Back
Top