Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retrieving data using a datareader and stored procedure

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

916 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