Adding dataset row to database

S

s cartner

Iv got a simple form on my pda which i use to add new records to the
dataset, the form contains combo boxes and text fields, no bound objects at
all. I can add new records to that dataset no problem. After i add a record
to the dataset, i want to add the new row/record to my SQL Server CE
database. i can add the first record no problem, its when i try to add
another record im getting an exception:Update error. From what i understand,
my code should add a new row to the database and NOT update it. The code is
below. *Note* everytime i add a record to the dataset, i insert it into the
database.

Thanks for your help

Dim NewRow As DataRow

NewRow = dsFindingsSub.Tables(0).NewRow
With NewRow
.Item("InsNo") = mlngInsNo
.Item("TreeID") = mlngTreeID
.Item("Finding") = Me.cboFindings.Text
.Item("Comments") = Me.txtComments.Text
.Item("Priority") = Me.cboPriorities.Text
End With

dsFindingsSub.Tables(0).Rows.Add(NewRow)
'dsFindingsSub.AcceptChanges()
Me.BindingContext(Me.dsFindingsSub.Tables(0)).Position =
dsFindingsSub.Tables(0).Rows.Count
Dim sqlFindingsSubAddCommand As New SqlServerCe.SqlCeCommand
Dim strFinding As String
Dim strComments As String
Dim strPriority As String
Dim lngInsNo As Long
Dim lngTreeID As Long
Dim strInsertSQL As String

lngInsNo = mlngInsNo
lngTreeID = mlngTreeID
strFinding = cboFindings.Text
strComments = txtComments.Text
strPriority = cboPriorities.Text

strInsertSQL = "INSERT INTO zsExpInspSub(InsNo, TreeID, Finding,
Comments, Priority) VALUES ("
strInsertSQL = strInsertSQL & "'" & lngInsNo & "', "
strInsertSQL = strInsertSQL & "'" & lngTreeID & "', "
strInsertSQL = strInsertSQL & "'" & strFinding & "', "
strInsertSQL = strInsertSQL & "'" & strComments & "', "
strInsertSQL = strInsertSQL & "'" & strPriority & "' "
strInsertSQL = strInsertSQL & ")"

sqlFindingsSubAddCommand = ssceconn.CreateCommand()
sqlFindingsSubAddCommand.CommandText = strInsertSQL
daFindingssub.InsertCommand = sqlFindingsSubAddCommand
daFindingssub.Update(dsFindingsSub, "zsExpInspSub")
 
E

Earl

Is NewRow a reserved word?

In any event, what happens if any of your insert values are null? Set a
breakpoint and check all of your values. What I do is protect those input
values with something like:

Dim drA as DataRow

If Not (CStr(Me.cboFindings.Text) = Nothing) Then
drA("Finding") = Me.cboFindings.Text
Else
drA("Finding") = DBNull.Value
End If
 

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