Saving DataGridView changes back to SQL database

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
        adapter.Fill(myDataSet)
        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
        Try

            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(Me.adapter)
            cb.GetUpdateCommand()
      
            adapter.Update(myDataSet)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

End Sub

Open in new window

LVL 2
gwosgoodIT MgrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gwosgoodIT MgrAuthor Commented:
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.
0
CodeCruiserCommented:
Glad to help :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.