Solved

ado.net question to find specific row

Posted on 2008-10-09
13
246 Views
Last Modified: 2012-05-05
I have this datareader connection below.  How can I get first row or second or third?

     Try
Dim conn As New Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings("connectionUSSL"))
           
       conn.Open()
        Dim cmd As New Data.SqlClient.SqlCommand("dbo.wmItemOptionGroupsbyLevelSp", conn)
        cmd.CommandType = CommandType.StoredProcedure

        'cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Request.QueryString("id").ToString()
        Dim dr As Data.SqlClient.SqlDataReader = Nothing
 
        dr = cmd.ExecuteReader()
 
       While dr.Read()
 
           Session("description") = dr("description").ToString()
         
           
       End While
 
        conn.Close()
        conn.Dispose()
Catch
End Try
0
Comment
Question by:gianitoo
[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
  • 6
  • 4
  • 3
13 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22679565
One method is to put your results into a DataSet/DataTable: http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx

If you define what the primary key column(s) on DataTable you can use its functionality to find specific row based on id value.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22679616
Here is an example from one of my programs to help with concept:
Dim columns(2) As DataColumn, dtKeys(1) As DataColumn
Dim dt As New DataTable()
Dim dr As DataRow
 
columns(0) = New DataColumn("ID", Type.GetType("System.String"))
columns(1) = New DataColumn("Value", Type.GetType("System.Double"))
dtKeys(0) = columns(0)
 
dt.Columns.AddRange(columns)
dt.PrimaryKey = dtKeys
 
dr = dt.Rows.Find("MyID")

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22679801
While dr.Read() is a loop, if you still want to keep SqlDataReader, then you can do something like this:

Dim i As Integer
If (dr.HasRows) Then 'Always to check before retrieving
  While dr.Read() 
   If (i = 1) Then ' i=1 for the second row, i=2 third row, etc
     Session("description") = dr("description").ToString()   
   End If
   i += 1       
  End While
End If

Open in new window

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

 
LVL 22

Expert Comment

by:prairiedog
ID: 22679822
Sorry, forgot to assign value to "i"
Dim i As Integer = 0

Dim i As Integer = 0
If (dr.HasRows) Then 'Always to check before retrieving
  While dr.Read() 
   If (i = 1) Then ' i=1 for the second row, i=2 third row, etc
     Session("description") = dr("description").ToString()   
   End If
   i += 1       
  End While
End If

Open in new window

0
 
LVL 1

Author Comment

by:gianitoo
ID: 22681946
when i use this
While dr.Read()
            Session("description") = dr("description").ToString()
       End While
I get a result
when i use that one below i get zero results ????

Dim i As Integer = 0
If (dr.HasRows) Then 'Always to check before retrieving
  While dr.Read()
   If (i = 1) Then ' i=1 for the second row, i=2 third row, etc
     Session("description") = dr("description").ToString()  
   End If
   i += 1      
  End While
End If
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22681993
"if (i=1) Then" is looking for the second record in dr, but if you have only one record, then you will get nothing.
0
 
LVL 1

Author Comment

by:gianitoo
ID: 22682007
i have 4 records
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22682077
Set a break point on While dr.Read() to see how the code is executed.
Also add Exit While after Session so you don't have to loop through the whole set if you find the row.
The code works for me.

Dim i As Integer = 0
If (dr.HasRows) Then 'Always to check before retrieving
  While dr.Read() 
   If (i = 1) Then ' i=1 for the second row, i=2 third row, etc
     Session("description") = dr("description").ToString()  
     Exit While 
   End If
   i += 1       
  End While
End If

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22682200
Debugging would be good as you have a TRY/CATCH in your code above, so if you are getting a SQL exception of other error you will not see it since CATCH block is empty.

Removing that or adding a trace write there will help troubleshoot as well.
0
 
LVL 1

Author Comment

by:gianitoo
ID: 22682243
This is my connection below.  ur code is not running and i can tell because i added else statement and still get no data ????

Dim conn As SqlConnection = ConfigurationManager.ConnectionStrings("test").ConnectionString
           
        conn.Open()
        Dim cmd As New Data.SqlClient.SqlCommand("wmItemOptionGroupsbyLevelSp", conn)
        cmd.CommandType = CommandType.StoredProcedure

        'cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Request.QueryString("id").ToString()
        Dim dr As Data.SqlClient.SqlDataReader = Nothing
 
        dr = cmd.ExecuteReader()
 
        'While dr.Read()
 
        '    Label1.Text = dr("description").ToString()
         
           
        'End While
        Dim i As Integer = 0
        If (dr.HasRows) Then 'Always to check before retrieving
            While dr.Read()
                If (i = 4) Then ' i=1 for the second row, i=2 third row, etc
                    Session("description") = dr("description").ToString()
                    Exit While
                End If
                i += 1
            End While
        Else
            Label1.Text = "no data"
        End If

 
        conn.Close()
        conn.Dispose()
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22682320
You only have 4 records, but i=4 is looking for the fifth record, that it why.
Option 1: Change i=4 to i=3
Option 2: Dim i As Integer = 1
0
 
LVL 1

Author Comment

by:gianitoo
ID: 22682362
this is what i have and nothing yet
  Dim i As Integer = 1
        If (dr.HasRows) Then 'Always to check before retrieving
            While dr.Read()
                If (i = 3) Then ' i=1 for the second row, i=2 third row, etc
                    Session("description") = dr("description").ToString()
                    Exit While
                End If
                i += 1
            End While
        Else
            Label1.Text = "no data"
        End If
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22682414
Did you use the break point as I mentioned before to step into your code?
0

Featured Post

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.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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