the insert is working...

cjaymcmeans

Well-known member
Joined
Jan 12, 2005
Messages
85
Programming Experience
3-5
Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click

Dim Nrow As DataRow

Dim Listctr, i As Integer

Dim NameStr As String

If CheckText() <> False Then

If AddFlag = True Then

Listctr = lvw1.Items.Count

Nrow = NewTable.NewRow()

MyClassObj.SaveRecords(Nrow, Trim(txtid.Text), Trim(txtfname.Text), Trim(txtlname.Text),
CDate(txtdate.Text), True)

lvw1.Items.Add(txtid.Text)

NameStr = Trim(txtfname.Text.ToString) & " " & Trim(txtlname.Text.ToString)

lvw1.Items(Listctr).SubItems.Add(NameStr)

lvw1.Items(Listctr).SubItems.Add(Format(
CDate(txtdate.Text), "MM/dd/yyyy"))

Else

Nrow = NewTable.Select("userid='" & Trim(txtid.Text) & "'")(0)

Nrow.BeginEdit()

MyClassObj.SaveRecords(Nrow, Trim(txtid.Text), Trim(txtfname.Text), Trim(txtlname.Text),
CDate(txtdate.Text), False)

Nrow.EndEdit()

' Listview update not yet done

For i = 0 To lvw1.Items.Count - 1

If lvw1.Items(i).Text = Trim(txtid.Text) Then

NameStr = Trim(txtfname.Text.ToString) & " " & Trim(txtlname.Text.ToString)

lvw1.Items(i).SubItems(1).Text = (NameStr)

lvw1.Items(i).SubItems(2).Text = (Format(
CDate(txtdate.Text), "MM/dd/yyyy"))

Exit For

End If

Next i

End If

NewTable.AcceptChanges()

VButs(
True)

End If

End Sub

Public Sub Equate(ByVal Xrow As DataRow, ByVal Id As String, ByVal fname As String, ByVal lname As String, ByVal DOB As Date, ByVal Ttype As Boolean)

If Ttype Then

Xrow("userid") = Id

Xrow("userfname") = fname

Xrow("userlname") = lname

Xrow("Userdate") = Format(
CDate(DOB), "MM/dd/yyyy")

Else

' Xrow("userid") = Xrow("userid")

Xrow("userfname") = fname

Xrow("userlname") = lname

Xrow("Userdate") = Format(
CDate(DOB), "MM/dd/yyyy")

End If

End Sub

Function SaveComm(ByVal strSql As String) As OleDbCommand

Dim comm As New OleDbCommand(strSql, XConn)

comm.Parameters.Add("@UId", OleDbType.Char, 10, "userid")

comm.Parameters.Add("@FName", OleDbType.Char, 50, "userfname")

comm.Parameters.Add("@LName", OleDbType.Char, 50, "userlname")

comm.Parameters.Add("@DOB", OleDbType.DBTimeStamp, 8, "userdate")

Return comm

End Function

Public Sub SaveRecords(ByVal XRow As DataRow, ByVal Id As String, ByVal fname As String, ByVal lname As String, ByVal DOB As Date, ByVal Ttype As Boolean)

Dim SaveRow As DataRow

If Ttype = True Then

SaveRow = XRow

Equate(SaveRow, Id, fname, lname, DOB,
True)

MyDstUser.Tables("UserTable").Rows.Add(SaveRow)

Dim MySaveCom As OleDbCommand = SaveComm("insert into users (userid, userfname, userlname, userdate) values " & _

"(@UId, @FName, @LName, @DOB)")

MyAdpUser.InsertCommand = MySaveCom

Else

SaveRow = XRow

Equate(SaveRow, Id, fname, lname, DOB,
False)

Dim MyEditCom As OleDbCommand = SaveComm("update users set userfname=@FName, userlname=@LName, userdate=@DOB " & _

"where userid=@UId")

MyAdpUser.UpdateCommand = MyEditCom

End If

Try

MyAdpUser.Update(MyDstUser, "UserTable")

MsgBox("Update Complete")

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub


the insert works... but the update doesnt... the insert works in both the dataset and the database but the update isnt...
can you guys help me out with this..

 
Hi,
Just Remove/comment the Following line in ur cmdsave_Click Handler.............


Nrow = NewTable.Select("userid='" & Trim(txtid.Text) & "'")(0)

Nrow.BeginEdit()

MyClassObj.SaveRecords(Nrow, Trim(txtid.Text), Trim(txtfname.Text), Trim(txtlname.Text), CDate(txtdate.Text), False)

Nrow.EndEdit()



As u r trying to save Data Before calling EndEdit that means still the Rowstate of ur Row is not Mark as Modified hence there is no record to Update.............

I hope this will Help u............................


Regards,
Ritesh



 
already did that a while ago...

i did this.. but an error keeps coming out..

Public Sub SaveRecords(ByVal XRow As DataRow, ByVal Id As String, ByVal fname As String, ByVal lname As String, ByVal DOB As Date, ByVal Ttype As Boolean)

Dim SaveRow As DataRow

If Ttype = True Then

SaveRow = XRow

Equate(SaveRow, Id, fname, lname, DOB,
True)

MyDstUser.Tables("UserTable").Rows.Add(SaveRow)

Dim MySaveCom As OleDbCommand = SaveComm("insert into users (userid, userfname, userlname, userdate) values " & _

"(@UId, @FName, @LName, @DOB)")

MyAdpUser.InsertCommand = MySaveCom

Else

XRow.BeginEdit()

Equate(XRow, Id, fname, lname, DOB,
False)

XRow.EndEdit()

Dim MyEditCom As OleDbCommand = SaveComm("update users set userfname=@FName, userlname=@LName, userdate=@DOB " & _

"where userid=@UId")

MyAdpUser.UpdateCommand = MyEditCom

End If

Try

MyAdpUser.Update(MyDstUser, "UserTable")

MsgBox("Update Complete")

'If Ttype = False Then

' XRow.EndEdit()

'End If

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

if i place the datarow.endedit before the dataadapter.update an error occurs.. if i place it after it saves only in the dataset., but not in the database...

 
Back
Top