HASCHANGES has no change


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: HASCHANGES has no change

  1. #1
    Join Date
    Aug 2005
    Posts
    111

    HASCHANGES has no change

    Hi,

    I have a dataadapter DA, a dataset DS and a textbox txtBox. In the designer view, I set the databinding text property of txtBox to custName in DS and add codes in Page_Load event:

    DA.fill(DS)
    txtBox.DataBind()

    It displays the name as expected. I continue to change the value in txtBox and press a button. The button triggers a ServerClick event which has code:

    If Not DS.HasChanges(DataRowState.Modified) Then Exit Sub
    Dim updDs As DataSet
    updDs = DS.GetChanges(DataRowState.Modified)
    DA.Update(updDs)

    I expect it will perform the update to database, but when I use the debugger to look into the steps, it performs the "Exit Sub" in the IF statement!!!! Why there is no change in DS? Would someone give me some hints? Thank you very much.

  2. #2
    Join Date
    Aug 2005
    Posts
    111

    Thumbs down

    After reading from other web sites, I FEEL that I have something missed in the above coding. But I could not figure it out, because they only show some pieces of information of using dataset updating. So, would someone advise me what is missing, or just give me some web sites which have detailed implementation of Dataset updating (include binding of textbox control)?

    Thank you very much.

  3. #3
    Join Date
    Aug 2005
    Posts
    22

    Talking Try this...

    Whenever I use a dataadapter to update a dataset I create a commandbuilder object for the dataadapter and then call update() of the dataadapter passing it the dataset and the name of the table as parameters. Like this:

    Code:
    'THIS CREATES ALL THE NECESSARY INSERT, UPDATE, DELETE 
    'STATEMENTS NECESSARY TO MAKE THE CHANGES TO 
    'THE DATABASE
    'SDA IS THE SQLDATAADAPTER, DS IS THE DATASET
    Dim sqb As SqlCommandBuilder = New SqlCommandBuilder(sda)
    dsa.Fill(ds, "Employees") 'PASSING IT THE DATASET AND TABLE NAME
    Obviously, you will want to do this within a Try... Catch block. I believe you were passing only the changes made to the dataset to the update() statement. The dataset object as a whole maintains both the original information downloaded as well as the information pertaining to the changes that have been made to it - all of this information is needed for making the updates to the db server. You can use a conditional statement to determine if changes have been made before attempting to update the server. Try something like this:

    Code:
    If (ds.HasChanges() = True)
    As for databinding a textbox, I usually code it by hand if I am not using a typed dataset. Try this:

    Code:
    textBox1.DataBindings.Add("Text", dataSet1.Tables("TableName"), "ColumnName")
    Please let me know if this helps, or if you need more info...
    Last edited by str_test; 09-14-2005 at 10:38 AM.

  4. #4
    Join Date
    Aug 2005
    Posts
    111
    Hi str_test, thank you very much for your help. I really frustrated for working on this over two weeks. I follow your advice to add the sqlcommandbuilder statement and now the content of the code-behind page is like that:

    In Page_Load event:
    IF not.Page.ispostback then
    DA.selectcommand.connection.connectionstring = Conn.connectionstring
    DA.Fill(DS)
    txtBox.databind()
    END IF

    In the ServerClick event of the "Save" button:

    Private Sub Save_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
    Dim sqb As SqlCommandBuilder = New SqlCommandBuilder(DA)
    DA.selectcommand.connection.connectionstring = Conn.connectionstring
    DA.Fill(DS)

    Dim dt As New DataTable
    dt = DS.Tables(0).GetChanges(DataRowState.Modified)

    Dim dr As DataRow
    dr = dt.Rows(0)
    dr.BeginEdit()
    dr.Item("surname") = txtSurname.Text
    'dr.Item("institution") = txtInstitution.Text
    dr.EndEdit()

    If dt.HasChanges(DataRowState.Modified) Then
    DA.Update(DS)
    DS.AcceptChanges()
    End If
    END sub

    I find two problems here:
    1. If I use the FILL statement in the ServerClick event, it seems that the new value in the txtBox could not update to the database. But, it still passes the dt.HASCHANGES statement and perform the DA.update(DS)?
    2. If I remove the FILL statement, it seems that the DS has been destroyed and all dt and dr statements returns error: "Object referenct not set to an instance of an object..."

    By the way, do you have any web site suggested which could provide a comprehensive and detail examples of how to use dataadapter, dataset and binding textbox control ( in design time) to me?

    Thanks again.

  5. #5
    Join Date
    Aug 2005
    Posts
    22

    Talking I could write an app...

    Probably the easiest way to demonstrate exactly what I am talking about would be to write a small app that does what you need it to do (I realize that you are working in ASP.NET, but an app would offer us something very clear and direct for both of us to look at and discuss). Are you able to get a copy of the Northwind mdb file (this will be the db we will access in the app - it will allow us to avoid problems with connection strings, etc. that may arise if we are attempting to use SQL server)? I attempted to upload the mdb verision of Northwind in zip format but was unable to due to size limitations on attached files to this site.

    Let me know if you are able to download or get access to the db.

    I will get to work on the example shortly.

    I want to be of any help that I can. I know how frustrating things like this can be...

  6. #6
    Join Date
    Aug 2005
    Posts
    111
    Hi str_test,

    I really appreciate your help. It is quite difficult for me to upload the pages. But I have pasted my works step-by-step in a MS Word file as in the attachment. I have changed some of my codings in the ServerClick event and this is the most updated verison. Hope that you could give me some advices. Thank you very much.
    Attached Files Attached Files

Similar Threads

  1. How to change the background color?
    By vegeta in forum C++
    Replies: 6
    Last Post: 06-14-2005, 01:56 PM
  2. how change preferred sound card?
    By midnait in forum VB Classic
    Replies: 0
    Last Post: 04-27-2001, 03:23 AM
  3. How to generate Make Files?
    By aMax in forum Java
    Replies: 2
    Last Post: 08-04-2000, 05:55 PM
  4. Replies: 6
    Last Post: 03-25-2000, 12:29 PM
  5. Replies: 0
    Last Post: 03-23-2000, 01:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center