Results 1 to 5 of 5

Thread: Stuck in saving Records in DB using ADO.Net (RESOLVED)

  1. #1

    Thread Starter
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Stuck in saving Records in DB using ADO.Net (RESOLVED)

    I am editing a specific column of a DataRow using Item collection.. then i am using DataAdapter's Update method to save the changes but it is giving me

    System.InvalidOperationException: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.


    Now how can i know the update command for this row@ run time? I am lost
    Well.. I am new to Ado.Net and VB.Net......

    ***Believe me.. I used to be a VB GURU***
    Last edited by moinkhan; Jun 30th, 2003 at 03:31 AM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Here you need to define the variable MyConnection as
    OleDbConnetion supplied with connection string . btw , you don't
    need OleDbAdapter in this method . Here you are actually
    executing an OleDbCommand .
    VB Code:
    1. Public Sub EditMyData(ByVal SQL_UPDATE As String)
    2. OpenDB()
    3.  
    4. Dim MyCommand As New OleDbCommand(SQL_UPDATE, MyConnection)
    5. Try
    6. If MyCommand.ExecuteNonQuery() Then
    7.  MsgBox("Successfully Updated")
    8. Else
    9. MsgBox("WRONG")
    10. End If
    11. Catch x As Exception
    12. MsgBox(x.Message)
    13. End Try
    14.  
    15. MyConnection.Close()
    16. MyCommand.Dispose()
    17.  End Sub

    Usage

    VB Code:
    1. Private Sub Button7_Click(ByVal sender As System.Object, ByVal
    2. e As System.EventArgs) Handles Button7.Click
    3.  
    4.  Dim MyTable As String = "MyTab"
    5.  
    6.         Dim str1 As String = TextBox1.Text
    7.         Dim str2 As String = ComboBox1.Text
    8.         Dim str3 As String = ComboBox2.Text
    9.         Dim str4 As String = TextBox2.Text
    10.         Dim str5 As String = TextBox3.Text
    11.         Dim str6 As String = TextBox4.Text
    12.         Dim str7 As String = TextBox5.Text
    13.         Dim str8 As String = ComboBox3.Text
    14.         Dim str9 As String = TextBox6.Text
    15.  
    16. Dim Updater As String = "UPDATE " & MyTable & " SET
    17. YourField1='" & str1 & "'," & " YourField2='" & str2 & "'," & "
    18. YourField3='" & str3 & "'," & " YourField4='" & str4 & "'," & "
    19. YourField5='" & str5 & "'," & " YourField6='" & str6 & "'," & "
    20. YourField6='" & str7 & "'," & "YourField7='" & str8 & "'," & "
    21. YourField8='" & str9 & "'" & " WHERE YourField1='" & str1 & "'"
    22.  
    23.  
    24. 'Call the sub to save edited data
    25. EditMyData(Updater)
    26. End Sub

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    Re: Stuck in saving Records in DB using ADO.Net

    Originally posted by moinkhan
    I am editing a specific column of a DataRow using Item collection.. then i am using DataAdapter's Update method to save the changes but it is giving me

    System.InvalidOperationException: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.


    Now how can i know the update command for this row@ run time? I am lost
    Well.. I am new to Ado.Net and VB.Net......

    ***Believe me.. I used to be a VB GURU***
    Post some code so we can see what you are doing wrong. Its more than likely that you did not specify an update command.

  4. #4
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    As DevGrp has stated, it sounds like you haven't put in the command builders. there is one for each action: Insert, Update & Delete

    You'll need to use .GetUpdateCommand like so....

    Code:
          Dim cmdBuild As OleDbCommandBuilder = New OleDbCommandBuilder(daCty)
    
            'Generate the command builders
            daCty.UpdateCommand = cmdBuild.GetUpdateCommand
    You might have to post your code for us to help you to see what you have done....or not done!

  5. #5

    Thread Starter
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Stuck in saving Records in DB using ADO.Net(RESOLVED)

    Thanx.. It's resovled..
    By the hints from u ppl i wrote the following Code....
    and it worked...
    Thanx everybody

    Wallabie!! your GetUpdateCommand seemed to do the trick.. Thanx....

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = F:\Moin\.Net Products\EditingDataSet\temp.mdb"
    3.         con.Open()
    4.         cmd.Connection = con
    5.         cmd.CommandText = "Select * from Crime"
    6.         da.SelectCommand = cmd
    7.         da.Fill(ds, "Crime")
    8.         Dim cmdBuilder As New OleDbCommandBuilder(da)
    9.         ds.Tables(0).Rows(0).Item("Doer") = "Salman Talpur Achakzai"
    10.         da.UpdateCommand = cmdBuilder.GetUpdateCommand
    11.         da.Update(ds, "Crime")
    12.     End Sub
    13. End Class

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