Saving DataGridView changes back to SQL database

Posted on 2011-10-20
Last Modified: 2012-05-12
Greetings experts,

I have a supposedly simple application that will enable end users to modify account contact information for our order acknowledgement email system.  I would like to simply present them with a grid that they can make changes to, and it saves after you leave the row a la Access.

I have the beginnings under way and am using a button to test my db save code, but I keep running into an error saying that I dont have a key value column defined.

"Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information."

Below is my code snipet, I tried searching through the previously asked questions here but was not able to find any help specific to my needs.  If anyone here can provide any insight I would be most grateful.  Thanks.

Private adapter As SqlDataAdapter
Private myDataSet As DataSet

Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load

        PBPPEconn = modPBPPurchasingEmailPODB.GetConnectString
        Dim myBindingSource As New BindingSource

        Dim strSQL As String = "SELECT * FROM PBPEDIByEmailList"
        Dim conn As New SqlConnection(PBPPEconn)
        Dim cmd As New SqlCommand(strSQL, conn)
        adapter = New SqlDataAdapter(cmd)
        myDataSet = New DataSet
        myBindingSource.DataSource = myDataSet.Tables(0).DefaultView

End Sub

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles cmdUpdate.Click

            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(Me.adapter)

        Catch ex As Exception
        End Try

End Sub

Open in new window

Question by:gwosgood
    LVL 83

    Accepted Solution

    As the error suggests, you need to have a primary key defined in the table so that the update command can use it in Where clause.
    LVL 2

    Author Comment

    I actually kind of feel like an idiot now.  I've been using a linked table in Access this whole time to test and see changes made to the db table.  Apparenty, even if you dont assign a key field in SQL, Access will mark the first column as "key".

    Lesson:  follow through and check the SQL db instead of assuming its setup properly.

    Thanks for your help.
    LVL 83

    Expert Comment

    Glad to help :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now