Solved

Search a value in a datareader

Posted on 2004-10-01
8
819 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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