Results 1 to 16 of 16

Thread: Database not updating!

  1. #1

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71

    Database not updating!

    Hi All,

    Any boady know what is rong in this simble code, my DataRow and DataSet is update, but Database is not updating.
    *** Note: I am using Sql - MSDE

    myDR("bankname_a") = "US First Bank"
    myDA.Update(myDS, "Bank")
    myDS.AcceptChanges()

    Thanks in advance
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Do you get any errors or is it just not updating?

    The code looks OK to me so if you dont get any error that means you forgot something elswhere in your code.

    So....
    if error, then what error?
    if no error, more of your code will be needed to solve this I guess.

    For example, is your UpdateCommand correctly written or did you use a commandbuilder to creat it?

    /Leyan

  3. #3

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71
    Athley, Thank you for your reply,

    Yes I am using CommandBuilder, below is my related code in brief:

    ' Initiolize ....
    Dim mySql = "SELECT * FROM AN_Bank"
    Dim myDA As New SqlDataAdapter(mySql, myConn.oConnection)
    Dim myCB As New SqlCommandBuilder(myDA)

    Dim myDS As New DataSet()
    Dim myDR As DataRow

    Private Sub frm_Bank_Load
    myDA.UpdateCommand = myCB.GetUpdateCommand
    myDA.Fill(myDS, "Bank")

    ' Fill the DataRow
    myDR = myDS.Tables("Bank").Rows(0)

    Call DataBinding()
    End Sub

    Private Sub DataBinding()
    Me.cbSelect.DataSource = myDS.Tables("bank").DefaultView
    Me.cbSelect.DisplayMember = "BankName_e"

    Me.txtBankName_e.DataBindings.Add("Text", myDS.Tables("Bank").DefaultView, "BankName_e")
    Me.txtBankName_a.DataBindings.Add("Text", myDS.Tables("Bank").DefaultView, "BankName_a")
    End Sub

    Private Sub btnSave_Click
    myDR("bankname_a") = "First Bank" ' This as test
    myDA.Update(myDS, "Bank")
    myDS.AcceptChanges()
    End Sub

    Thanks again
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  4. #4
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    Hi
    You should have done databinding before you try to initializing a new command builder

  5. #5

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71
    Hi,
    You think it will work if I moved the Command Builder intiolization after the databinding?
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  6. #6
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    Yes
    If you put after the bindings the command builder will then detect the tables that is being bind and automatically generate sql statement to update the database

  7. #7
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    That shouln't make a difference as the commandbuilder builds the command from the adapter Select Query. At least that is what MS says, so it might have a difference. =)

    /Leyan
    Last edited by Athley; Nov 25th, 2002 at 03:09 AM.

  8. #8

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71
    Thanks for the info.

    I'll try out.
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  9. #9
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    One of my questions still stands, do you get an error or does it just dont update?

    /Leyan

  10. #10

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71
    Athley,
    No, no errors, my dataset & data adapter are updated, when I move to next or previos record after the make the update i see the record is updated, but if I closed the form and re-connect and re-get my data from database I get the old data.
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  11. #11
    Addicted Member CoMMiE's Avatar
    Join Date
    Jul 2000
    Location
    Malaysia, Kuala Lumpur
    Posts
    179
    Sorry my mistake
    I think you should initialize your command builder after your dataset has been modifiy that is after the DS.AccepChanges , so that the command builder can generate the update command to update your database

  12. #12
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Ahhhh, I see. I think it has to do with the building of the dataset before you filled it. Change that order.

    myDA.Fill(myDS, "Bank")
    myDA.UpdateCommand = myCB.GetUpdateCommand

    Else the adapter could not know what fields there are in the table for example

    /Leyan

  13. #13

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71
    I did what you said, but still the same problem.

    Even I check the Sql database rights for update, everything is ok.!!!!!!!!!!

    Any ieda what else could be the problem?
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  14. #14
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Hmmm, I Created a SQL database with 2 fields ("BankName_a, "BankName_e"), added 2 textboxes and a combobox, copy/pasted your code added some to get a connection and changed some to get my events to fire and ...

    Code:
    Dim myConn As New SqlConnection("Integrated Security=SSPI;Data Source=LEYAN" _
        & ";Initial Catalog=Snabel;")
    Dim mySql = "SELECT * FROM AN_Bank"
    Dim myDA As New SqlDataAdapter(mySql, myConn)
    Dim myCB As New SqlCommandBuilder(myDA)
    
    Dim myDS As New DataSet()
    Dim myDR As DataRow
    
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
        myDR("BankName_a") = "First Bank"
        myDA.Update(myDS, "Bank")
        myDS.AcceptChanges()
    
    End Sub
    
    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    
        myDA.Fill(myDS, "Bank")
        myDA.UpdateCommand = myCB.GetUpdateCommand
        myDR = myDS.Tables("Bank").Rows(0)
    
        Me.cbselect.DataSource = myDS.Tables("bank").DefaultView
        Me.cbselect.DisplayMember = "BankName_e"
        Me.txtBankName_e.DataBindings.Add("Text", myDS.Tables("Bank").DefaultView, "BankName_e")
        Me.txtBankName_a.DataBindings.Add("Text", myDS.Tables("Bank").DefaultView, "BankName_a")
    
    End Sub
    .... my database updates!!

    Something but the code has to be wrong...

    /Leyan
    Last edited by Athley; Nov 26th, 2002 at 06:10 AM.

  15. #15

    Thread Starter
    Lively Member adsc's Avatar
    Join Date
    Nov 2002
    Location
    Kuwait
    Posts
    71
    YES, it's working....

    The code looks ok, I don't know what was the problem, I've deleted some lines & re-code it again.

    But, a new thing now....
    Binding a field to a text property in a textbox does not mean accepting the modifications correct.....

    When I remove this line (myDR("BankName_a") = "First Bank")
    should I write the following to accept the changes from the textbox:

    myDR("bankname_a") = Me.txtBankName_a.Text
    myDR("bankname_e") = Me.txtBankName_e.Text

    In addition to that in the previos example always the first row in the database table is updated! this means the dataadapter can't point to the correct record in the database!!!
    Last edited by adsc; Nov 26th, 2002 at 11:39 AM.
    <><><><><><><><><><><><><><><><><><>
    <><> REMEMBER,,,,KNOWLEDGE IS POWER <><>
    <><><><><><><><><><><><><><><><><><>

  16. #16
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    If you made changes and jumped to another record to make the changes to the dataset its just to make the update.

    /Leyan

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