Results 1 to 33 of 33

Thread: All worked fine yesterday..sqldatareader yet again

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    All worked fine yesterday..sqldatareader yet again

    I asked a question yesterday and suddenly before I got down to the answer the app was working fine, went good through many tests...Now it blows up again complaining that the datareader cannot be read because it is closed...How so?

    here is the calling code:

    VB Code:
    1. Dim db As New Database.SQLDatabase("User ID=test;Password=test;Initial Catalog=QuikFix;Data Source=test;")
    2.  
    3.         Dim dr As SqlDataReader
    4.         Dim da As SqlDataAdapter
    5.         Dim ds As DataSet
    6.  
    7.         db.SetSQLDBCommand("select_ticket_by_id") 'pass procedure name
    8.         db.AddSQLDBCmdParameter("@TicketID", SqlDbType.BigInt, 3357) 'pass a parameter
    9.         dr = db.UseSQLDBExecuteReader() 'grab the data
    10.  
    11. [B]        While dr.Read()[/B]
    12.             MsgBox(dr("TicketID"))
    13.         End While
    14.         dr.Close()
    15.  
    16.         db.SetSQLDBCommand("select_ticket_ids") 'another procedure
    17.         db.AddSQLDBCmdParameter("@ClientID", SqlDbType.Int, 1) 'parameters
    18.         db.AddSQLDBCmdParameter("@FacilityID", SqlDbType.BigInt, 1)
    19.         db.AddSQLDBCmdParameter("@bOpen", SqlDbType.Bit, 1)
    20.         db.AddSQLDBCmdParameter("@bOnHold", SqlDbType.Bit, 1)
    21.  
    22.         dr = db.UseSQLDBExecuteReader() 'execute it
    23.  
    24.         While dr.Read()
    25.             MsgBox(dr("TicketID"))
    26.         End While
    27.         dr.Close()
    28.  
    29.         db.SetSQLDBCommand("select_ticket_by_id") 'procedure
    30.         db.AddSQLDBCmdParameter("@TicketID", SqlDbType.BigInt, 3357) 'param
    31.         ds = db.UseSQLDBDataSet() 'works with datasets as well
    32.  
    33.         Dim dd As New ComboBox
    34.         Controls.Add(dd)
    35.  
    36.         With dd
    37.             .DataSource = ds.Tables(0)
    38.             .DisplayMember = "TicketID"
    39.             .ValueMember = "TicketID"
    40.         End With

    It errors out in the first while loop when it tries to read dr.
    Ill post the SQLDatabase class in a seperate reply to this post since it is lengthy.

  2. #2

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    SQL Database class
    VB Code:
    1. Imports System.Data.SqlClient
    2. Public Class SQLDatabase
    3.     Private mSQLDBConnString As String
    4.     Private mSQLDBCmd As SqlCommand
    5.     Private mSQLDBConnector As SqlConnection
    6.     Public Sub New(ByVal pConnStr As String)
    7.         mSQLDBConnString = pConnStr
    8.         Try
    9.             mSQLDBConnector = New SqlConnection(pConnStr)
    10.             mSQLDBConnector.Open()
    11.         Catch ex As Exception
    12.             Throw New Exception(ex.ToString())
    13.         End Try
    14.     End Sub
    15.     Public Property SQLDBConnString()
    16.         Get
    17.             Return mSQLDBConnString
    18.         End Get
    19.         Set(ByVal Value)
    20.             mSQLDBConnString = Value
    21.         End Set
    22.     End Property
    23.     Public Sub CloseSQLDB()
    24.         If IsSQLDBOpen() Then
    25.             mSQLDBConnector.Dispose()
    26.             mSQLDBConnector.Close()
    27.             mSQLDBConnector = Nothing
    28.         End If
    29.     End Sub
    30.     Public Sub OpenSQLDB()
    31.         If Not IsSQLDBOpen() Then
    32.             If mSQLDBConnector Is Nothing Then
    33.                 'not even a valid database object
    34.                 'try to recreate the object, object recreated if a string exists
    35.                 If mSQLDBConnString.Length > 0 Then
    36.                     mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    37.                     mSQLDBConnector.Open()
    38.                 Else
    39.                     'no string
    40.                     Throw New Exception("Database null, please create a valid SQLDB object!")
    41.                     Exit Sub
    42.                 End If
    43.             Else
    44.                 mSQLDBConnector.Open()
    45.             End If
    46.         End If
    47.     End Sub
    48.     Public Function IsSQLDBOpen() As Boolean
    49.         Return mSQLDBConnector.State = ConnectionState.Open
    50.     End Function
    51.     Public Function SQLDBUsed() As String
    52.         Return mSQLDBConnector.Database
    53.     End Function
    54.     Public Sub SetSQLDBCommand(ByVal pStrSprocName As String)
    55.         If mSQLDBConnector Is Nothing  Then
    56.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    57.             mSQLDBConnector.Open()
    58.         else
    59.             If Not IsSQLDBOpen()  then
    60.                 mSQLDBConnector.Open()
    61.             end if
    62.         End If
    63.  
    64.         mSQLDBCmd = New SqlCommand(pStrSprocName, mSQLDBConnector)
    65.         With mSQLDBCmd
    66.             .CommandType = CommandType.StoredProcedure
    67.         End With
    68.     End Sub
    69.     Public Function UseSQLDBExecuteReader() As SqlDataReader
    70.         Dim lRead As SqlDataReader
    71.         If mSQLDBCmd Is Nothing Then
    72.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    73.             Exit Function
    74.         End If
    75.         If mSQLDBConnector Is Nothing  Then
    76.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    77.             mSQLDBConnector.Open()
    78.         else
    79.             If Not IsSQLDBOpen()  then
    80.                 mSQLDBConnector.Open()
    81.             end if
    82.         End If
    83.         Try
    84.             With mSQLDBCmd
    85.                 .Connection = mSQLDBConnector
    86.                 lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
    87.             End With
    88.             Return lRead
    89.         Catch ex As Exception
    90.                       Throw New Exception(ex.ToString())
    91.         Finally
    92.             mSQLDBCmd.Dispose()
    93.             mSQLDBCmd = Nothing
    94.             mSQLDBConnector.Close()
    95.             mSQLDBConnector = Nothing
    96.         End Try
    97.     End Function
    98.     Public Function UseSQLDBDataSet() As DataSet
    99.         Dim lDs As DataSet
    100.         Dim lDataAdapter As SqlDataAdapter
    101.         If mSQLDBCmd Is Nothing Then
    102.             Throw New Exception("Must use SetDBCommand to initialize SQLCommand object!")
    103.             Exit Function
    104.         End If
    105.         If mSQLDBConnector Is Nothing Then
    106.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    107.             mSQLDBConnector.Open()
    108.         Else
    109.             If Not IsSQLDBOpen() Then
    110.                 mSQLDBConnector.Open()
    111.             End If
    112.         End If
    113.         Try
    114.             With mSQLDBCmd
    115.                 lDataAdapter = New SqlDataAdapter
    116.                 With lDataAdapter
    117.                     .SelectCommand = mSQLDBCmd
    118.                 End With
    119.                 lDs = New DataSet
    120.                 lDataAdapter.Fill(lDs)
    121.             End With
    122.             Return lDs
    123.         Catch ex As Exception
    124.                        Throw New Exception(ex.ToString())
    125.         Finally
    126.             mSQLDBCmd.Dispose()
    127.             mSQLDBCmd = Nothing
    128.             lDs = Nothing
    129.             lDataAdapter = Nothing
    130.             mSQLDBConnector.Close()
    131.             mSQLDBConnector = Nothing
    132.         End Try
    133.     End Function
    134.     Public Sub UseSQLDBExecuteNonQuery()
    135.         If mSQLDBCmd Is Nothing Then
    136.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    137.             Exit Sub
    138.         End If
    139.         If mSQLDBConnector Is Nothing Then
    140.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    141.             mSQLDBConnector.Open()
    142.         Else
    143.             If Not IsSQLDBOpen() Then
    144.                 mSQLDBConnector.Open()
    145.             End If
    146.         End If
    147.         Try
    148.             With mSQLDBCmd
    149.                 .ExecuteNonQuery()
    150.             End With
    151.         Catch ex As Exception
    152.                         Throw New Exception(ex.ToString())
    153.         Finally
    154.             mSQLDBCmd.Dispose()
    155.             mSQLDBCmd = Nothing
    156.             mSQLDBConnector.Close()
    157.             mSQLDBConnector = Nothing
    158.         End Try
    159.     End Sub
    160.     Public Function UseSQLDBExecuteScalar() As Integer
    161.         Dim intNumRows As Integer
    162.         If mSQLDBCmd Is Nothing Then
    163.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    164.             Exit Function
    165.         End If
    166.         If mSQLDBConnector Is Nothing Then
    167.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    168.             mSQLDBConnector.Open()
    169.         Else
    170.             If Not IsSQLDBOpen() Then
    171.                 mSQLDBConnector.Open()
    172.             End If
    173.         End If
    174.         Try
    175.             With mSQLDBCmd
    176.                 intNumRows = CInt(.ExecuteScalar().ToString())
    177.             End With
    178.             UseSQLDBExecuteScalar = intNumRows
    179.         Catch ex As Exception
    180.                         Throw New Exception(ex.ToString())
    181.         Finally
    182.             mSQLDBCmd.Dispose()
    183.             mSQLDBCmd = Nothing
    184.             mSQLDBConnector.Close()
    185.             mSQLDBConnector = Nothing
    186.         End Try
    187.     End Function
    188.     Public Sub DeleteSQLDBCmd()
    189.         mSQLDBCmd.Dispose()
    190.         mSQLDBCmd = Nothing
    191.     End Sub
    192.     Public Sub AddSQLDBCmdParameter(ByVal pName As String, ByVal pType As SqlDbType, ByVal pVal As Object)
    193.         If mSQLDBCmd Is Nothing Then
    194.             Throw New Exception("Must use SetDBCommand to initialize SQLCommand object!")
    195.             Exit Sub
    196.         End If
    197.         If mSQLDBConnector Is Nothing Then
    198.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    199.             mSQLDBConnector.Open()
    200.         Else
    201.             If Not IsSQLDBOpen() Then
    202.                 mSQLDBConnector.Open()
    203.             End If
    204.         End If
    205.         Try
    206.             With mSQLDBCmd
    207.                 .Parameters.Add(pName, pType).Value = pVal
    208.             End With
    209.         Catch ex As Exception
    210.                        Throw New Exception(ex.ToString())
    211.         End Try
    212.     End Sub
    213.     Public Sub ClearSQLDBCmdParameters()
    214.         If mSQLDBCmd Is Nothing Then
    215.             Throw New Exception("Must use SetDBCommand to initialize SQLCommand object!")
    216.             Exit Sub
    217.         End If
    218.         With mSQLDBCmd
    219.             .Parameters.Clear()
    220.         End With
    221.     End Sub
    222. End Class

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    maybe add an
    VB Code:
    1. If Dr.HasRows then
    before you attempt to read it?

  4. #4

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Tried that before to but it complains that an invalid attempt was made when the reader was closed. I know there are rows though, when it calls the UseSQLDBExecuteReader() function in my class it pulls the data and in debug mode HasRows = True...
    I even ran the query in SQL Query Analyzer and it returned the row that I expected...

    Why does it complain the reader is closed ?

  5. #5

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Here is the strange thing...
    If I am in debug mode and I move the yellow arrow from the debug mode to the end try in that function:
    VB Code:
    1. Public Function UseSQLDBExecuteReader() As SqlDataReader
    2.         Dim lRead As SqlDataReader
    3.         If mSQLDBCmd Is Nothing Then
    4.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    5.             Exit Function
    6.         End If
    7.         If mSQLDBConnector Is Nothing  Then
    8.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    9.             mSQLDBConnector.Open()
    10.         else
    11.             If Not IsSQLDBOpen()  then
    12.                 mSQLDBConnector.Open()
    13.             end if
    14.         End If
    15.         Try
    16.             With mSQLDBCmd
    17.                 .Connection = mSQLDBConnector
    18.                 lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
    19.             End With
    20.             Return lRead
    21.         Catch ex As Exception
    22.                       Throw New Exception(ex.ToString())
    23.         Finally
    24.             mSQLDBCmd.Dispose()
    25.             mSQLDBCmd = Nothing
    26.             mSQLDBConnector.Close()
    27.             mSQLDBConnector = Nothing
    28.  
    29.         End Try
    30.     End Function
    So it skips the finally portion it works absolutely fine...so one would think ok let me try getting rid of the entire finally statement so I even tried this:

    VB Code:
    1. Public Function UseSQLDBExecuteReader() As SqlDataReader
    2.         Dim lRead As SqlDataReader
    3.         If mSQLDBCmd Is Nothing Then
    4.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    5.             Exit Function
    6.         End If
    7.         If mSQLDBConnector Is Nothing  Then
    8.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    9.             mSQLDBConnector.Open()
    10.         else
    11.             If Not IsSQLDBOpen()  then
    12.                 mSQLDBConnector.Open()
    13.             end if
    14.         End If
    15.         Try
    16.             With mSQLDBCmd
    17.                 .Connection = mSQLDBConnector
    18.                 lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
    19.             End With
    20.             Return lRead
    21.         Catch ex As Exception
    22.                       Throw New Exception(ex.ToString())
    23.         End Try
    24.     End Function

    Seems logical right? Wrong, it doesnt work with this either!!! Explain that one to me...it works ONLY when you move the debugger to the end try ?????

    Umm...WHAT!!~~ :|

  6. #6

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Klienma did that make sense what I posted, or are you as stumped as me ?

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    what happens if you step through your UseSQLDBExecuteReader method (using F8) one line at a time?

  8. #8

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Same thing, explain this a step by step F8 / F11 going through the debug line by line I get the same error, If I move the debugger by dragging and dropping the yellow line to the end try before the finally statement it works FINE...ending the code..
    If I delete the finally statement entirely and just run the application OR even step through the code line by line it FAILS!!!

    I dont get it at all..cant be the finally statement, it cant be deleting the finally statement, it only works when I move the debugger past it..it makes no sense...

  9. #9

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Do you run sql server?
    Can you try my code, simply compile the class into a dll, reference it in say a web form, create a simple web form with one button and place that code that I posted in my first thread of this topic. Simply replace the stored procedure name with one of your own procedures, etc...

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    Perhaps what is going on is the DataReader is being closed because you are closing the DB connection associated with it?

    That is the only thing that seems to make any sense. You are closing the connection to the database, which the DR needs, so the DR closes as well??

    That is just a guess, but it seems to make sense... set a watch on your lRead.IsClosed property when you are in your UseSQLDBExecuteReader, and see if it changes from open to closed as your finally runs. Also even if you remove the finally, the connection is going out of scope when your function ends, which would explain why even if you remove the finally it still happens?

  11. #11

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Nope not the case, I could go line by line and close the connection (note the code where I set the Command Behavior:

    mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)

    I could go line by line and LET IT close the db connection and then move the debugger to line to the end try and the function works fine, but if I remove that code of closing the db connection and run the code it errors out...driving me nuts !

  12. #12

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Quote Originally Posted by kleinma
    Perhaps what is going on is the DataReader is being closed because you are closing the DB connection associated with it?

    That is the only thing that seems to make any sense. You are closing the connection to the database, which the DR needs, so the DR closes as well??

    That is just a guess, but it seems to make sense... set a watch on your lRead.IsClosed property when you are in your UseSQLDBExecuteReader, and see if it changes from open to closed as your finally runs. Also even if you remove the finally, the connection is going out of scope when your function ends, which would explain why even if you remove the finally it still happens?
    Why would it work then if I move the debugger doesnt it still lose the scope since the function ends?
    That doesnt make any sense its valid when I move the debugger to the "end try" line of the finally statement without hitting the actual dispose and close methods...yet if I even comment them out and run the same code..it fails?

    How would you go around returing lRead then ? I do not want to maintain a connection to the db as this reduces the performance of this entire class...

  13. #13
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    I will see if I can run your code here and get a better idea of the issue...

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    you are using 1.1 or 2.0?

  15. #15

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Im running 1.1

  16. #16

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Any luck with it, you dont have to include all that code (the callee code front end / ui code) you can just keep the first part upto the first while loop...should compile and run with no issues. Just change the sproc name, and pass a parameter or not to it, I dont think it will matter if u use the parameter function, if your procedure expects one use it otherwise dont.

    Im still puzzled.

  17. #17
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    well what I have found so far is your DataReader closes when you call
    mSQLDBConnector.Close()
    in your finally section

  18. #18
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    I don't think there is a way to return a datareader without it's connection still open and alive... DataReaders aren't disconnected recordsets like a datatable is, so the reader needs the connection in order to dish records out.

    When someone creates an instance of your class, you open the DB right away, so why close it after they use one method of your class? Leave it open and have your class implement IDisposable. Then when they call dispose on your class, you close and kill off any objects that are still alive.

  19. #19

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    This doesn't make any sense it worked fine yesterday, same code...
    The CommandBehavior should handle the disconnection though.
    It still doesnt make sense about the entire app working when moving the debug portion down and then when I GET RID of the entire finally statement even the connection to the database why does it not work then ?

  20. #20

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Quote Originally Posted by kleinma
    I don't think there is a way to return a datareader without it's connection still open and alive... DataReaders aren't disconnected recordsets like a datatable is, so the reader needs the connection in order to dish records out.

    When someone creates an instance of your class, you open the DB right away, so why close it after they use one method of your class? Leave it open and have your class implement IDisposable. Then when they call dispose on your class, you close and kill off any objects that are still alive.
    But even if I DONT close the db (get rid of the entire finally statement) it does not work...
    So again I ask how does one return lRead...This was all production code last night, why would it suddenly not work. I think it goes deeper into what happens in the internals of the connection (pooling , etc).

  21. #21
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    ok, I found if you do this in your finally
    VB Code:
    1. Finally
    2.             mSQLDBCmd.Dispose()
    3.             mSQLDBCmd = Nothing
    4.             'mSQLDBConnector.Close()
    5.             'mSQLDBConnector = Nothing
    6.         End Try

    it works. The connection remains open and all looks well. When you close the returned datareader object, it will close the connection. That should sort things out for you right?

  22. #22

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    klienma if that is the case, lets assume we do keep the connection alive...get rid of the .close on the connection object, and the code still fails...so that is what I dont understand.

    To answer your second question why the connection is not kept alive, is for memory and performance reasons. If I were to dispose of the object I would need to reconnect and reestablish the database. The object may be persistant across an entire application, therefore for performance reasons the object is closed. THis is just the scenerio, but I'm more wondering why if you do not close the connection why it doesnt work still ?

  23. #23

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Doesnt work on my system I got rid of the closing and setting the db connection class to nothing and still no go ?

  24. #24

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Here's what I have:

    VB Code:
    1. Public Function UseSQLDBExecuteReader() As SqlDataReader
    2.         Dim lRead As SqlDataReader
    3.         If mSQLDBCmd Is Nothing Then
    4.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    5.             Exit Function
    6.         End If
    7.         If mSQLDBConnector Is Nothing  Then
    8.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    9.             mSQLDBConnector.Open()
    10.         else
    11.             If Not IsSQLDBOpen()  then
    12.                 mSQLDBConnector.Open()
    13.             end if
    14.         End If
    15.         Try
    16.             With mSQLDBCmd
    17.                 .Connection = mSQLDBConnector
    18.                 lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
    19.             End With
    20.            
    21.         Catch ex As Exception
    22.                       Throw New Exception(ex.ToString())
    23. Finally
    24.             mSQLDBCmd.Dispose()
    25.             mSQLDBCmd = Nothing
    26.      '       mSQLDBConnector.Close()
    27.       '      mSQLDBConnector = Nothing
    28.      
    29.         End Try
    30. Return lRead
    31.     End Function

    And the calling code:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim db As New Database.SQLDatabase("User ID=QuikFix;Password=ibm123;Initial Catalog=QuikFix;Data Source=HERCULES;")
    3.  
    4.         Dim dr As SqlDataReader
    5.         Dim da As SqlDataAdapter
    6.         Dim ds As DataSet
    7.  
    8.         db.SetSQLDBCommand("select_ticket_by_id") 'pass procedure name
    9.         db.AddSQLDBCmdParameter("@TicketID", SqlDbType.BigInt, 3357) 'pass a parameter
    10.         dr = db.UseSQLDBExecuteReader() 'grab the data
    11.  
    12.         If dr.HasRows() Then
    13.             While dr.Read()
    14.                 MsgBox(dr("TicketID"))
    15.             End While
    16.             dr.Close()
    17.         End If
    18.  
    19.  
    20.         db.SetSQLDBCommand("select_ticket_ids") 'another procedure
    21.         db.AddSQLDBCmdParameter("@ClientID", SqlDbType.Int, 1) 'parameters
    22.         db.AddSQLDBCmdParameter("@FacilityID", SqlDbType.BigInt, 1)
    23.         db.AddSQLDBCmdParameter("@bOpen", SqlDbType.Bit, 1)
    24.         db.AddSQLDBCmdParameter("@bOnHold", SqlDbType.Bit, 1)
    25.  
    26.         dr = db.UseSQLDBExecuteReader() 'execute it
    27.  
    28.         While dr.Read()
    29.             MsgBox(dr("TicketID"))
    30.         End While
    31.         dr.Close()
    32.  
    33.        
    34.     End Sub

    And that works for you ? So why doesnt it work for me ???

  25. #25
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    no the one that works for me, the return is NOT outside the try... here is the method I used that worked
    VB Code:
    1. Public Function UseSQLDBExecuteReader() As SqlDataReader
    2.         Dim lRead As SqlDataReader
    3.         If mSQLDBCmd Is Nothing Then
    4.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    5.             Exit Function
    6.         End If
    7.         If mSQLDBConnector Is Nothing Then
    8.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    9.             mSQLDBConnector.Open()
    10.         Else
    11.             If Not IsSQLDBOpen() Then
    12.                 mSQLDBConnector.Open()
    13.             End If
    14.         End If
    15.         Try
    16.             With mSQLDBCmd
    17.                 .Connection = mSQLDBConnector
    18.                 lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
    19.             End With
    20.             Return lRead
    21.         Catch ex As Exception
    22.             Throw New Exception(ex.ToString())
    23.         Finally
    24.             mSQLDBCmd.Dispose()
    25.             mSQLDBCmd = Nothing
    26.             'mSQLDBConnector.Close()
    27.             'mSQLDBConnector = Nothing
    28.         End Try
    29.     End Function

  26. #26

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    I replaced mine with yours:

    VB Code:
    1. --------------------------------------------------------------------------------
    2.     Public Function UseSQLDBExecuteReader() As SqlDataReader
    3.         Dim lRead As SqlDataReader
    4.         If mSQLDBCmd Is Nothing Then
    5.             Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
    6.             Exit Function
    7.         End If
    8.         If mSQLDBConnector Is Nothing Then
    9.             mSQLDBConnector = New SqlConnection(mSQLDBConnString)
    10.             mSQLDBConnector.Open()
    11.         Else
    12.             If Not IsSQLDBOpen() Then
    13.                 mSQLDBConnector.Open()
    14.             End If
    15.         End If
    16.         Try
    17.             With mSQLDBCmd
    18.                 .Connection = mSQLDBConnector
    19.                 lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
    20.             End With
    21.             Return lRead
    22.         Catch ex As Exception
    23.             Throw New Exception(ex.ToString())
    24.         Finally
    25.             mSQLDBCmd.Dispose()
    26.             mSQLDBCmd = Nothing
    27.             'mSQLDBConnector.Close()
    28.             'mSQLDBConnector = Nothing
    29.         End Try
    30.     End Function

    And my calling code is above and I still get the error...see what Im saying how this is driving me insane .

  27. #27
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    Don't know what to tell you man. It works for me just fine... I called a storedproc in one of my databases, and it returned the data I expected it to.. no issues...

    only thing I changed in the calling code was that I needed to pass 2 params, and I changed BitInt to Int because BitInt is for 64bit integers...

  28. #28

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Err...

    These sorts of things piss me off , but you were definately helpful so dont be sorry. I think this is something very minor and prolly could be an issue on my pc (uhh ya thats it ). So im going to call it a night...because I have to deal with datareaders in this class I think Im going to need to Implement IDisposable and just handle the close for this specific case...do you think thats a good idea ?

    Thanks again Kleinma.

  29. #29
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    just as a sanity check, you may want to reboot your PC incase anything is acting screwy with connection pooling.. you probably have run this code several times on your machine today...

    a fresh reboot should clear that up if it is an issue...

  30. #30

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    :| Umm after a reboot it is working normally now :|...wow...I have to implement idisposable either way tomorrow...unbelievable..this whole time I thouhgt it was the cold . I wasted the whole day . Had to recompile the class as well.

  31. #31
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: All worked fine yesterday..sqldatareader yet again

    well better late than never... 1 day isnt bad.. I spent 2 weeks debugging a problem once, and it wasn't even my code.. friggin zone alarm was screwing up my PC even though I had fully disabled all its features

  32. #32

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    Err...

    Now I tried Implementing IDisposable...
    and that is being a bug too...

    VB Code:
    1. Imports System.Data.SqlClient
    2. Public Class SQLDatabase
    3.     Implements IDisposable
    4.     Private disposed As Boolean = False
    5.     Private mSQLDBConnString As String
    6.     Private mSQLDBCmd As SqlCommand
    7.     Private mSQLDBConnector As SqlConnection
    8.     Private Sub Dispose() Implements IDisposable.Dispose
    9.         If mSQLDBCmd Is Nothing Then
    10.             'no need
    11.         Else
    12.  
    13.             mSQLDBCmd.Dispose()
    14.             mSQLDBCmd = Nothing
    15.         End If
    16.  
    17.         If mSQLDBConnector Is Nothing Then
    18.             'no need to deallocate
    19.         Else
    20.             'coded by kumar
    21.             mSQLDBConnector.Close()
    22.             mSQLDBConnector.Dispose()
    23.             mSQLDBConnector = Nothing
    24.         End If
    25.  
    26.     End Sub

    What is so wrong here? VS.net 1.1 Im using..it is placing a blue line under the Dispose() as well as the IDisposable.Dispose portion...Not sure why, am I not implementing it correctly. I could of sworn the IDisposable interface has ONE sub routine and that is namely Dispose()...

    So whats the dilly ?

  33. #33

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: All worked fine yesterday..sqldatareader yet again

    O my lord..never mind had two signatures of the dispose method...wow Im on a roll today I think im going home now !

Posting Permissions

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



Click Here to Expand Forum to Full Width