Connecting Tech Pros Worldwide Forums | Help | Site Map

How to update datatable

Newbie
 
Join Date: Jun 2009
Posts: 2
#1: Jun 12 '09
Hi All!
I want to update a record in my database but without using "update mytblname" query.
I am using following code
Expand|Select|Wrap|Line Numbers
  1. sql = "select * from customer where id = " & Val(Me.id.Text)
  2.  
  3.  
  4.         Dim adapter As New SqlDataAdapter(sql, conn)
  5.         Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)
  6.         Dim ds1 As New DataSet("customer")
  7.         adapter.Fill(ds1, "customer")
  8.         Dim T As DataTable = ds1.Tables("customer")
  9.         Dim i As Integer
  10.         Dim row As DataRow
  11.         If T.Rows.Count = 0 Then
  12.             row = T.NewRow
  13.         Else
  14.             T.Rows(0).BeginEdit()
  15.             row = T.Rows(0)
  16.             row.BeginEdit()
  17.         End If
  18.  
  19.         row("cname") = cname.Text
  20.         If addre.Text = "" Then addre.Text = "-"
  21.         row("address") = Me.addre.Text
  22.         If email.Text = "" Then email.Text = "-"
  23.         row("email") = email.Text
  24.         If ph.Text <> "" Then row("ph") = ph.Text
  25.         If cont.Text <> "" Then row("cont") = cont.Text
  26.         If other.Text <> "" Then row("other") = other.Text
  27.         If desi.Text <> "" Then row("desi") = desi.Text
  28.         row("zone") = Me.cmbzone.Text
  29.         If Val(Me.id.Text) = 0 Then
  30.             T.Rows.Add(row)
  31.             '  Else
  32.         Else
  33.             T.Rows(0).EndEdit()
  34.             row.EndEdit()
  35.         End If
  36.         adapter.Update(ds1, "customer")
  37.         adapter.Dispose()Its giving me error.
  38.  
WHat is problem with above code ?
Data is not getting updated to database/

Familiar Sight
 
Join Date: Apr 2008
Posts: 160
#2: Jun 12 '09

re: How to update datatable


Quote:

Originally Posted by ashukla View Post

Hi All!
I want to update a record in my database but without using "update mytblname" query.
I am using following code

Expand|Select|Wrap|Line Numbers
  1. sql = "select * from customer where id = " & Val(Me.id.Text)
  2.  
  3.  
  4.         Dim adapter As New SqlDataAdapter(sql, conn)
  5.         Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)
  6.         Dim ds1 As New DataSet("customer")
  7.         adapter.Fill(ds1, "customer")
  8.         Dim T As DataTable = ds1.Tables("customer")
  9.         Dim i As Integer
  10.         Dim row As DataRow
  11.         If T.Rows.Count = 0 Then
  12.             row = T.NewRow
  13.         Else
  14.             T.Rows(0).BeginEdit()
  15.             row = T.Rows(0)
  16.             row.BeginEdit()
  17.         End If
  18.  
  19.         row("cname") = cname.Text
  20.         If addre.Text = "" Then addre.Text = "-"
  21.         row("address") = Me.addre.Text
  22.         If email.Text = "" Then email.Text = "-"
  23.         row("email") = email.Text
  24.         If ph.Text <> "" Then row("ph") = ph.Text
  25.         If cont.Text <> "" Then row("cont") = cont.Text
  26.         If other.Text <> "" Then row("other") = other.Text
  27.         If desi.Text <> "" Then row("desi") = desi.Text
  28.         row("zone") = Me.cmbzone.Text
  29.         If Val(Me.id.Text) = 0 Then
  30.             T.Rows.Add(row)
  31.             '  Else
  32.         Else
  33.             T.Rows(0).EndEdit()
  34.             row.EndEdit()
  35.         End If
  36.         adapter.Update(ds1, "customer")
  37.         adapter.Dispose()Its giving me error.
  38.  
WHat is problem with above code ?
Data is not getting updated to database/

Hi ,

As per my understanding you r going in the worng direction..

You wann to update the database Customer table from the table which is there in the ds1 dataset... Wright ?

If this is the case ..you need to use the ExecuteNonQuery() function of the DataAdapter..

Just have a look of Dataadapter.ExecuteNonQuery() and get your task done..

Please let me know about any clarification..

Thanks!
Newbie
 
Join Date: Jun 2009
Posts: 2
#3: Jun 12 '09

re: How to update datatable


Thanks for reply.
But i dont want to use "Update table .." query.
Hence i am using above code.
Familiar Sight
 
Join Date: Apr 2008
Posts: 160
#4: Jun 12 '09

re: How to update datatable


Quote:

Originally Posted by ashukla View Post

Thanks for reply.
But i dont want to use "Update table .." query.
Hence i am using above code.

I think you have performance issue with using "Update table.." query..

Okay fine.. Just perform the ds1.AcceptChanges() before calling the adapter.Update() function.

Please let me know for the qury if any.

Thanks!
Reply