-
November 24th, 2010, 12:15 PM
#1
Using DataTable populated with a Sqldataadapter
Hello everyone,
I am fairly inexperienced with using datatables in VB. My question is this: Can I access a particular column of a table by name if it is not defined elsewhere like when I populate a datatable using sqldataadapter.
Example:
Code:
Dim dtTempTable As New DataTable
Try
Using sqlCmd As New SqlCommand
sqlCmd.Connection = sqlConn
With sqlCmd
.CommandType = CommandType.Text
.CommandText = "SELECT * FROM CustData ORDER BY CustID"
.CommandTimeout = 300
End With
Using daAdapter As New SqlDataAdapter
daAdapter .Fill(dtTempTable )
End Using
End Using
So can I now access a particular column of a row using the column name?
for example:
Code:
For Each drRow In dtTempTable.Rows
tmpCustID = drRow.("CustID")
Next
Is this the way it can be done? I believe this is the case, but can't find any definitive examples online (probably not searching properly)
Thanks,
Hawk
-
November 24th, 2010, 01:44 PM
#2
Re: Using DataTable populated with a Sqldataadapter
You need the SELECTED ITEM #, so it doesn't always show only the LAST record in the recordset.
-
November 24th, 2010, 01:50 PM
#3
Re: Using DataTable populated with a Sqldataadapter
Thanks for the reply, but not sure I follow. Would it not show the record of the row I am on?
-
November 24th, 2010, 01:51 PM
#4
Re: Using DataTable populated with a Sqldataadapter
Not unless you stop the loop at the row that you're at.
-
November 24th, 2010, 01:59 PM
#5
Re: Using DataTable populated with a Sqldataadapter
Oh I think I follow. That was just some code I whipped up to illustrate what I want to do.
Here's what I want
Code:
For Each drRow In dtTempTable.Rows
tmpCustID = drRow.("CustID")
'No do stuff with this record
'finished with this record, move on to the next one
Next
What I was looking for was confirmation that using the above syntax, would the value of the current row I am on, pass the value at column "CustID" to the variable tmpCustID? Or do I need to get the data of that column at that row a differnent way?
Thanks again
-
November 24th, 2010, 03:47 PM
#6
Re: Using DataTable populated with a Sqldataadapter
In VB, the combobox's value can be retrieved several ways. x=combobox.selectedvalue will return a row like the error says. What intellisense doesn't tell you is that you can retrieve the information from this row using .Item(0) so x=combobox.selectedvalue.item(0) will give you the value including the type like integer, etc.
posted the link in the other thread
No need to loop...
-
November 24th, 2010, 05:09 PM
#7
Re: Using DataTable populated with a Sqldataadapter
Thanks for the help. Much appreciated!
-
November 24th, 2010, 06:02 PM
#8
Re: Using DataTable populated with a Sqldataadapter
This is late binding and can give a runtime error if the field doesn't exist. I think the syntax is drRow.Item("CustID") or drRow.Cell("CustID"). I'm not at my computer with VS installed.
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
|