Undo Changes to a table after SQL fill() command

Posted on 2006-06-01
Last Modified: 2010-04-23
Hi Experts,

I have a Data table that i fill from a SQLDataAdapter.  The table has a unique column that triggers an error if data gets added with a row with duplicate data in that column, which i want it to do but i cant get the reject changes to then remove the rows that i have just added!  here is the snippet of code that is not working.


SQLDa.SelectCommand.CommandText = "SELECT * FROM db WHERE ID = '123456'"

   CompTBL.Columns("ID").Unique = True
Catch ex As Exception
End Try
Question by:SteSi
    LVL 34

    Accepted Solution

    .RejectChanges works by replacing a row of which the .RowState is Modified by (its own stored) version for which the .RowState is Unchanged.  When .Fill adds a row to a datatable it does so with its .RowState set to Unchanged.  So .RejectChanges won't see it.  That's why your approach is not working.

    But why, I wonder, are you trying to add the row before you check if it already exists in the datatable?  What would be wrong with something on the lines of

       Dim dr() As DataRow = CompTBL.Select("ID = '123456'")
       If dr.Length = 0 then
       End If

    LVL 1

    Author Comment

    Hi Roger,

    the only reason i didn't do it that way and tried it the rejectchanges was because before i do the first fill command there is no column structure in the CompTBL so there is no ID column.  Is there anyway that i can set the CompTBL up without adding any data?
    LVL 34

    Expert Comment

    Is there anyway that i can set the CompTBL up without adding any data?

    Yes.  If you are using the wizards to set up your connection and dataadapter you can also get them to generate a strongly typed dataset including the relevant table.  Just right-click on the dataadapter on your form in design view and select "Generate Dataset ...".  

    Or, if you are coding the dataadapter yourself, you can define the table before calling the .Fill method.  It's on these lines (very much in outline)

       Dim CompTBL As New DataTable
       Dim dc0 As New DataColumn
       With dc0
           .ColumnName = "<whatever you want>"
           .DataType = GetType(String) 'or Integer, or whatever
       End With
       'and similar for all other columns

    But a different way round that problem would be to enclose

       Dim dr() As DataRow = CompTBL.Select("ID = '123456'")
       If dr.Length = 0 then
       End If

    in some sort of test code.  E.g.

       If Not CompTBL Is Nothing Then

    or even in a Try Catch block with nothing in the Catch.

    LVL 1

    Author Comment

    I found a way to build the table just by using a SELECT command that didn't generate any records and then filled the table on initialize which got round my empty table problem.

    Thanks for your help, i just thought there would be commands that would do these for me but thats always the way!

    Points awarded

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now