Results 1 to 8 of 8

Thread: distinct values in dataset

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    distinct values in dataset

    I have populated a dataset with one table.
    The data in the table looks as follows:
    Index1Name Index2Name City Value
    Indexk Indexk city1 2
    Indexk Indexk city1 3
    Indexk Indexd city1 2
    Indexk Indexy city1 1
    Indexy Indexk city1 1
    Indexx Indexk city1
    Indext Indexq city1 5
    Indexs Indexs city1 6

    Now I would like to have an array which gets populated from the distinct values in columns of Index1Name and Index2Name
    So the array should contain something like:
    Indexd Indexk Indexy Indexx Indext Indexs Indexq

    Thanks

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: distinct values in dataset

    How did you populate the dataset?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: distinct values in dataset

    SqlDataAdapter oDA = new SqlDataAdapter(comm);

    DataSet dsData = new DataSet();

    oDA.Fill(dsData, "datavalues");

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: distinct values in dataset

    Not exactly what i meant. I presume you had a SQL statement to select values?
    Something like:

    SELECT * FROM tableName

    What you could do is to use the DISTINCT keyword in SQL to get distinct records which would save you lots of trouble.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: distinct values in dataset

    Ok, thanks

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: distinct values in dataset

    Assuming I now have a dataset with the column values being distinct, how can I populate an array with the values please?

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: distinct values in dataset

    I would have a way to do it but really doubt it's the correct way. Just change the column name/value below

    vb Code:
    1. 'Get number of results/rows returned by the dataset
    2. Dim numResults As Integer = dsData.Tables("dataValues").Rows.Count - 1
    3. Dim myResults(numResults) As String  'Dimension the array? Should this be +1
    4.  
    5. Dim Drow As DataRow
    6.  
    7. For i As Integer = 0 To numResults  'Go through each row
    8.     Drow = dsData.Tables("dataValues").Rows(i)    'assign current row
    9.     myResult(i) = Drow("Column").ToString          'Change column to your name
    10. Next

    My advice - wait until someone who knows about SQL stuff replies - I've only started it. It's most likely either badly flawed or really slow.


    Edit: I found this example on the web. Kind of the same approach -

    vb Code:
    1. Dim arData() As String
    2. Dim ds As New DataSet
    3. ' Fill your dataset in your own way here
    4.  
    5. ReDim arData(ds.Tables(0).Rows.Count)  'Is this correct?
    6.  
    7. For intCounter As Integer = 0 To ds.Tables(0).Rows.Count - 1
    8.        arData(intCounter) = ds.Tables(0).Rows(intCounter).Item(0).ToString
    9.  
    10. Next
    Last edited by stimbo; May 1st, 2007 at 05:15 AM. Reason: fix format
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: distinct values in dataset

    Is there a particular reason that you need the values in an array rather than just using then directly from the DataTable?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width