Class Beginner

Results 1 to 2 of 2

Thread: Class Beginner

  1. #1
    Join Date
    Dec 1969

    Default Class Beginner

    If I want to make a class, that have a connection to sql, and tha class have a sub that run a storeprocedure, how is that?<BR>I&#039;m newbie in oop. I&#039;ve tried this script, but it has an error.<BR><BR>Public Class Coba<BR> Private _NewsID as String<BR> Private _hari<BR> Private _ID as Integer<BR> Public Sub New(ID as Integer)<BR> Dim strConnect, strselect As String<BR> strConnect = "server=Denny;database=DBSiteXXX;" & _<BR> "uid=ABC;pwd="<BR> Dim objConnect As New SqlConnection(strConnect)<BR> strSelect = "spVData"<BR> Dim objCommand As sqlCommand<BR> objCommand = New SqlCommand(strSelect, objConnect) <BR> objCommand.CommandType = CommandType.StoredProcedure<BR> Dim Parm As SQLParameter<BR> Parm = objCommand.Parameters.Add("@IDData",SqlDbType.Int) <BR> Parm.Value = ID<BR> Dim objDataImage As SqlDataReader<BR><BR>***************************** *****************************<BR>&#039;ERROR IN THIS LINE, IT SAID THAT<BR>&#039;"System.InvalidOperationException: ExecuteReader <BR>&#039;Requires an open and available Connection. <BR>&#039;The connection&#039;s current state is Closed."<BR>&#039;If I put this script outside the class, <BR>&#039;it worked. But I Need it in class for some reason.<BR>&#039;Please help me, Thank you.<BR><BR> objDataImage = objCommand.ExecuteReader()<BR>******************** ***************************************<BR><BR> If ObjDataImage.Read then <BR> _Hari = ObjDataImage("CreateDate")<BR> End If<BR> ObjDataImage.Close()<BR> End Sub<BR> <BR> Public Readonly Property Hari as sqlcommand<BR> Get<BR> Return _Hari <BR> End Get<BR> End Property <BR>End Class<BR><BR>Sub Page_Load(Source As Object, E As EventArgs)<BR> Dim ID, Hari<BR> ID = 7<BR> Dim Kelas as New coba(ID)<BR> Hari = Kelas.Hari<BR> Response.Write(Hari)<BR>End Sub

  2. #2
    Join Date
    Dec 1969

    Default RE: Class Beginner

    open the connection before you call executeReader

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts