Updating database tables

syed

New member
Joined
Jan 17, 2005
Messages
1
Programming Experience
Beginner
hello everybody,
i am new to vb.net. can anybody give me the code to add the database. Means, if i enter name and age in the form and click Submit button, the name and age should be updated in the respective table.

Thanks in advance.
 
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)

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

' Listview update not yet done

End If

NewTable.AcceptChanges()

VButs(
True)

End If

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("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.Integer, 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

XRow.BeginEdit()

Equate(SaveRow, 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

MyAdpUser.Update(MyDstUser, "UserTable")

MsgBox("Update Complete")

End Sub

just try to trace through these lines... its kinda hard to explain.. but this works in both adding and editing...
 
syed said:
hello everybody,
i am new to vb.net. can anybody give me the code to add the database. Means, if i enter name and age in the form and click Submit button, the name and age should be updated in the respective table.

Thanks in advance.

soundz like my boss...

stupid boss.;)

you must do your own...and we just help for modifying on what you would like to happend.
 
:cool: :rolleyes: hehehe.... dont worry mzim... its really their loss not ours... we help if we can...
we ask for it if we cant... :D
 

Latest posts

Back
Top