Solved

Retrieving data using a datareader and stored procedure

Posted on 2009-04-01
3
675 Views
Last Modified: 2013-11-05
Hi,

I'm using VS 2008 and SQL server (sqldatasource1) on Win XP Professional coding in VB.net.

I want to create a datareader that uses a stored procedure as the datasource. This datareader will contain the data from the stored procedure which I will use to display the data from the fieldnames in labels. Could someone show me how to code this?

The stored proc is called SearchOrg

My labels are OrgName, OrgLocation, OrgManager

Someone helped me with this, but didn't give more detail due to time constraints....


Code:
Dim dr as New Datareader
Dim cmd as New SqlCommand
(I dont know if I should use SqlCommand or SqlDatasource, please advise)

For the labels

Code:
lblOrgName.Text = dr("fieldname").ToString




I saw the following code in vs2008 dynamic help, but I really dont' understand it, where to put it or how to bind the reader to my labels.


Code:
Private Sub HasRows(ByVal connection As SqlConnection)
    Using connection
        Dim command As SqlCommand = New SqlCommand( _
          "SELECT CategoryID, CategoryName FROM Categories;", _
          connection)
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        If reader.HasRows Then
            Do While reader.Read()
                Console.WriteLine(reader.GetInt32(0) _
                  & vbTab & reader.GetString(1))
            Loop
        Else
            Console.WriteLine("No rows found.")
        End If

        reader.Close()
    End Using
End Sub

I'd really appreciate your help!
0
Comment
Question by:NorthArrow
[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
3 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24045339
Please post your stored procedure SearchOrg
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24046246
have a look at below code I did in windows application using vb.net. [getEmpList] is my SP name which return all the list of empolyee. if you want to look at SQL SP and table. it is pasted here and vb.net code is in code snippet panel below.  If you have given your SP than I would have done VB.NET code for your SP only but you can modify the code given in code snippet panel.


--create table
USE [adventureworks]
GO
CREATE TABLE [dbo].[emps](
    [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Dept] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Company] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

--insert records
INSERT INTO emps
SELECT 'rITESH','MIS','ECHEM' UNION ALL
SELECT 'Rajan','MIS','mar'

--creating stored procedure which return emp name
CREATE PROC [dbo].[getEmpList]
AS
SELECT Name FROM emps

--checking SP
EXEC getemplist



 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection
        conn.ConnectionString = "Data Source=.;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=sa"
        conn.Open()
 
        Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
        cmd.CommandType = System.Data.CommandType.StoredProcedure
        cmd.Connection = conn
        cmd.CommandText = "[getEmpList]"
 
        Dim reader As System.Data.SqlClient.SqlDataReader
        reader = cmd.ExecuteReader
        Dim XPos As Integer = 0
        Dim YPos As Integer = 0
 
        Do While reader.Read()
            XPos = 9
            YPos = YPos + 35
 
            Dim lblfieldname As System.Windows.Forms.Label = New System.Windows.Forms.Label
            lblfieldname.Text = reader("Name").ToString()
            lblfieldname.Location = New System.Drawing.Point(XPos, YPos)
            lblfieldname.Size = New System.Drawing.Size(120, 23)
 
            Me.Controls.Add(lblfieldname)
            XPos = XPos + 20
            YPos = YPos + 35
 
        Loop
 
    End Sub

Open in new window

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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