?
Solved

vb.net Update database using dataAdapter failing, "Dynamic SQL generation UpdateCommand is not supported..."

Posted on 2004-09-20
6
Medium Priority
?
8,852 Views
Last Modified: 2012-08-14
Hi experts,

I have a datagrid that displays the contents of a database table via a dataAdapter.  I am under the impression that ADO.NET in disconnected mode allows dynamically changing the datagrid contents and writing it back to the original database via the dataAdapter....  However, I keep getting an error: "Dynamic SQL generation UpdateCommand is not supported against a SelectCommand that does not return key column information".

Could someone please take a look at my code and let me know where I went astray?  The initial datagrid view works fine.  Clicking the "SaveChanges" button illicits the correct message box as well, which means the tables and temporary tables are being accessed properly as well....

"dgCustomers" is the DataGrid on the Windows Form.

Windows Form variables:

   Dim daDataTransfer As New OleDbDataAdapter
   Dim dtCustomerTemp As New DataTable

OnLoad():

  Try
            Dim cmdCommand As New OleDbCommand
            Dim conConnect As New OleDbConnection

            conConnect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source = " & System.AppDomain.CurrentDomain.BaseDirectory & "\dbOrders.mdb"

            conConnect.Open()

            cmdCommand.Connection = conConnect
            cmdCommand.CommandText = "SELECT CustomerInfo.FirstName, CustomerInfo.LastName, CustomerInfo.TelephoneNo,FROM CustomerInfo;"

            daDataTransfer.SelectCommand = cmdCommand
            daDataTransfer.Fill(dtCustomerTemp)

            dtCustomerTemp.DefaultView.AllowNew = False
            Me.dgCustomers.DataSource = dtCustomerTemp
            Me.btnSave.Visible = True

        Catch ex As Exception
            MsgBox("Unable to create DataGrid view.  ERROR: " & ex.ToString, MsgBoxStyle.Information)
        End Try


On Clicking the SaveChanges button():

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim dtChanged As DataTable
        Dim comBuild As OleDbCommandBuilder
        Dim drRows As Integer

        'check to see if there were any changes made
        dtChanged = dtCustomerTemp.GetChanges()

        If Not IsNothing(dtChanged) Then
            Try
                comBuild = New OleDbCommandBuilder(daDataTransfer)
                drRows = daDataTransfer.Update(dtChanged)
                MsgBox("Updates successful.", MsgBoxStyle.Information)
            Catch ex As Exception
                MsgBox("Update ERROR:  " & ex.Message, MsgBoxStyle.Exclamation)
            End Try
        Else
            MsgBox("There was nothing to update.", MsgBoxStyle.Information)
        End If

    End Sub

Thanks!
0
Comment
Question by:Larry_T
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12105949
cmdCommand.CommandText = "SELECT CustomerInfo.FirstName, CustomerInfo.LastName, CustomerInfo.TelephoneNo,FROM CustomerInfo;"


change the select command to contain the primary key ...liek customer Id.

good luck
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12105995
Hi Larry_T,
are you sure that this line is executed : SELECT CustomerInfo.FirstName, CustomerInfo.LastName, CustomerInfo.TelephoneNo,FROM CustomerInfo; ?
i think it should be:

SELECT CustomerInfo.FirstName, CustomerInfo.LastName, CustomerInfo.TelephoneNo FROM CustomerInfo;

HTH
B..M
0
 
LVL 1

Author Comment

by:Larry_T
ID: 12106053
SELECT CustomerInfo.FirstName, CustomerInfo.LastName, CustomerInfo.TelephoneNo FROM CustomerInfo;

Sorry that was a typo on my behalf....  I actually took out a few of the fields for the SQL statement.

I know the SQL statement works, because the datagrid is displaying the information properly.  It's in the dataAdapter Command where I think the problem is- it crashes is there's any changes.

Praneetha, how would I change the SELECT statement to include the Primary Key information?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 16

Accepted Solution

by:
RobertRFreeman earned 800 total points
ID: 12106055
Unless you include a primary key column in your initial select, you must manually enter your insert, update, and delete commands for your adapter.

I prefer to do this through the Visual Studio GUI.  Just right click on your data adapter, then select Configure Data Adapter.

For instructions on using the GUI to add an adapter go here.
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21133673.html

For visuals, see this site:
http://www.c-sharpcorner.com/Tutorials/OleDbDataAdapterDesign.asp

You can also do it manually through the oleDbAdapter.InsertCommand .UpdateCommand and .InsertCommand properties.
0
 
LVL 1

Author Comment

by:Larry_T
ID: 12106073
>>You can also do it manually through the oleDbAdapter.InsertCommand .UpdateCommand and .InsertCommand properties.

I forgot to mention that I am using OLEDB.  I've read many articles so far but none of the implementations seem to work properly....

any suggestions?  :)
0
 
LVL 1

Author Comment

by:Larry_T
ID: 12106196
Thanks- I got it.

I forgot to include the Table Column that had the Primary key... because I didn't want the viewer to be able to overwrite it.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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