Trouble with Dataset

  • Thread starter Thread starter al
  • Start date Start date
A

al

Hi,

I'm having some difficulties updating a Dataset. The problem is the
Dataset does not get updated when I modify a bound textbox. Although
I see the changes whene I move next or previous in the form, however,
when I check the HasChanges flage of Dataset, it returns False. This
clearly causes no update to the DB.

code:

'Declaration

Dim stgr1 As String = ("data source=pc1; initial
catalog=northwind;integrated_ security=sspi")

Dim cn As SqlConnection = New SqlConnection(stgr1)
Dim cmd As New SqlCommand
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim pl As Long
Dim str As String


'Load data to client and binde textbox to dataset thorough txtbox
databinding

Private Sub Customers_Load(ByVal sender As System.Object, ByVal e
As_ System.EventArgs) Handles MyBase.Load

cn.Open()
cmd = cn.CreateCommand
cmd.CommandText = ("select * from customers")
da.SelectCommand = cmd
Try
da.Fill(ds, "customers")
TextBox1.DataBindings.Add("text", ds.Tables_("customers"),
"customerid")
TextBox2.DataBindings.Add("text", ds.Tables_("customers"),
"companyname")

Catch ex As Exception
MsgBox("" & ex.Message)
End Try
End Sub

'sub for updating dataset after user changes fields in form

Private Sub UpdateDS_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click

Dim prm As New SqlParameter

cmd = cn.CreateCommand
cmd.CommandText = "update customers set customerid=@customerid
where_ companyname =@companyname"

prm = cmd.Parameters.Add("@customerid", SqlDbType.NVarChar,_
10, "customerid")
prm = cmd.Parameters.Add("@companyrname", SqlDbType.NVarChar,_
40, "companyname")
prm.SourceVersion = DataRowVersion.Original
ds.AcceptChanges()
da.UpdateCommand = cmd


End Sub


'update database
Private Sub UpdateDB_Click(ByVal sender As System.Object, ByVal e
As_ System.EventArgs) Handles Button3.Click

da.Update(ds, "customers")

End Sub
End Class
 
Try calling EndEdit on the row as you navigate away to the next row.

--
____________________________________
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
William \(Bill\) Vaughn said:
Try calling EndEdit on the row as you navigate away to the next row.

--
____________________________________
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Thank you, it worked like a charm!

Grawsha, Al
 
make sure you are calling endcurrentedit. This is done for you
automatically when you move off the record.
 
Back
Top