Hello All,

I am writing my self a simple little address book application and I seem to be having some trouble with updating the dataset.

I have written the code and I cant see for any reason why it shouldnt work, but during run time, when I delete an entry and try to save the changes I get the following error.

Update failed dynamic sql generation for the delete command is not supported against a select command that does not return key column information.

I have never come accross this error before. Please see my code below.

Imports System.Data.SqlClient

Public Class AddAddress

    Dim conn As SqlConnection
    Dim daAddressBook As SqlDataAdapter
    Dim dsAddressBook As DataSet

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub AddAddress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=AddressBookdb;Integrated Security=True")
            dsAddressBook = New DataSet
            daAddressBook = New SqlDataAdapter("SELECT Name,HomeTellNumber,MobileTellNumber,BirthDay,Email,Address FROM Details", conn)
            Dim cmdBldr As SqlCommandBuilder = New SqlCommandBuilder(daAddressBook)
            daAddressBook.Fill(dsAddressBook, "AddressBook")
            DataGridView1.DataSource = dsAddressBook.Tables("AddressBook")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Try
            daAddressBook.Update(dsAddressBook, "AddressBook")
            MessageBox.Show("Address Book Updated")
        Catch ex As Exception
            MessageBox.Show("Update Failed" & ex.Message)
        End Try
    End Sub
End Class

Any help on what has gone wrong would be much apreciated.

Thanks

John

Recommended Answers

All 5 Replies

For delete you have to define primary key in your table, I think you dont have any primary key in your table and you have select that column as well

Try this, I think it will help you

For delete you have to define primary key in your table, I think you dont have any primary key in your table and you have select that column as well

Try this, I think it will help you

Hi,

Thanks for the reply, I added a primary key to my table already, I added it through the addressbookdataset.xsd file in my solutions explorer.

Any other thaughts welcome.

Thanks

did you added your primary key column in this line of code

daAddressBook = New SqlDataAdapter("SELECT Name,HomeTellNumber,MobileTellNumber,BirthDay,Email,Address FROM Details", conn)

i mean to say in select command

did you added your primary key column in this line of code

daAddressBook = New SqlDataAdapter("SELECT Name,HomeTellNumber,MobileTellNumber,BirthDay,Email,Address FROM Details", conn)

i mean to say in select command

No, the only way I know to add a primary key is through the dataset xsd file.

Would you mind telling me how to do it through code.

Thanks

John

Please send the structure of your table, indicating primary key field

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.