Results 1 to 16 of 16

Thread: [2005] Update table question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    [2005] Update table question

    Hello,
    I am developing a small app that connects to Informix databases, and allows the user to type an SQL statement into a TextBox, and display the results in a DataGridView. This is fine.
    However, what I want the user to be able to do is update individual cells in the grid, press update, and update the underlying table.
    Hard coding specific update commands is ok, but if the user enters a command in the TextBox along the lines of ....

    SELECT name, address FROM table WHERE name = "Joe"

    ... how would I get it write the appropriate changes to 'table'?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    Create a DataAdapter with the query string and then use a CommandBuilder to automatically generate the non-query commands. You will need to set the MissingSchemaAction property of the adapter to AddWithKey.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    Thanks for your reply.
    Unfortunately, I keep getting, "Dynamic SQL generation is not supported against a SelectCommand that does not return any base table information." on my line:
    Code:
    myDataAdapter.Update(ds, "Table")
    I'm assuming that I've got a problem with the order in which I'm putting things together, but I can't figure out where the problem is:
    Code:
                myCommand.CommandText = Me.txtSQLStatement.Text
    
                myDataAdapter = New OleDbDataAdapter(myCommand.CommandText, conn)
    
                myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
                myCommandBuilder = New OleDbCommandBuilder(myDataAdapter)
    
                myDataAdapter.Fill(ds, "Table")
    I believe that I need to run the MissingSchemaAction line before the Fill?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    What does your query look like?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    Select * From Comp
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    That seems very odd. There is also a peculiarity in your code. You're creating a Command, setting its CommandText and then creating a DataAdapter without using the Command. What is that Command for? Either use it as the SelectCommand of the DataAdapter or get rid of it altogether. Fix that and then see if you still get the same error.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    Unfortunately, I get the same problem either way.

    The code currently looks like:
    Code:
    myCommand.Connection = conn
    myCommand.CommandText = Me.txtSQLStatement.Text
    
    myDataAdapter.SelectCommand = myCommand
    
    myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
    'myDataAdapter = New OleDbDataAdapter(Me.txtSQLStatement.Text, conn)
    
    myCommandBuilder = New OleDbCommandBuilder(myDataAdapter)
    
    myDataAdapter.Fill(ds, "Table")
    But I get the same problem if the code is:
    Code:
    'myCommand.Connection = conn
    'myCommand.CommandText = Me.txtSQLStatement.Text
    
    'myDataAdapter.SelectCommand = myCommand
    
    myDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
    myDataAdapter = New OleDbDataAdapter(Me.txtSQLStatement.Text, conn)
    
    myCommandBuilder = New OleDbCommandBuilder(myDataAdapter)
    
    myDataAdapter.Fill(ds, "Table")
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    Is the SelectCommand.CommandText of your DataAdapter definitely a valid query when you call the Update method?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    Yes, it seems to be:
    Code:
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    
        MsgBox("'" & myDataAdapter.SelectCommand.CommandText.ToString & "'")
    
        myDataAdapter.Update(ds, "Table")
    
        MsgBox("Done")
    
    End Sub
    Displays, "'SELECT * FROM COMP'", before it throws the error.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    And this is definitely the same DataAdapter object you called Fill on previously?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    Yes - its the same adapter - though it was using the same name as one used earlier. So, just to be sure, I created a new one - myGridViewDataAdapter - and I still get the same error.

    Beginning to wonder if this is something to do with Informix?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Update table question

    Quote Originally Posted by penguin5000
    Beginning to wonder if this is something to do with Informix?
    After reading through this thread, if you have followed jmcilhinney's suggestions, and it seems you have, but it is still blowing up, then that would be my guess.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    I've re-written part of this to use the IBM.Data.Informix namespace, and the error message has changed to, "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information" when it hits this line:
    Code:
    myGridViewDataAdapter.Update(myDataSet, "Table")
    in the btnUpdate_Click event.

    The code used to fill the DataSet is now:
    Code:
            myIfxConnection = New IfxConnection("Database=" & myDatabase & _
                                                ";Server=" & myServer & _
                                                ";User ID=username;Password=password")
    
            myGridViewDataAdapter = New IfxDataAdapter
    
            myIfxCommand = New IfxCommand(strSQLCommand, myIfxConnection)
    
            myGridViewDataAdapter.SelectCommand = myIfxCommand
    
            myIfxBuilder = New IfxCommandBuilder(myGridViewDataAdapter)
    
            myGridViewDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
            myDataSet = New DataSet
    
            myIfxConnection.Open()
    
            myGridViewDataAdapter.Fill(myDataSet, "Table")
    
            myIfxConnection.Close()
    Any advice is really appreciated.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    So the obvious question is "does your SelectCommand return any key column information?". If your table has a primary key, that column (or columns) is returned by the query and you've set the MissingSchemaAction property to AddWithKey then the CommandBuilder should do its thing. If all those things are true and it still doesn't work then you're just going to have to build the non-query commands yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Update table question

    build the non-query commands yourself
    Gulp!

    Unfortunately, I didn't build the database - so I'll have to dig deeper to see if there are any primary keys. Informix is horrible (IMO)!

    I have noticed that there are certain hidden columns with Informix databases - one of which is called 'RowID'. This is unique, and seems to be something that Informix creates for itself (?).

    Do you know of a way of assigning a primary key AFTER the Fill? I could probably code it all in such a way that 'RowID' is always collected.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Update table question

    You can assign to the DataTable's PrimaryKey property once the columns have been added. I don't know that that will help with the CommandBuilder though because it doesn't know anything about the DataTable. The CommandBuilder only knows about the DataAdapter. Maybe the DataTable schema will be used when you call Update on the adapter, maybe not. Only one way to find out.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width