Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Just trying to get the correct syntax for reading the data so that it it available so that I can call the function....

I've seen the data read like..

.CompanyName = CStr(nwDataReader.GetString(1))
.ContactName = CStr(nwDataReader.GetString(2))

Etc.


I have approx 30 columns to read, do I have to call each line in the function in the above manner or is there a better way?





PREVIOUS POST:


I am creating my base class and calling a function to retrieve the data. If I read the data in the following format, will I be able to call the function in the code behind page?
VB
Public Function GetPortfolioInfo(ByVal Contract As String) As PortfolioInfo

Dim Reader...
Dim PortfolioInfo...
Dim Conn..

Do while Read
If IsDBNull(rd("ContractDesc")) = True Then
                   _ContractDesc = ""
               Else : _ContractDesc = "" & CType(rd("ContractDesc"), String)
               End If

               If IsDBNull(rd("Status")) = True Then
                   _Status = ""
               Else : _Status = "" & CType(rd("Status"), String)
               End If

Return PortfolioInfo1
End Function
Posted
Updated 23-Jun-10 8:54am
v3

You are on the right track. However, I'm not sure what your data source is. I'll assume it is a database, and I'll use an SQLDataAdapter to get the data from a database and use it to fill a datatable. When the datatable is filled, specify the desired row index and the item index. In the example below, the row is set to 0, which is the first row in the database table. The item is also set to 0, which is the first column in the database table. If the item you are looking for is in the 4th column, set the item value to 3 (for example).

Dim SQLQuery as string = "Select * from dbTable"
Dim ConnectionString as string = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
Dim DT as DataTable = new system.data.datatable
DT= SQLToDT(ConnectionString, SQLQuery)
CompanyName = DT.rows(0).item(0).tostring





Public Function SQLToDT(ConnectionString As String, SQLQuery As String) As DataTable
    Dim myDTable as system.data.datatable
    Dim conn As New SqlConnection(ConnectionString )
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = new SqlCommand(SQLQuery , conn)
    adapter.Fill(myDTable)
    Return myDTable
End Function
 
Share this answer
 
It is not clear whether you are trying to retrieve a single record from the database or returning all records. In my opinion using a DataReader as you are trying to do is by far the best option, and the method you are using is pretty robust. When retrieving records I use this syntax:
VB
If Not IsDBNull(dbrProducts(1)) Then .Name = dbrProducts.GetString(1) Else .Name = "Default"
If Not IsDBNull(dbrProducts(2))Then.CostPrice = dbrProducts.GetDecimal(2) Else .CostPrice = 0
If Not IsDBNull(dbrProducts(3)) Then .PackSize = dbrProducts.GetInt32(3) Else .PackSize = 0
If Not IsDBNull(dbrProducts(4)) Then .RetailPrice = dbrProducts.GetDecimal(4) Else .RetailPrice = 0


just a different way of putting it is all maybe a liitle more concise and readable. Hope this helps.

Happy Coding
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900