Link to home
Start Free TrialLog in
Avatar of Dave091277
Dave091277

asked on

Updating a database??????

Hi,

I'm having a bit of trouble with updating a database. The code i have at the minute is......

 Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        Dim dr() As DataRow = ds.Tables("Customers").Select("CustomerID = '" & cbCustomers.SelectedItem(0) & "'")
   
        If UBound(dr) > -1 Then
            dr(0)("CompanyName") = TextBox1.Text
        End If
        daAddCustomer.Update(ds, "Customers")

    End Sub

.....When i click the button, the dataset seems to change but the database isn't actually updated. I have found though that is i change

dr(0)("CompanyName") = TextBox1.Text   to
dr(0)("CompanyName") = "New companyname"

it updates properly. I have tried everything I can think of but I'm still really new to all this, can anyone point me in the right direction??

Cheers
Dave
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Hi Dave091277,

ADO.Net uses disconnected dataset. That means that until you explicitely call an update method. nothing is sent to the server.

You should have a DataApdapter object somewhere that you used to fill your DataSet object. You need to use this adapter object and call its .Update method to send modifications to the database.

Cheers!
Avatar of Dave091277
Dave091277

ASKER

hi,

Thanks for the quick reply. I have a line in there that I thought was the update.....

daAddCustomer.Update(ds, "Customers")

....Should I have someting else there?? If i put an actual text value instead of textbox1.text it updates properly. It just won't seem to take what is in the textbox and write that to the DB. I'm thinking it because its bound to the dataset but like i said, I'm really new to this so I'm just guessing!!!

Dave091277,

If you change
> dr(0)("CompanyName") = TextBox1.Text  

for
> dr(0)("CompanyName") = "New companyname"

Your update is working?

Are you sure you are having a valid value into TextBox1.Text?

Is any exception is triggered?
do you have text in TextBox1.Text? put a msgbox(textbox1.text) before dr(0)("CompanyName") = TextBox1.Text to check that...
just checked all that and the message box displays the new value i type in the textbox so i know thats all ok.

In the Forms load sub i have ....

TextBox1.DataBindings.Add("Text", ds.Tables("Customers"), "CompanyName")

...so that the text in the textbox shows the record i select from a combo box. If i take that line of code away the database is updated properly but I need it to be bound so that the customers details are displayed.
Dave091277,

If you are bound, you don't need to put values back into the datarow but you still need to call the Update method of the Adapter.

>>If i take that line of code away the database is updated properly but I need it to be bound so that the customers details are displayed.

You may need to call the EndCurrentEdit of the currency manager to send values from your control back to the datatable before calling the Update method.
That sounds like we might be getting somewhere!! I had just noticed that when I changed a value it would stay, so like you say i just need to update. How do i go about doing that with the currency manager, I am really new to this!! Will I have to create an instance of the currency manager and go from there??
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much!!

just before i read this i remembered a bit about the currency manager and added....

        Dim cm As CurrencyManager = Me.BindingContext(ds.Tables("Customers"))
        cm.EndCurrentEdit()

...which worked. I'll change it to your code now because it looks better!! Thank you again!! I didn't think anyone was going to be able to solve that!!