Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving data using a datareader and stored procedure

Posted on 2009-04-01
3
Medium Priority
?
680 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

715 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