Solved

Retrieving data using a datareader and stored procedure

Posted on 2009-04-01
3
668 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
3 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Please post your stored procedure SearchOrg
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
Comment Utility
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now