Results 1 to 21 of 21

Thread: Create a dataset from a Queue Collection (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Create a dataset from a Queue Collection (Resolved)

    Is it possible to take these Queue items and put them back into a (new) dataset?
    VB Code:
    1. Private Function GetCustomerInfo() As DataSet
    2.         Dim Stores As String() = { _
    3.             CStr(1128), _
    4.             CStr(1129)}
    5.  
    6.         Dim rendomCustQueue As New Queue
    7.         Dim CustomerPicker As New Random
    8.         Dim myrows As DataRow()
    9.         Dim value As Integer
    10.  
    11.         For Each storeNum As String In Stores
    12.             myrows = GetRandomCustomers.Tables(0).Select("LastStore = '" & storeNum & "'")
    13.             If myrows.Length > 0 Then
    14.                 value = CustomerPicker.Next(0, myrows.Length)[b]
    15.                 rendomCustQueue.Enqueue(myrows(value).Item("FullName").ToString)
    16.                 rendomCustQueue.Enqueue(myrows(value).Item("Address").ToString)
    17.                 rendomCustQueue.Enqueue(myrows(value).Item("City").ToString)
    18.                 rendomCustQueue.Enqueue(myrows(value).Item("State").ToString)
    19.                 rendomCustQueue.Enqueue(myrows(value).Item("Zip").ToString)
    20.                 rendomCustQueue.Enqueue(myrows(value).Item("LastStore").ToString)[/b]
    21.             End If
    22.             Dim en As System.Collections.IEnumerator = rendomCustQueue.GetEnumerator()
    23.  
    24.             While en.MoveNext()
    25.                 ' Create and Return Dataset here???
    26.             End While
    27.         Next
    28.     End Function

    I tried this but it didn't work.
    VB Code:
    1. Dim ds As New DataSet
    2.                 Dim dt As New DataTable
    3.  
    4.                 Dim FullName As DataColumn
    5.                 Dim Address As DataColumn
    6.                 Dim City As DataColumn
    7.                 Dim State As DataColumn
    8.                 Dim Zip As DataColumn
    9.                 Dim Store As DataColumn
    10.  
    11.                 FullName = (rendomCustQueue.Enqueue(myrows(value).Item("FullName").ToString))
    12. etc....
    Last edited by FastEddie; Sep 7th, 2006 at 02:27 PM.

  2. #2
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Create a dataset from a Queue Collection

    You are going to have to create the dataset structure you want and then insert the items from the queue into the dataset in their respective columns.

    Try looking at the following link.
    http://msdn2.microsoft.com/en-us/library/ms171909.aspx

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    I think I understand that part of it and I can create a datatable on the fly with this code.
    VB Code:
    1. Dim ds As DataSet
    2.         Dim tbl As DataTable
    3.         Dim col1, col2 As DataColumn
    4.  
    5.         ds = New DataSet()
    6.         tbl = New DataTable("mytable")
    7.         col1 = New DataColumn("Name", Type.GetType("System.String"))
    8.         col2 = New DataColumn("Address", Type.GetType("System.String"))
    9.  
    10.         tbl.Columns.Add(col1)
    11.         tbl.Columns.Add(col2)
    12.         tbl.Rows.Add("Fast Eddie", "123 Nowhere")
    13.         tbl.Rows.Add("Tom Servo", "123 Space")
    14.  
    15.         ds.Tables.Add(tbl)
    16.         DataGridView1.DataSource = tbl

    My problem is that I don't know how to pluck the items from the queue and place them in my dataset.

  4. #4
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Create a dataset from a Queue Collection

    You can cycle through your queue using similiar code, however where they belong in your dataset depends on your information in the queue.

    VB Code:
    1. Dim numbers As New Queue(Of String)
    2.         numbers.Enqueue("one")
    3.         numbers.Enqueue("two")
    4.         numbers.Enqueue("three")
    5.         numbers.Enqueue("four")
    6.         numbers.Enqueue("five")
    7.  
    8.         ' A queue can be enumerated without disturbing its contents.
    9.         For Each number As String In numbers
    10.             Dim myRow As DataRow = myDataSet.Table(0).NewRow ' I think this is correct
    11.             myRow(0) = number
    12.             Debug.Print(number)
    13.              
    14.            myDataSet.Table(0).Row.Add(myRow)
    15.  
    16.         Next

    I cannot be positive the syntax is correct so dont just copy and paste it becuase I might be missing something, at work right now but not at a computer with VS on it.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    I am lost. Is this anywhere near close to what I need?
    VB Code:
    1. For Each storeNum As String In Stores
    2.             myrows = GetRandomCustomers.Tables(0).Select("LastStore = '" & storeNum & "'")
    3.             If myrows.Length > 0 Then
    4.                 value = CustomerPicker.Next(0, myrows.Length)
    5.                 Dim aDs As New DataSet
    6.                 Dim numbers As New Queue(Of String)
    7.                 numbers.Enqueue("FullName")
    8.                 numbers.Enqueue("Adresss")
    9.                 numbers.Enqueue("three")
    10.                 numbers.Enqueue("four")
    11.                 numbers.Enqueue("five")
    12.                 For Each number As String In numbers
    13.                     Dim myRow As DataRow = aDs.Tables(0).NewRow
    14.                     myRow(0) = number
    15.                     Debug.Print(number)
    16.                     ' aDs.Tables(0).Row.Add(myRow)
    17.                 Next
    18.             End If
    19.         Next

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    Hi FastEddie, why you want to put the queue items in to another dataset?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    Because my last step is to take the customer list and address information and create an XML file so that it can be used with a merge doc or mailing label program.

    From start to finish I had a list of 500 customers (all stores) that needed to be narrowed down to one winner from each store (29). I have that now so I need to take the 29 randomly selected customers and send them to an XML file for merging into a form letter and mailing labels.

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    I think I did not explain the queue in my last post. The queue is an abstract construction and the order of data it has is firs in firs out. Now one thing to make the queue usable in your case is to save an entire array with the customer’s info in the queue. In that way it will be more usable. Like this:
    VB Code:
    1. rendomCustQueue.Enqueue(myrows(value))
    instad of this
    VB Code:
    1. numbers.Enqueue("FullName")
    2.  numbers.Enqueue("Adresss")
    3.  numbers.Enqueue("three")
    4.  numbers.Enqueue("four")
    5.  numbers.Enqueue("five")
    and you can get the queue items using
    VB Code:
    1. rendomCustQueue.Dequeue() 'it will return the first item in the queue and remove the item from the queue.
    or
    VB Code:
    1. rendomCustQueue.Peek() 'it will return the first item in the queue with out removing the item from the queue.
    Now I am not sure if you need a dataset to creat an xml file. If you want to creat dataset in order to have indexed access to the wining customers information then the abov exaple will do. just Dequeue the queue items and put them in xml file.

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    By the way if you want to get the customer’s row and info from the queue you can use
    VB Code:
    1. Dim name, address As String
    2.  Dim dr As DataRow
    3.  dr = CType(rendomCustQueue.Dequeue, DataRow)
    4.  name = CStr(dr.Item("FullName"))
    5.  address = CStr(dr.Item("Adresss"))

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    I have never created xml file. Is it the same as you create a text file? You open the file and write in it. If it so then you don’t need a queue even. You can write the customer info in the For Each loop and close the file after the loop is over.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    Yes there is a little more involved but it is essentialy the same thing. Can you point me to a sample?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    I already have code that creates an XML file that is perfect for my mail merge so if I can get this to work in a dataset I am home free.

    By the way if anyone is interested here is the code to turn a DS into an XML file here it is.
    VB Code:
    1. myDataset.WriteXml("c:\Customers.xml", XmlWriteMode.WriteSchema)
    2.         Dim xmlSW2 As System.IO.StreamWriter = New System.IO.StreamWriter("Winners.xml")
    3.         Try
    4.             myDataset.WriteXml(xmlSW2, XmlWriteMode.WriteSchema)
    5.         Catch ex As Exception
    6.             MessageBox.Show(ex.Message)
    7.         Finally
    8.             xmlSW2.Flush()
    9.             xmlSW2.Close()
    10.         End Try

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    I have not created xml file but I searched this forum and I got some code which I put to show how you can do it in the for each loop. This only writes the customer name but you can add address and all the info in the loop.
    VB Code:
    1. Imports System
    2. Imports System.Xml
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub GetRandomCustomers()
    7.  
    8.         Dim myDataset As New DataSet()
    9.  
    10.  
    11.         'Assign the store numbers to a string array
    12.         Dim Stores As String() = { _
    13.             CStr(1101), _
    14.             CStr(1115), _
    15.             CStr(1116), _
    16.             CStr(1121), _
    17.             CStr(1126), _
    18.             CStr(1129)}
    19.  
    20.         Dim rendomCustQueue As New Queue
    21.         Dim CustomerPicker As New Random
    22.         Dim myrows As DataRow()
    23.         Dim value As Integer
    24.  
    25.         ' Create the XmlDocument.
    26.         Dim doc As XmlDocument = New XmlDocument()
    27.         doc.LoadXml("<item><name>wrench</name></item>")
    28.  
    29.         'Loop through each store
    30.         For Each storeNum As String In Stores
    31.             'Get all the rows from specified store by "storeNum"
    32.             myrows = myDataset.Tables(0).Select("LastStore = '" & storeNum & "'")
    33.             'Checks to see if the store has any customer records.
    34.             'If not go to next store.
    35.             If myrows.Length > 0 Then
    36.                 'Use the rows count from the particular store to get a random number
    37.                 value = CustomerPicker.Next(0, myrows.Length)
    38.                 'Use the random number as an index in "myrows"
    39.                 'to get the customer name and write it on xml file.
    40.                 'Add a customer name element.
    41.                 Dim newElem As XmlElement = doc.CreateElement("FullName")
    42.                 newElem.InnerText = CStr(myrows(value).Item("FullName"))
    43.                 doc.DocumentElement.AppendChild(newElem)
    44.             End If
    45.             'Go to the next store
    46.         Next
    47.         ' Save the document to a file and auto-indent the output.
    48.         Dim writer As XmlTextWriter = New XmlTextWriter("data.xml", Nothing)
    49.         writer.Formatting = Formatting.Indented
    50.         doc.Save(writer)
    51.  
    52.     End Sub
    53. End Class

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    or if you want to use dataset for writing xml file then this is what you want
    VB Code:
    1. Private Sub GetRandomCustomers()
    2.  
    3.         Dim myDataset As New DataSet()
    4.         'Assign the store numbers to a string array
    5.         Dim Stores As String() = { _
    6.             CStr(1101), _
    7.             CStr(1115), _
    8.             CStr(1116), _
    9.             CStr(1121), _
    10.             CStr(1126), _
    11.             CStr(1129)}
    12.  
    13.         Dim rendomCustQueue As New Queue
    14.         Dim CustomerPicker As New Random
    15.         Dim myrows As DataRow()
    16.         Dim value As Integer
    17.  
    18.         'create a new dataset
    19.         Dim ds As New DataSet
    20.         ds = myDataset.Copy
    21.         ds.Clear()
    22.  
    23.         'Loop through each store
    24.         For Each storeNum As String In Stores
    25.             'Get all the rows from specified store by "storeNum"
    26.             myrows = myDataset.Tables(0).Select("LastStore = '" & storeNum & "'")
    27.             'Checks to see if the store has any customer records.
    28.             'If not go to next store.
    29.             If myrows.Length > 0 Then
    30.                 'Use the rows count from the particular store to get a random number
    31.                 value = CustomerPicker.Next(0, myrows.Length)
    32.                 'Use the random number as an index in "myrows"
    33.                 'to get the wining customer data row and adds it to a new dataSet.
    34.                 ds.Tables(0).Rows.Add(myrows(value))
    35.             End If
    36.             'Go to the next store
    37.         Next
    38.  
    39.         'write ds to xml.
    40.         ds.WriteXml("c:\Customers.xml", XmlWriteMode.WriteSchema)
    41.         Dim xmlSW2 As System.IO.StreamWriter = New System.IO.StreamWriter("Winners.xml")
    42.         Try
    43.             ds.WriteXml(xmlSW2, XmlWriteMode.WriteSchema)
    44.         Catch ex As Exception
    45.             MessageBox.Show(ex.Message)
    46.         Finally
    47.             xmlSW2.Flush()
    48.             xmlSW2.Close()
    49.         End Try
    50.     End Sub

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    I am really trying to do this without having to come back here for help on every step of the way but this has (obviously) been kicking my butt. I had to change my Sub slightly from the way I had it last night so I don't know if that is causing the problem or it is something else. Here is the entire sub.
    VB Code:
    1. Private Sub GetCustomers()
    2.         Dim myDataset As New DataSet()
    3.         Dim strConn As String = _
    4.             "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    5.             "Data Source=" & strFileName & ";" & _
    6.             "Extended Properties=""Excel 8.0;"""
    7.         Dim myData As New OleDbDataAdapter("SELECT " & _
    8.             "FullName, " & _
    9.             "V_ADDRESS as Address, " & _
    10.             "V_CITY as City, " & _
    11.             "V_STATE as State, " & _
    12.             "V_ZIP as Zip, " & _
    13.             "[Last Visit] as LastVisit, " & _
    14.             "V_LASTSTOR as LastStore " & _
    15.             "From [Mailing List$] WHERE " & _
    16.             "(V_STATE = 'MI') AND " & _
    17.             "(Len(V_ZIP) >= 5) AND " & _
    18.             "(V_ADDRESS <> '') AND " & _
    19.             "(V_CITY <> '') AND " & _
    20.             "(FullName <> '')", strConn)
    21.  
    22.         myData.TableMappings.Add("Table", "CustomerList")
    23.         myData.Fill(myDataset)
    24.         dgvCustomer.DataSource = myDataset.Tables(0).DefaultView
    25.        
    26.         Dim Stores As String() = { _
    27.             CStr(1101), _
    28.             CStr(1115), _
    29.             CStr(1116), _
    30.             CStr(1121), _
    31.             CStr(1126), _
    32.             CStr(1129)}
    33.  
    34.         Dim rendomCustQueue As New Queue
    35.         Dim CustomerPicker As New Random
    36.         Dim myrows As DataRow()
    37.         Dim value As Integer
    38.  
    39.         'create a new dataset
    40.         Dim ds As New DataSet
    41.         ds = myDataset.Copy
    42.         ds.Clear()
    43.  
    44.         'Loop through each store
    45.         For Each storeNum As String In Stores
    46.             'Get all the rows from specified store by "storeNum"
    47.             myrows = myDataset.Tables(0).Select("LastStore = '" & storeNum & "'")
    48.             'Checks to see if the store has any customer records.
    49.             'If not go to next store.
    50.             If myrows.Length > 0 Then
    51.                 'Use the rows count from the particular store to get a random number
    52.                 value = CustomerPicker.Next(0, myrows.Length)
    53.                 'Use the random number as an index in "myrows"
    54.                 'to get the wining customer data row and adds it to a new dataSet.
    55.                 ds.Tables(0).Rows.Add(myrows(value))
    56.             End If
    57.             'Go to the next store
    58.         Next

    The error is This row already belongs to another table.


  16. #16
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection

    Hey I am not tired to help you so you come back and ask as much as you like, it is OK with me. Now the error spouse to happen because the row we were tring to add is already a row for the other table so what I did here is to copy the row into a new “ds.Table” row and then add the now row to the “ds.Table”. Unfortunately I can’t run the code in my computer. I don’t have the customers’ data but try this to see if it works.
    VB Code:
    1. 'create a new dataset
    2.         Dim ds As New DataSet
    3.         ds = myDataset.Copy
    4.         ds.Clear()
    5.         Dim dr As DataRow
    6.         'Loop through each store
    7.         For Each storeNum As String In Stores
    8.             'Get all the rows from specified store by "storeNum"
    9.             myrows = myDataset.Tables(0).Select("LastStore = '" & storeNum & "'")
    10.             'Checks to see if the store has any customer records.
    11.             'If not go to next store.
    12.             If myrows.Length > 0 Then
    13.                 'Use the rows count from the particular store to get a random number
    14.                 value = CustomerPicker.Next(0, myrows.Length)
    15.                 dr = ds.Tables(0).NewRow
    16.                 dr.ItemArray = myrows(value).ItemArray
    17.                 'Use the random number as an index in "myrows"
    18.                 'to get the wining customer data row and adds it to a new dataSet.
    19.                 ds.Tables(0).Rows.Add(dr)
    20.             End If
    21.             'Go to the next store
    22.         Next

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection

    VBDT you nailed it. Thank you very much! With your help I was able to complete this project and get the letters out on time. I'll send you a virtual drink of your choice. Cheers!
    Last edited by FastEddie; Sep 7th, 2006 at 02:29 PM.

  18. #18
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Create a dataset from a Queue Collection (Resolved)

    Well, that should be “Skyy Vodka” or tequila
    I am glad it worked.
    Cheers!

  19. #19
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Create a dataset from a Queue Collection (Resolved)

    You can write a dataset to xml with one call.

    VB Code:
    1. myDataSet.WriteXml("C:\MyXML.xml")

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection (Resolved)

    Here's to ya!

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create a dataset from a Queue Collection (Resolved)

    JC thanks for the shortcut. I thought I was going to need the SCHEMA with my file but it turns out I didn't so your one liner works perfectly.
    Last edited by FastEddie; Sep 7th, 2006 at 03:48 PM.

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