Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Set Creating INCORRECT ID (Primary Key) Field

Status
Not open for further replies.

NipsMG

Programmer
Dec 27, 2000
215
US
HELP!

I've got a DataSet that points to to a table - BatchInfo - that stores information about batches of items we've shipped. There's an identity column BatchID.

The detail table is called BatchData, and also has a BatchID column, the value of which must exist in BatchInfo.

I've been able to create a DataSet by selecting * from BatchInfo, create a data adapter, and create/save rows.
However, after cancelling/deleting a row, adding a new one comes back with the SAME BatchID as the Deleted row, which is an invalid number. However, if I save that row to the database using a daUpdate, it writes the CORRECT ID number into the table?

Anyone have ANY ideas on this?

Code:
    'Create connection with specified connection string and open it
    sqlConnection = New SqlClient.SqlConnection(ConnectionString)
            sqlConnection.Open()

    'Create DataAdapter for DataTable for later use
    daData = New SqlClient.SqlDataAdapter("select * from " & _
    "NET001_Data", sqlConnection)
    daData.FillSchema(dsData, SchemaType.Source, "NET001_Data")
    daData.Fill(dsData, "NET001_Data")

    'Create Data Adapter and Read In Data and Schema info 
    daBatchInfo = New SqlClient.SqlDataAdapter("select * from " & _
    "NET001_BatchInfo", sqlConnection)
    daBatchInfo.FillSchema(dsBatchInfo, SchemaType.Source, "NET001_BatchInfo")
    daBatchInfo.Fill(dsBatchInfo, "NET001_BatchInfo")


    Dim strInsertCommand As String = _
    "Insert Into NET001_BatchInfo(BatchUser, BatchStart, BatchType) values(" & _
    "@BatchUser, @BatchStart, @BatchType)"

    daBatchInfo.InsertCommand = New SqlClient.SqlCommand(strInsertCommand, sqlConnection)

    'Set up parameters in sqlcommand object For Insert Command 
    Dim param As SqlClient.SqlParameter

    param = daBatchInfo.InsertCommand.Parameters.Add( _
        New SqlClient.SqlParameter("@BatchUser", SqlDbType.NVarChar, 30))
    param.SourceColumn = "BatchUser"
    param.SourceVersion = DataRowVersion.Current

    param = daBatchInfo.InsertCommand.Parameters.Add( _
                New SqlClient.SqlParameter("@BatchStart", SqlDbType.DateTime))
    param.SourceColumn = "BatchStart"
    param.SourceVersion = DataRowVersion.Current

    param = daBatchInfo.InsertCommand.Parameters.Add( _
        New SqlClient.SqlParameter("@BatchType", SqlDbType.NVarChar, 10))
    param.SourceColumn = "BatchType"
    param.SourceVersion = DataRowVersion.Current


    'Set Up Parameters in Batch Info Command Object for Update command 
    Dim strUpdateCommand As String = "Update NET001_BatchInfo Set " & _
        "BatchQty = @BatchQty where BatchID = @BatchID"

    'Create Update Command object
    daBatchInfo.UpdateCommand = New SqlClient.SqlCommand(strUpdateCommand, sqlConnection)

    param = daBatchInfo.UpdateCommand.Parameters.Add( _
        New SqlClient.SqlParameter("@BatchQty", SqlDbType.Int))
    param.SourceColumn = "BatchQty"
    param.SourceVersion = DataRowVersion.Current

    param = daBatchInfo.UpdateCommand.Parameters.Add( _
                New SqlClient.SqlParameter("@BatchID", SqlDbType.Int))
    param.SourceColumn = "BatchID"
    param.SourceVersion = DataRowVersion.Current

    'Generate Delete Command And Set Up parameter
    Dim strDeleteCommand As String = "DELETE FROM NET001_BatchInfo WHERE " & _
        "BatchID = @BatchID"
    daBatchInfo.DeleteCommand = New SqlClient.SqlCommand(strDeleteCommand, sqlConnection)

    param = daBatchInfo.DeleteCommand.Parameters.Add( _
               New SqlClient.SqlParameter("@BatchID", SqlDbType.Int))
    param.SourceColumn = "BatchID"
    param.SourceVersion = DataRowVersion.Current

Dim drCurrent as DataRow
Dim dtTable as DataTable = dsBatchInfo.Tables("BatchInfo")



'****************************************************
'** AT THIS POINT, drCurrent("BatchID") returns 315
'****************************************************

drCurrent = dtTable.NewRow()
msgbox drCurrent("BatchID").ToString

daAdapter.Update(dsBatchInfo)
drCurrent.Delete
daAdapter.Update(dsBatchInfo)

'*******************************************************
'** drCurrent("BatchID") STILL returns 315?!?!
'*******************************************************
drCurrent = dtTable.NewRow()
msgbox drCurrent("BatchID").ToString


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top