Solved

Search a value in a datareader

Posted on 2004-10-01
8
817 Views
Last Modified: 2008-03-17
What is the best way to search a value (string type)  in a datareader (all columns) ?
0
Comment
Question by:vbhaha
  • 5
  • 3
8 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12205861
If you want to use reader, why don't you set the SQL query with proper WHERE clause ?
-Baan
0
 

Author Comment

by:vbhaha
ID: 12205909
Because I wanted to use datareader as any temp array for searching different values.
If I cannot use datareader, would you tell me what can I use and give me some example please !

Thanks
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12205934
Sure, please post the code u are using to read data into DataReader. Then it will be easier for me to give you an exact answer.
0
 

Author Comment

by:vbhaha
ID: 12205947
Here you're:

        Dim cmd As New SqlClient.SqlCommand
        Dim retValue as string
 
        Try
            cmd.Connection = APCnn
            cmd.CommandText = "select * from " + MappingTableName + " where sys_id='" & SysID & "'"
            drTableMapping = cmd.ExecuteReader()
            If Not drTableMapping.Read Then
                Throw New Exception("No records in mapping table")
            End If
            Do While dr.Read()
                if dr(1) = "value" then
                    retValue = dr(2)
                endif
            Loop
        Catch ex As Exception
            Throw New Exception(ex.Message, ex)
        End Try
        return retValue

Thnaks a lot.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12205973
There are lot of mistakes, but I will come to those later. Well, let me know what you are trying to do.
>> if dr(1) = "value" then <<
So what you are trying to do is, if you find "value" in DR(1) you want to return DR(2). What if you find the first match, do you still want to go looking for the next match ? What will happen to retValue if you do not have any match ?
-Baan
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12205977
One more question ... Why did you give me "C" in the last question you accepted ? I gave you the right answer I guess.
0
 

Author Comment

by:vbhaha
ID: 12205992
Yes you're right. I should add 'exit' after the first match.

Actually, this function is used for table name mapping, it input the key value and return the physical table name.
It should be something like that:

    Public Function GetTableName(ByVal inKey As String) As String
        Dim retValue As String = ""

        Try
           Do While dr.Read()
                if dr(1) = "value" then
                    retValue = dr(2)
                    exit do
                end if
            Loop        Catch ex As Exception
            Throw New Exception("The table mapping key '" + inKey.Trim + "' doesn't exist !")
        End Try
        Return retValue
    End Function

Thanks.
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 250 total points
ID: 12206019
If this is the case DataReader will work fine...

    Public Function GetTableName(ByVal inKey As String) As String
        Dim retValue As String = ""
        Try
           While dr.Read()
                if dr.GetString(1) = inKey.Trim then    'Use GetString method to read the data.
                    retValue = dr.GetString(2)
                    exit While
                end if
            End While
        Catch ex As Exception
            Throw New Exception("The table mapping key '" + inKey.Trim + "' doesn't exist !")
        End Try
        Return retValue
    End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now