Monday, March 12, 2012

Retrieving Data

Hi

I've got a module that contains the following function.
Imports System.Data.SqlClient

Module SQL_Sprocs
Function ListUsers()

Dim conn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("DBConn"))

Dim cmd As SqlCommand = New SqlCommand("list_users", conn)
cmd.CommandType = CommandType.StoredProcedure
conn.Open()

Dim dr As SqlDataReader

dr = cmd.ExecuteReader

ListUsers = dr

dr.Close()
conn.Close()

End Function
End Module

I then want to call this function from my webform. So I'm using the following

Dim dr As SqlClient.SqlDataReader

dr = Timetracker.SQLProcs.ListUsers()

Do While dr.Read
Label1.Text &= dr("first_name") & " " & dr("last_name") & ", "
Loop

dr.Close()

But it's not working. I want to have a module that contains all my sprocs and be able to call them from the individual webpages.

What am I doing wrong?

LbobIt's also private.

You're returning a closed data reader.

dr = cmd.ExecuteReader

ListUsers = dr

dr.Close()
conn.Close()

-------

and you should never break apart the datalayer, and require a reader object to be return back to the primary worker process.

ghetto code.|||Can someone tell me then what I should do with stored procedures that get called from multiple places? In our current app we have a sql_procs.asp which contains all of them, we then use include on the required page and return the recordset.

Any ideas??|||You should never return a data reader to a presentation component, it keeps the connection open until you close, as you've just discovered. If you're looking for a simple solution I'd used a DataSet instead.

No comments:

Post a Comment