Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if duplicates exist

Status
Not open for further replies.

jcs1953

IS-IT--Management
Nov 28, 2007
53
US
This is the code I'm using:

Dim mytable1 As DataTable
mytable1 = DAL.GetCombinedPSAResults(TBPID.Text)
Dim dr As New DataTableReader(mytable1)
mytable1.Load(dr)
Dim dv As DataView = mytable1.DefaultView
Dim mytable2 As DataTable
mytable2 = DAL.GetOraclePSAResults(TBPID.Text)
Dim dr2 As New DataTableReader(mytable2)
mytable1.Load(dr2)
Dim dv1 As DataView = mytable1.DefaultView
recordcount = dv1.Count

This will return a datatable with the results of the 2 functions combined. The table has 2 columns, Date and Results. I use the data to create a chart. Unfortunately now I have run into a situation where several of the Date values are the same and that doesn't work well in a chart. So what I need is a way to determine if there are duplicate values in the Date column so i can display the data in a gridview instead of a chart.

If (date column has duplicates) Then
put data in gridview
Else
Use data for chart

Thanks for your help.
 
1. sort table by date.
2. loop through table comparing current record to previous record.

if at any point the previous = current load into grid, else load into chart.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
So sorting would be
dv1.sort = "Date DESC"

I'm getting confused on the looping part. Would I set a variable first to the date value?
 
Code:
if(table.Rows.Count == 0)
{
   load chart;
}
else
{
   var useChart = true;

   var view = table.DefaultView;
   view.Sort = "date field asc. or desc.";
   var previous = view[0];

   for(var i = 1; i < table.Rows.Count; i++)
   {
        var current = view[i];
        if(current["date"] == previous["date"])
        {
            useChart = false;
            break;
        }
        previous = current;
   }

   if(useChart)
   {
      load chart;
   }
   else
   {
      load grid;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Hi Jason,
Thanks for the example. I'm trying to follow it but am C# illiterate (and it seems VB illiterate sometimes too). Could you please translate to VB?
Thanks
 
there are online translators. and it's basics syntax, if/else, indexing, declarations.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks again Jason. I had no idea there were online C# to VB converters! It's those little things ......
 
I converted the code to VB but there are 2 parts that I don't understand.



1. If table.Rows.Count = 0 Then
2. Dim chart As load
3. Else
4. Dim useChart = True
5.
6. Dim view = table.DefaultView
7. view.Sort = "date field asc. or desc."
8. Dim previous = view(0)
9.
10. For i = 1 To table.Rows.Count - 1
11. Dim current = view(i)
12. If current("date") = previous("date") Then
13. useChart = False
14. Exit For
15. End If
16. previous = current
17. Next
18.
19. If useChart Then
20. Dim chart As load
21. Else
22. Dim grid As load
23. End If
24. End If

If table.Rows.Count = 0 Then Dim chart As load

Why load a chart if the table has no rows and what is "load"? Is that a property in C# that's not in VB?
 
it's pseudo code. if there are no rows do "X" else load a chart or a grid, your choice.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Got it work. Thanks Jason. I just wish there were some type of converter to translate VB into plain English!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top