Update a new added row in VB.NET

A

Aliya

Hi all!
I'm writing the form in vb.net.
And the problem is that if I don't leave form after adding new row, I
can't update it in second time.
This is my code:

Public Class MemberDetails
Inherits System.Windows.Forms.Form
Dim ds As New System.Data.DataSet() 'DataSet With Tables for
combo
Dim CurrentCommitteeMemberID As Int32
Dim strAction As String
Dim dr As DataRow
Dim mf As New MemberFinder.MemberFinder()

Private Sub MemberDetails_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
GetDataSet()
End Sub

Private Sub GetDataSet()
Dim dsFinder As New System.Data.DataSet()
Dim i As Int32
Dim dt As DataTable
Dim HasRows As Boolean
Dim c As Control

dsFinder = mf.GetMemberData(CurrentCommitteeMemberID)
dt = dsFinder.Tables(0)
dt.DataSet.Tables.Remove(dt)
dt.TableName = "APP_CommitteeMember"
For i = 0 To ds.Tables.Count - 1
If ds.Tables(i).TableName = "APP_CommitteeMember" Then
ds.Tables.Remove("APP_CommitteeMember")
End If
Next i
ds.Tables.Add(dt)

HasRows = ds.Tables("APP_CommitteeMember").Rows.Count > 0
If HasRows = False Then
ds.Tables("APP_CommitteeMember").Rows.Add(ds.Tables("APP_CommitteeMember").NewRow)
End If
dr = ds.Tables("APP_CommitteeMember").Rows(0)

For Each c In Me.Controls
If c.DataBindings.Count > 0 Then
c.DataBindings.RemoveAt(0)
Next

IDTxt.DataBindings.Add("Text", ds,
"APP_CommitteeMember.IDNbr")
FirstNameTxt.DataBindings.Add("Text", ds,
"APP_CommitteeMember.FirstName")
LastNameTxt.DataBindings.Add("Text", ds,
"APP_CommitteeMember.LastName")
StatusCmb.DataBindings.Add("SelectedValue", ds,
"APP_CommitteeMember.CommitteeMemberStatus_ID")
AppointmentTxt.DataBindings.Add("Text", ds,
"APP_CommitteeMember.AppointmentDate")
RoleCmb.DataBindings.Add("SelectedValue", ds,
"APP_CommitteeMember.CommitteeMemberRole_ID")
NotesTxt.DataBindings.Add("Text", ds,
"APP_CommitteeMember.Notes")
End Sub

Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles SaveBtn.Click
Dim mf As New MemberHandler.MemberHandler()
Dim ErrID As Int32
Dim valData As New Diagnosis.DataSetValidator()
Dim ErrStr As String

ErrStr = valData.checkMemberdataset(ds, False)
If ErrStr = "" Then
dr.EndEdit()
If AppointmentTxt.Text <> "" Then AppointmentTxt.Text =
CType(AppointmentTxt.Text, DateTime).ToString("dd/MM/yyyy")
Else
MessageBox.Show(ErrStr, "Title", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Exit Sub
End If

Select Case strAction
Case "Add"
ErrID = mf.AddMember(ds)
Case "Update"
ErrID = mf.UpdateMember(ds)
Case "Cancel"
ErrID = mf.EndAppointment(ds)
End Select

If ErrID = 0 Then
MessageBox.Show("Save Error")
Else
If strAction = "Add" Then
strAction = "Update"
CurrentCommitteeMemberID = ErrID
GetDataSet()
End If
dr.AcceptChanges()
MessageBox.Show("Save OK")
End If
End Sub
End Class

Thank you for any answers, Aliya.
 
D

David Sceppa

Aliya,

It sounds like the database is generating values for new
rows, such as auto-increments, timestamps, defaults, etc. If you
do not re-fetch this data after submitting the new row, the
contents of your DataRow are out of synch with the contents of
the row in the database and subsequent attempts to submit changes
to the row will fail.

For examples on how to re-fetch such data, see "Retrieving
Identity or Autonumber Values" in the .NET Framework SDK. There
are examples using SQL Server and Access with C# and VB.NET.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 

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