Results 1 to 9 of 9

Thread: SQL Database new row won't save

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    SQL Database new row won't save

    I have the following code. When I add a new row, it adds it to the dataset in memory but never updates the database on the hard drive. What am I doing wrong?

    VB Code:
    1. myCon = New SqlConnection("Data Source=.\SQLEXPRESS..........)
    2. myDA = New SqlDataAdapter("SELECT * FROM Rescue", myCon)
    3. myDA.TableMappings.Add("Table", "Rescue")
    4. myDS = New DataSet
    5. myDA.Fill(myDS)
    6.  
    7. Dim myRow As DataRow = myDS.Tables("Rescue").NewRow
    8. myRow.Item("CallNum") = Me.TxtCallNum.Text
    9. myRow.Item("TimeCall") = Me.TxtTimeCall.Text
    10. myDS.Tables("Rescue").Rows.Add(myRow)
    11. myDA.Update(myDS)

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

    Re: SQL Database new row won't save

    What value does Update return? If it is non-zero then the record is being saved.
    VB Code:
    1. Dim recordsAffected As Integer = myDA.Update(myDS)
    2.  
    3. MessageBox.Show(recordsAffected & " records were affected.")
    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
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Database new row won't save

    Quote Originally Posted by jmcilhinney
    What value does Update return? If it is non-zero then the record is being saved

    It is returning 0.

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

    Re: SQL Database new row won't save

    I'm guessing that it is your use of a table mapping that is messing things up. Try this and see if it works:
    VB Code:
    1. myCon = New SqlConnection("Data Source=.\SQLEXPRESS..........)
    2. myDA = New SqlDataAdapter("SELECT * FROM Rescue", myCon)
    3. myDS = New DataSet
    4. myDA.Fill(myDS, "Rescue")
    5.  
    6. Dim myRow As DataRow = myDS.Tables("Rescue").NewRow
    7. myRow.Item("CallNum") = Me.TxtCallNum.Text
    8. myRow.Item("TimeCall") = Me.TxtTimeCall.Text
    9. myDS.Tables("Rescue").Rows.Add(myRow)
    10. myDA.Update(myDS, "Rescue")
    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
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Database new row won't save

    Using your code, the update command returns the following error:

    "Update requires a valid InsertCommand when passed DataRow collection with new rows."

    This should be a straightforward answer. Simply how to add rows to a sql express database. Should be used by many people. Am I doing something different that is making this a harder question than it should be?

    Greg

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

    Re: SQL Database new row won't save

    That indicates that it was definitely the table mapping that was messing you up before. The DataAdapter was looking for a table with the wrong name. Now it's looking for, and finding, the correct table, but you haven't provided any SQL code to insert new rows into the database. You couldn't retrieve any rows without a SELECT statement, so likewise you cannot add any rows without an INSERT statement. You could use an SqlCommandBuilder to generate the SQL code automatically but they have so many limitations that I suggest that you avoid them altogether. Follow the ADO.NET tutorial link in my signature and read up on the basics of data access. If you're still unable to proceed after that then post back again.
    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
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Database new row won't save

    Two things. First of all, you're right. I shouldn't have had that table mapping in there. It was accidentally left when I was trying to add multiple tables to the dataset.

    But even with it removed, I got this error:

    Update requires a valid InsertCommand when passed DataRow collection with new rows.

    I saw on a few sites to add this line:

    Code:
    Dim myDataRowsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(myDA)
    Not sure how it works when I don't ever really use this line besides declaring it here. But it works.

    Greg
    Last edited by ghall426; Jan 6th, 2007 at 01:22 AM.

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

    Re: SQL Database new row won't save

    As I said in my previous post, you can use an SqlCommandBuilder to generate the required SQL code automatically. That's how it works. When your SqlDataAdapter requires the SQL code to insert a record it is automatically generated by the SqlCommandBuilder. I also said that I recommend against their use because they have important limitations. I recommend that you code your own SQL code to insert your records, as I said in my previous post. If you read about the SqlDataAdapter in the MSDN documentation you'll get pretty much all the information you need. I'd concentrate on the InsertCommand property.
    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
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Database new row won't save

    OK. I understand better now. Sorry I missed that in your previous post. But thanks for telling me again.

    Greg

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