Please HELP!

Discussion in 'C#' started by JEP_Dude, Jul 24, 2009.

  1. #1
    Hey there....

    Please help. I've typed in a program from a book on VB.Net. I've received an error stating, "ExecuteReader requires an open and available Connection. The connection's current state is closed."

    What should I do to correct this? The program that follows generates this error on line, " dr = SQLcmd.ExecuteReader(CommandBehavior.CloseConnection)
    ".

    -----------------

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Data.OleDb

    Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim sqlConnString As String = "Initial Catalog=pubs:Integrated Sceurity=SSPI"

    If Not Page.IsPostBack Then
    Dim conn As New SqlConnection(sqlConnString)
    Dim sql As String = "SELECT * FROM Authors"
    Dim SQLcmd As New SqlCommand(sql, conn)
    Dim dr As SqlDataReader

    Try
    conn.Open()
    Catch ex As Exception
    Response.Write(ex.Message)
    End Try

    ' Read data into the DataReader
    dr = SQLcmd.ExecuteReader(CommandBehavior.CloseConnection)

    ' Show all the records in the ListBox
    Do While dr.Read
    Dim Total As Integer = dr.FieldCount - 1

    ' Get the entire set of data at once.
    Dim AllData(Total) As Object
    dr.GetValues(AllData)

    Dim i As Integer
    Dim n As String

    For i = 0 To Total ' Show one reconrd
    n = dr.GetName(i)
    ListBox1.Items.Add(n & ": " & AllData(i).ToString)
    Next i

    ListBox1.Items.Add("______________") ' line to divide records
    Loop ' More to next record
    End If
    End Sub
    End Class

    ----------

    Any help you can give that MAY assist would be greatly appreciated! Thanks for your time.

    May you have a blessed day as the Lord wills.
     
    JEP_Dude, Jul 24, 2009 IP
  2. EmeraldOne

    EmeraldOne Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    DataReader needs to have open connection while looping..
    Don't close the connection when calling the ExecuteReader.
    After the While Read, close the reader and than close the connection if you want...

    in .NET you don't have to be worried about open connection since you have the garbage collector.

    Isaac,
    http://www.7Live7.com
     
    EmeraldOne, Jul 25, 2009 IP
  3. JEP_Dude

    JEP_Dude Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your reply.

    I have another question. Allow me to illustrate the process of the website that I plan to design....

    !) A third party app' collects data from an online source and stores the basic data on a dedicated server.

    2) A VB.Net 2008 app' analyzes the content of this data and transforms it into a SQL DB.

    3) An ASP.Net 2008 app' displays the content of this SQL DB online.

    Can ASP.Net display the DB while the VB.Net app' is updating this same DB?

    May you hae a blessed day as the Lord wills.

    JEP_Dude
     
    JEP_Dude, Jul 25, 2009 IP
  4. asmaster

    asmaster Peon

    Messages:
    86
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What do you mean? If you mean using SELECT and UPDATE at one time, it's certainly possible.
     
    asmaster, Jul 27, 2009 IP
  5. JEP_Dude

    JEP_Dude Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I do plan to read in the data from an SQL DB into a large array. Then I'd like to display this information via ASP.Net online.

    I'm new to this. If it were to read in the data via a SELECT statement, would ASP.Net need to repeat that same SQL SELECT statement each time a visitor comes to the website? Because I would like to minimize the overall DB needs.

    May you have a blessed day as the Lord wills.

    JEP_Dude
     
    JEP_Dude, Jul 27, 2009 IP
  6. certifications4you

    certifications4you Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you need an active connection while reading by SqlDataReader.
     
    certifications4you, Aug 4, 2009 IP
  7. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You could either add a refresh button to the form or have the page auto-refresh every n minutes
     
    Free Born John, Aug 10, 2009 IP