Results 1 to 7 of 7

Thread: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    I have written such a simple code in vb.net
    I have used data adapter command and dataset to pass data in database
    this is my code
    Code:
     Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
            If Mode = "New" Then
                sql = "select * from donorentry"
                cmd = New SqlClient.SqlCommand(sql, cn)
                Dim dt As DataTable
    
                dset.Clear()
                dadap.SelectCommand = cmd
                'dadap.Fill(dset)
                dadap.Fill(dset, "donorentry")
                dt = dset.Tables("donorentry")
                ro = dt.NewRow
                'dset.Clear()
                SetRows()
    
                dt.Rows.Add(ro)
                ' ==================== Error on  this statement ================
               'error is 
    'Update requires a valid InsertCommand when passed DataRow collection with new rows.
                dadap.Update(dset, "donorentry")
    
    
            ElseIf Mode = "Edit" Then
                ro.BeginEdit()
                SetRows()
                ro.EndEdit()
            End If
    
        End Sub


    Code:
     Private Sub SetRows()
    'initialization of record into ro (a data row)
    ' ro is declared globally
            ro.Item("did") = txtDID.Text
            ro.Item("entrydate") = dtEntryDate.Value
            ro.Item("fname") = txtFirstName.Text
            ro.Item("lname") = txtLastName.Text
            ro.Item("sname") = txtSurName.Text
    
    ' first time tried this method of initializing it was also not working then changed into above format.
            'ro!hadd1 = txtHadd1.Text
            'ro!hadd2 = txtHadd2.Text
            'ro!hcity = cmbCity.Text
            'ro!hstate = cmbHstate.Text
            'ro!hcountry = cmbHCountry.Text
            'ro!hpin = txtHPincode.Text
    
        End Sub

    I am trying from three days but I couldn't touch the solution.
    Every where on net I found this method to insert record. I cant understand where there is silly mistake in this code.
    Please help me
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

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

    Re: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    You have written the SQL code to retrieve data from the database:
    Code:
    sql = "select * from donorentry"
    The SQL code to insert data into the database will not appear by magic. Either you have to write it or you have to tell the system to generate it for you. Follow the Data Access link in my signature for examples of each way.
    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
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    Ohhh, it becomes very lengthy because i have so many fields in my each table, what can we do for it.
    Is it the one only solution?


    I have referred this links and I have also referred which do not say to write insert statement.
    http://metrix.fcny.org/wiki/display/...ment+in+VB.NET
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

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

    Re: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    The code in that link uses a command builder, as does one of the examples in the Data Access link of mine. I alluded to that in my previous post:
    Either you have to write it or you have to tell the system to generate it for you. Follow the Data Access link in my signature for examples of each way.
    If you want to insert data then you need an SQL INSERT statement. You already have the two options for creating it in front of you.

    If writing a bit of SQL code with a lot of column names in it is a problem for you I suggest you give up programming now.
    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
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    yes i did it without insert query.......
    i just changed two lines......


    Code:
    If Mode = "New" Then
                sql = "insert into donorentry ()"
                sql = "select * from donorentry"
                cmd = New SqlClient.SqlCommand(sql, cn)
                Dim dt As DataTable
                
                dset.Clear()
                dadap.SelectCommand = cmd
                'dadap.Fill(dset)
                dadap.Fill(dset, "donorentry")
    '==========
    '                  it automatically creates insert command
                Dim cmdbldr As New SqlCommandBuilder(dadap)
                cmdbldr.GetInsertCommand()
    '==========
                dt = dset.Tables("donorentry")
                ro = dt.NewRow
                'dset.Clear()
                SetRows()
    
                dt.Rows.Add(ro)
               
                dadap.Update(dset, "donorentry")
    I got this thing from
    http://metrix.fcny.org/wiki/display/...ment+in+VB.NET

    and MSDN
    http://msdn.microsoft.com/en-us/libr...ndbuilder.aspx

    I was trying from three days now its done

    Thank v.much you for your co-operation.
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    UAE
    Posts
    191

    Re: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    I don't give up anything I just find solution hidden inside problem.
    Each New Difficulty Is Best Opportunity,
    If You Ignore Solving It,
    You Lost Your Life's Best Opportunity.
    _______________________________________
    Dynamic Property value assignment - C#
    TCP client/server connection

    CatchIt-Game
    Fortunes
    SQL Tutorials

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

    Re: Error in data set. Update requires a valid InsertCommand when passed DataRow.....

    Quote Originally Posted by ishrar
    yes i did it without insert query
    No, you didn't. What you did was tell the system to generate the INSERT statement for you, as I have already said twice. Data cannot be inserted into a database without an INSERT statement. My Data Access thread provides a code example of doing just that.
    Quote Originally Posted by jmcilhinney
    Note that if your query involves only one table and it has a primary key then you can take the easy option and use a CommandBuilder instead of creating the non-query commands yourself:
    vb.net Code:
    1. Dim connection As New SqlConnection("connection string here")
    2. Dim adapter As New SqlDataAdapter("SELECT ID, Name, Quantity, Unit FROM StockItem", connection)
    3. Dim builder As New SqlCommandBuilder(adapter)
    4.  
    5. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    6.  
    7. Dim table As New DataTable
    8.  
    9. 'Retrieve the data.
    10. adapter.Fill(table)
    11.  
    12. 'The table can be used here to display and edit the data.
    13. 'That will most likely involve data-binding but that is not a data access issue.
    14.  
    15. 'Save the changes.
    16. adapter.Update(table)
    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