|
-
Mar 25th, 2006, 10:55 PM
#1
Thread Starter
Hyperactive Member
Create Array from column in dataset (Resolved)
I want to create an array from a column in a dataset. Basicaly I want to put all the Zipcodes from the ueZip field into an array. Is that possible? My goal is to use what I learned on this thread Question about Array with an array created from a dataset instead of a hardcoded one as I do there.
VB Code:
Dim strSelect2 As String = _
"Select ueStore, ueAddress, ueCity, ueState, " & _
"ueZip From queUncEdsZCodes " & _
"Where Latitude <=" & HighLatitude & _
"And Latitude >=" & LowLatitude & _
"And Longitude <=" & HighLongitude & _
"And Longitude >=" & LowLongitude
Dim Cmd2 As New OleDbDataAdapter(strSelect2, conn)
Dim ds As New DataSet
Cmd2.Fill(ds, "ZipCodes")
Return ds
End Function
......Snip.....
Dim myDS As DataSet = GetUE_ZipRadius(tbZip.Text, CInt(tbRadius.Text))
DataGrid1.DataSource = myDS
DataGrid1.DataMember = "ZipCodes"
Last edited by FastEddie; Mar 26th, 2006 at 03:39 PM.
-
Mar 26th, 2006, 04:59 AM
#2
Re: Create Array from column in dataset
VB Code:
With ds.Tables("queUncEdsZCodes")
Dim zipCodes(.Rows.Count - 1) As String
For i As Integer = 0 To .Rows.Count - 1
zipCodes(i) = DirectCast(.Rows(i).Item("ueZip"), String)
Next
'or
Dim zips As New ArrayList
For Each d As Data.DataRow In .Rows
zips.Add(d.Item("ueZip"))
Next
End With
-
Mar 26th, 2006, 12:38 PM
#3
Thread Starter
Hyperactive Member
Re: Create Array from column in dataset
Like this? I get this error: Object reference not set to an instance of an object. It highlights this line zipCodesA(i) = DirectCast(.Rows(i).Item("ueZip"), String)
VB Code:
Dim strStaticZip As String = tbZip.Text
Dim myDS As DataSet = _
GetUE_ZipRadius(tbZip.Text, _
CInt(tbRadius.Text))
DataGrid1.DataSource = myDS
DataGrid1.DataMember = "ZipCodes"
With myDS.Tables("ZipCodes")
Dim zipCodesA(.Rows.Count - 1) As String
For i As Integer = 0 To .Rows.Count - 1
zipCodesA(i) = DirectCast(.Rows(i).Item("ueZip"), String)
LookUpZipCode(strStaticZip, zipCodesA(i))
ListBox1.Items.Add(Format(CalcDistance(), "F").ToString)
Next
End With
Last edited by FastEddie; Mar 26th, 2006 at 12:43 PM.
-
Mar 26th, 2006, 01:32 PM
#4
Re: Create Array from column in dataset
Is your 'GetUE_ZipRadius(tbZip.Text, CInt(tbRadius.Text))' function returning a valid dataset, that contains a table with the ueZip column?
As a test, you could comment out the line:
VB Code:
zipCodesA(i) = DirectCast(.Rows(i).Item("ueZip"), String)
and replace it with:
VB Code:
zipCodesA(i) = "12345" 'choose one of your valid zip codes
This will fill your array with this one value of course, but you will be able to see if the code runs.
-
Mar 26th, 2006, 02:35 PM
#5
Thread Starter
Hyperactive Member
Re: Create Array from column in dataset
Andy_P when I did as you suggest the error went away. I put together a quick form to show that the dataset is loading data into the datagrid and that there is a field called ueZip. Of course the distances are all the same because it is hardcoded.
I changed it back and I didn' think I made any other changes but now I am getting a different error. This is it,
-
Mar 26th, 2006, 03:20 PM
#6
Re: Create Array from column in dataset
Well to me that suggestes that the data contained in the ueZip field is of the Integer type, not String, and as such, DirectCast is complaining.
If that is the case, perhaps this might work:
VB Code:
With myDS.Tables("ZipCodes")
Dim zipCodesA(.Rows.Count - 1) As String
Dim intZip as Integer
For i As Integer = 0 To .Rows.Count - 1
intZip = DirectCast(.Rows(i).Item("ueZip"), Integer)
zipCodesA(i) = intZip.ToString
LookUpZipCode(strStaticZip, zipCodesA(i))
ListBox1.Items.Add(Format(CalcDistance(), "F").ToString)
Next
End With
-
Mar 26th, 2006, 03:38 PM
#7
Thread Starter
Hyperactive Member
Re: Create Array from column in dataset
Thanks Andy P when I tried that the IDE gave me an Option Strict warning about String to Integer type conversions and suggested this, which works (I love VS 2005).
zipCodesA(i) = CStr(DirectCast(.Rows(i).Item("ueZip"), Integer))
Thanks again for pointing me in the right direction.
-
Mar 26th, 2006, 03:42 PM
#8
Re: Create Array from column in dataset (Resolved)
No prob.
Could you mark the thread as resolved if all is ok.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|