Solved

Search a value in a datareader

Posted on 2004-10-01
8
823 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

624 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