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:
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.

        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)
            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)


            adapter.UpdateCommand = myBuilder.GetUpdateCommand()

        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.
G ScottAsked:
Who is Participating?
SAMIR BHOGAYTAConnect With a Mentor Freelancer and IT ConsultantCommented:
you need to execute the command, not simply add to the DataAdapter
Also, as it coded now, you don't need the DataAdapter at all.

Dim connection As New Data.SqlClient.SqlConnection
Dim command As New Data.SqlClient.SqlCommand

connection.ConnectionString = "Server= server; Database= DB; integrated security=true"
command.CommandText = "INSERT INTO <table> (Col1, Col2, Col3, Col4) VALUES (@Name, @Property, @Value, @Date)"

command.Parameters.Add("@ServerName", SqlDbType.VarChar)
command.Parameters.Add("@Property", SqlDbType.VarChar)
command.Parameters.Add("@Value", SqlDbType.VarChar)
command.Parameters.Add("@CaptureDate", SqlDbType.DateTime)
command.Connection = connection

For i As Integer = 0 To DataGridView.Rows.Count - 1
    command.Parameters(0).Value = dgvServerConfig.Rows(i).Cells(0).Value
    command.Parameters(1).Value = dgvServerConfig.Rows(i).Cells(1).Value
    command.Parameters(2).Value = dgvServerConfig.Rows(i).Cells(2).Value
    command.Parameters(3).Value = dgvServerConfig.Rows(i).Cells(3).Value
G ScottAuthor Commented:
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.
G ScottAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.