Solved

Search a value in a datareader

Posted on 2004-10-01
8
822 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - String Manipulation Scenario 2 45
Run software updates from the website 6 50
Make borderless form movable by user 2 27
vb.net searchandselect 12 17
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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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