CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2007
    Posts
    56

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2007
    Posts
    56

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Using DataTable populated with a Sqldataadapter

    Not unless you stop the loop at the row that you're at.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Mar 2007
    Posts
    56

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Mar 2007
    Posts
    56

    Re: Using DataTable populated with a Sqldataadapter

    Thanks for the help. Much appreciated!

  8. #8
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    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
  •  





Click Here to Expand Forum to Full Width

Featured