Hello - I'm at an interim point in my learning, here.. I'm starting to understand "objects" and OOP and their roles. In fact, I'm finding out more and more that what I need is some kind of Data Manager or Data Access Class.
So - I tried something like this in it's simplest form. I wanted to make a "Function" that returns a Datareader.
My purpose was to make a reuseable function to fill forms on my site - this one is for account maintenance..
I will post the code and you guys can see what I was doing:
I don't even know where to start w/ questions.
I guess my MAIN one at this time is:
Why does it say my FormFill DataReader is closed when I try it's READ method?
Anyways - You guys probably see where I'm going with this - I want to learn how to make a reuseable data "class" to use within a value object I made. I was just messing around and tried to pass a DR via function to see what happens and I failed miserably..
So - I tried something like this in it's simplest form. I wanted to make a "Function" that returns a Datareader.
My purpose was to make a reuseable function to fill forms on my site - this one is for account maintenance..
I will post the code and you guys can see what I was doing:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Get Customer_Id from Session / DB / etc.
' Fake it for testing:
Dim Customer_Id As Integer = 4
' Check for Querystring - See if it's valid stuff
' Then do an action based on "action" parameter
' Actions correspond to forms - edit contact info, edit addresses, etc..
If Len(Request.QueryString("action")) < 1 Then
' No Action Specified - Do Nothing
Else
Select Case Request.QueryString("action")
Case "editcontact"
' Edit Contact Information
' Populate form from database
Do While FillForm(Customer_Id, Request.QueryString("action")).Read
' ^^^^ This is where I know I messed up!!! ^^^^
' Because I can't call Read without the ARGS
' And if I Dim dr as SQLDatareader and do:
' dr = FillForm(Customer_Id, Request.QueryString("action"))
' It still will not read..
Loop
End Select
End If
End Sub
Function FillForm(ByVal cust_id As Integer, ByVal action As String) As SqlDataReader
Dim con As New SqlConnection(ConfigurationSettings.AppSettings("connStr"))
Dim cmd As New SqlCommand("sp_Management", con)
cmd.CommandType = CommandType.StoredProcedure
With cmd
.Parameters.Add("@Customer_Id", SqlDbType.Int).Value = cust_id
.Parameters.Add("@Action", SqlDbType.VarChar, 50).Value = action
End With
' Start Trap for errors here
con.Open()
' We're trying to simply SELECT from DB to fill a form.
' Update will be handled somewhere else using parameters.
FillForm = cmd.ExecuteReader(CommandBehavior.CloseConnection)
' ******************
' * Object Cleanup *
' ******************
If con.State.Open Then
con.Close()
End If
If Not IsNothing(con) Then
con.Dispose()
con = Nothing
End If
If Not IsNothing(cmd) Then
cmd.Dispose()
cmd = Nothing
End If
End Function
I don't even know where to start w/ questions.
I guess my MAIN one at this time is:
Why does it say my FormFill DataReader is closed when I try it's READ method?
Anyways - You guys probably see where I'm going with this - I want to learn how to make a reuseable data "class" to use within a value object I made. I was just messing around and tried to pass a DR via function to see what happens and I failed miserably..