?
Solved

Updating a database??????

Posted on 2006-05-21
9
Medium Priority
?
175 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:Dave091277
  • 4
  • 4
9 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16728980
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!
0
 

Author Comment

by:Dave091277
ID: 16728995
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!!!

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16729014
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:newyuppie
ID: 16729015
do you have text in TextBox1.Text? put a msgbox(textbox1.text) before dr(0)("CompanyName") = TextBox1.Text to check that...
0
 

Author Comment

by:Dave091277
ID: 16729043
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.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16729302
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.
0
 

Author Comment

by:Dave091277
ID: 16729326
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??
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 400 total points
ID: 16729338
Dave091277,

before calling the Update method, add this line:
me.BindingContext(ds.Tables("Customers")).EndCurrentEdit()
0
 

Author Comment

by:Dave091277
ID: 16729362
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!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question