Sushil Chordia said:
Are you using the same parameter object in two different
ParameterCollection:?
Actually, this is contained in an object. I am calling the same Object
twice, but I am recreating the SqlCommand object (I think).
Here is a page that calls the MyDbObject twice with the same parameters.
***************************************************************
sub Resume_Click(sender as Object, e as eventArgs)
Dim TemplateID as Integer
Dim Title as String
Dim myDbObject as new DbObject()
Dim dbReader As SqlDataReader
Dim parameters As SqlParameter () = { _
New SqlParameter("@UserID",SqldbType.VarChar,20) }
parameters(0).value = session("UserID")
dbReader = myDbObject.RunProcedure("GetDefaultTemplate", parameters)
if dbReader.Read then
Session("TemplateID") = dbReader("ResumeTemplateID")
response.redirect("DisplayTemplate.aspx?s=1")
else
dbReader = myDbObject.RunProcedure("GetTemplate",parameters)
if dbReader.Read then
response.redirect("Template.aspx")
end if
end if
end sub
********************************************************************
The Object is:
**********************************************************************
Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace MyFunctions
Public Class DbObject
Protected myConnection As SqlConnection
Private myConnectionString As String
Public Sub New()
myConnectionString =
System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING
_ft")
myConnection = New SqlConnection(myConnectionString)
End Sub
Public Overloads Function RunProcedure( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlDataReader
Dim returnReader As SqlDataReader
if myConnection.State = ConnectionState.Open then
myConnection.Close()
end if
myConnection.Open()
Dim command As SqlCommand = _
BuildQueryCommand(storedProcName, parameters)
command.CommandType = CommandType.StoredProcedure
returnReader = command.ExecuteReader( _
CommandBehavior.CloseConnection)
' Connection will be closed automatically
Return returnReader
End Function
' Properties
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
' Protected property that exposes the connection string to inheriting
' classes. Read-Only.
' ---
Protected ReadOnly Property ConnectionString() As String
Get
Return myConnectionString
End Get
End Property
' Builds a SqlCommand designed to return a SqlDataReader,
' and not an actual integer value.
' ----
Private Function BuildQueryCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand
Dim command As New SqlCommand(storedProcName, myConnection)
command.CommandType = CommandType.StoredProcedure
Dim parameter As SqlParameter
For Each parameter In parameters
command.Parameters.Add(parameter)
Next
Return command
End Function
End Class
End Namespace
**********************************************************************
The BuildQueryCommand does a:
Dim command As New SqlCommand(storedProcName, myConnection)
each time I call call MyDbObject.RunProcedure().
What do I need to do to prevent this error, if I call the object multiple
times?
Thanks,
Tom