Solved

Retrieving data using a datareader and stored procedure

Posted on 2009-04-01
3
676 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

632 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