Solved

Search a value in a datareader

Posted on 2004-10-01
8
816 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
Comment Utility
If you want to use reader, why don't you set the SQL query with proper WHERE clause ?
-Baan
0
 

Author Comment

by:vbhaha
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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