Results 1 to 18 of 18

Thread: [02/03] Updating datasource from dataset

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Arrow [02/03] Updating datasource from dataset

    Hi all,

    On a button click event, I have written the following the code to update the database:
    VB Code:
    1. Dim r As DataRow, MyDataAdapter as OleDbDataAdapter
    2.         MyDataAdapter = New OleDbDataAdapter
    3.         MyDataAdapter.SelectCommand = New OleDbCommand("select * from Results where code = '" & Code & "'", cnMyConnection)
    4.         dsMyDataSet = New DataSet
    5.         MyDataAdapter.Fill(dsMyDataSet)
    6.         With dsMyDataSet.Tables(0)
    7.             If .Rows.Count = 0 Then
    8.                 r = .NewRow
    9.                 r("Code") = Code
    10.                 r("First Name") = txtFName.Text
    11.                 r("Right issue") = txtLName.Text
    12.                 .Rows.Add(r)
    13.             Else
    14.                 .Rows(0).Item("First Name") = txtFName.Text
    15.                 .Rows(0).Item("Last Name") = txtLName.Text
    16.             End If
    17.             Try
    18.                 MyDataAdapter.Update(dsMyDataSet)
    19.             Catch ex As Exception
    20.                 MsgBox(ex.Message)
    21.             End Try
    22.         End With

    When the Update method is called, it gives me error: "Invalid INSERT INTO statement".

    Am I doing anything wrong?

    Pls guide

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215

    Re: [02/03] Updating datasource from dataset

    From your code, if dsMyDataSet.Tables(0) has no rows, you add a new row. If it has at least a row, you modified the first row.

    You need to provide an INSERT command and an UPDATE command.

    A CommandBuilder object may do the trick if you don't want to write the INSERT and UPDATE commands yourself.

  3. #3

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    Will you please provide me with the code?

    THx in advance

  4. #4

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    How to use the commandbuilder?

    Or is there a way thru which I can update db without using dataset?

  5. #5
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215

    Re: [02/03] Updating datasource from dataset


  6. #6

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    But...what's wrong in my code above?

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

    Re: [02/03] Updating datasource from dataset

    You cannot get data from a data source without a SELECT statement. How can you save new data to a data source without an INSERT statement? Unless you provide some SQL code that controls how the data is inserted where is it going to go? You should read some ADO.NET tutorials. There are a number of links in my signature.
    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

  8. #8

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    After reading an msdn article I amended my code as below:

    VB Code:
    1. Dim r As DataRow, MyDataAdapter as OleDbDataAdapter
    2.         MyDataAdapter = New OleDbDataAdapter
    3.         MyDataAdapter.SelectCommand = New OleDbCommand("select * from Results where code = '" & Code & "'", cnMyConnection)
    4.         [B]Dim CB As OleDbCommandBuilder = New OleDbCommandBuilder(daResults)[/B]
    5.         dsMyDataSet = New DataSet
    6.         MyDataAdapter.Fill(dsMyDataSet)
    7.         With dsMyDataSet.Tables(0)
    8.             If .Rows.Count = 0 Then
    9.                 r = .NewRow
    10.                 r("Code") = Code
    11.                 r("First Name") = txtFName.Text
    12.                 r("Right issue") = txtLName.Text
    13.                 .Rows.Add(r)
    14.             Else
    15.                 .Rows(0).Item("First Name") = txtFName.Text
    16.                 .Rows(0).Item("Last Name") = txtLName.Text
    17.             End If
    18.             Try
    19.                 MyDataAdapter.Update(dsMyDataSet)
    20.             Catch ex As Exception
    21.                 MsgBox(ex.Message)
    22.             End Try
    23.         End With

    I have used the commandbuilder as shown in the example in msdn. But failed to understand where is it used. There was just a declartion statement. The CB object was not used anywhere else in the code.

    Now, my application generates the following error:
    Code:
    Update unable to find TableMapping['Results'] or DataTable 'Results'.
    Pls bare with me. I am stuck to this prob since 3 days and I cannot proceed with my work before solving this one.

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

    Re: [02/03] Updating datasource from dataset

    You are supposed to pass your DataAdapter object to the CommandBuilder constructor, so your code should be this:
    VB Code:
    1. MyDataAdapter = New OleDbDataAdapter
    2.         MyDataAdapter.SelectCommand = New OleDbCommand("select * from Results where code = '" & Code & "'", cnMyConnection)
    3.         Dim CB As OleDbCommandBuilder = New OleDbCommandBuilder([B][U]MyDataAdapter[/U][/B])
    This creates the link between the CommandBuilder and the DataAdapter so that when you call the Update method of your DataAdapter the CommandBuilder will automatically generate the DeleteCommand, InsertCommand and UpdateCommand as required.

    This is why you shouldn't just copy code examples. They don't explain why things happen the way they do. You should have read the help topic for the OleDbCommandBuilder class and it would have told you how it works and how to use it.
    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

  10. #10

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    Sorry, my mistake... I typed it wrong in the prev post. But in my code its like this:
    VB Code:
    1. Dim CB As OleDbCommandBuilder = New OleDbCommandBuilder(MyDataAdapter)
    .

    I didnt copy the code from the help file.

    Still the appln is giving me the erorr:
    When I call the Update method as follows:
    VB Code:
    1. MyDataAdapter.Update(MyDataSet, "Results")
    it gives me the error:
    Code:
    Update unable to find TableMapping['Results'] or DataTable 'Results'
    And when I call it as:
    VB Code:
    1. MyDataAdapter.Update(MyDataSet)
    it gives me error:
    Code:
    Invalid syntax in INSERT INTO statement
    Last edited by pvbangera; May 24th, 2006 at 04:55 AM. Reason: Addition

  11. #11

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    Is there any way thru which I can view the SQL statement generated by the CommandBuilder object, while calling the DataAdapter's Update method?

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

    Re: [02/03] Updating datasource from dataset

    Have you read about the OleDbCommandBuilder yet? I'd say not or you probably would have seen mention of its GetInsertCommand method. This stuff is all there on MSDN waiting to be read.

    I'd guess that your problem is that you haven't named the DataTable when you've created it. Change this:
    VB Code:
    1. MyDataAdapter.Fill(dsMyDataSet)
    2. '...
    3. MyDataAdapter.Update(dsMyDataSet)
    to this:
    VB Code:
    1. MyDataAdapter.Fill(dsMyDataSet, "Results")
    2. '...
    3. MyDataAdapter.Update(dsMyDataSet, "Results")
    and see if it fixes your issue.
    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

  13. #13

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    I tried your suggestion. But it still gives me the error:
    Code:
    Syntax error in INSERT INTO statement
    Is there any way thru which I can view the SQL statement generated by the CommandBuilder object?

  14. #14

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    I viewed the SQL statement generated by CommandBuilder object:

    Code:
    INSERT INTO Results( Code, First Name , Last Name) VALUES ( ? , ?, ?)
    What could be the prob? Is it coz of the space within the FieldName??

  15. #15

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Updating datasource from dataset

    I tried removing the spaces in the FieldName, but its not solving the prob

  16. #16
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215

    Re: [02/03] Updating datasource from dataset

    Quote Originally Posted by pvbangera
    But...what's wrong in my code above?
    Your code has no INSERT and no UPDATE commands.

  17. #17
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215

    Re: [02/03] Updating datasource from dataset

    sorry, my earlier post was late.

    if a coulumn name has spaces, you need to enclose it within square braces, eg. [First Name]. Alternatively, consider using column names without spaces, the underscore character may be a good replacement.

    hope this will solve your problem.

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

    Re: [02/03] Updating datasource from dataset

    Firstly, you should never use column names with spaces in them. It's just inviting disaster. If you change the names of your columns in the database to remove the spaces it should work. Having said that, if you recode your query and use the actual column names with square brackets instead of the wildcard then the CommandBuilder should hopefully pick that up and do the same:[CODE]SELECT
    Code:
    , [First Name], [Last Name] FROM Results
    Also, you might think about coding your non-query commands yourself. The CommandBuilder is a convenience but problems like this are one of the reasons that doing things yourself is better than relying on autogeneration. The CommandBuilder also has various other limitations that make it unsuitable for a great many situations.
    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