[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Can't update database with SQL dataAdapter

Being new to database programming, I am trying my hand at using code (as opposed to dragging the graphical componants to the form).

I have a simple database on SQL server 2005 express, connected to a Windows form. When trying to update changes I make to to the fields on the form using the sqldataAdapter update command the changes are not committed to the databse.
I use the following line of code to perform the update:


The connection, dataAdapter and dataset were created using the following code:
        Dim sConnectionString As String = _
             "data source=TONI\SQLEXPRESS;" & "initial catalog=Murach;integrated security=SSPI;" & _
             "persist security info=false;workstation id=TONI;packet size=4096"
        Dim cnn As New SqlConnection(sConnectionString)
     The Select statement
    Dim sCustomersSelect = "Select * from Customers"
    Dim DataAdapterCustomers As New SqlDataAdapter(sCustomersSelect, cnn)
       Dim DataSetCustomers As New DataSet()  
I think I need to configure my dataAdapter, but I have no idea how to do that in code. Please help.
1 Solution
Provided your table has a primary key this should work, placed somewhere after you have given your dataadapter its Select statement.

   Dim cb As New SQLCommandBuilder(DataAdapterCustomers)

That will automatically configure the dataadapter with Update, Insert and Delete commands.  You can then just use the DataAdapterCustomers.Update method to get local changes back to the database.


tariqanis2001Author Commented:
Thank you

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now