Link to home
Start Free TrialLog in
Avatar of G Scott
G ScottFlag for United States of America

asked on

Save Datagrid Updates To SQL

Ok, I have spent about 2 whole days on this and I cannot get this to work. I have a table in a database with the following schema where ID is an identity column:
User generated image
I have a datagrid in a WPF window that I fill doing this:

    Private conn As SqlConnection
    Dim dt As New DataTable("Employee")
    Private adapter As SqlDataAdapter
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        Dim sda As SqlDataAdapter
        Dim CmdString As String = String.Empty
        Using con As New SqlConnection(sqlconnXML)
            CmdString = "SELECT ID, fullname, Title, Cell, Carrier From Contacts"
            Dim cmd As New SqlCommand(CmdString, con)
            adapter = New SqlDataAdapter(cmd)
            adapter.Fill(dt)
            DataGrid1.ItemsSource = dt.DefaultView
            DataGrid1.DataContext = dt

        End Using
        ' Add any initialization after the InitializeComponent() call.
        DataGrid1.SelectedItem = Nothing
    End Sub

Open in new window


I have a button in said window that I would like to use to save any changes/additions/deletions to this datagrid back to my SQL database.  I am at this point now (after trying 100 other things) and this gives me an error "The ConnectionString property has not been initialized."

 Using con As New SqlConnection(sqlconnXML)
            Dim myBuilder As SqlCommandBuilder = New SqlCommandBuilder(adapter)

            myBuilder.GetUpdateCommand()

            adapter.UpdateCommand = myBuilder.GetUpdateCommand()

            adapter.Update(dt)
        End Using

Open in new window


How can I get this to work?  I did see some stuff here on EE about Entity Framework (TheLearnedOne suggested), however, it seemed like that may complicate things for me.  I'd like to keep this simple for both me and the deployment of this application.  Thanks for any help on this.
ASKER CERTIFIED SOLUTION
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G Scott

ASKER

Isn't that an INSERT query.  I am looking to do an update.  For instance, they may just change a person's title in the DB.  I don't want to Insert a new record for this as I will have numerous records for the same person.  I just want to run an update.
Avatar of G Scott

ASKER

This didn't work and it has been months so I just want to close this question to get it off my 'open questions' list.  Thanks anyway.