0

This is my first question in stackoverflow and I'm a novice in .NET, so forgive me for the stupid question if it is so! When I test it in local no problem. On the server, it starts working good but after some time (more probably after some visitors visit the site) it stops working with this error and about 30mins later it starts working back. Here is my code: code behind:

    Dim baglanti As String = "Data Source=xxx.xxx.xxx;Initial Catalog=xxVT;Persist Security Info=True;User ID=xxx;Password=xxx"
    Public mRs As SqlDataReader
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim Conn As SqlConnection
    Conn = New SqlConnection(baglanti)
        Conn.Open()
    Dim lcSQLCommand1 As New SqlCommand("Select * from Fiyatlar", Conn)
        mRs = lcSQLCommand1.ExecuteReader()
    End Sub

aspx page:

    <% If Not IsNothing(mRs) Then   'I also tried with IsDBNull here, but same result.
    Do While mRs.Read  %>      

   <!--do some job with mRs-->
   <%Loop  
   mRs.close()
   end if  %>

and this is the error message: Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 22: Line 23: <% If Not IsDBNull(mRs) Then

Line 24: Do While mRs.Read%>
Line 25:
Line 26:

meeteine
  • 31
  • 6
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 27 '14 at 19:37
  • I've already read that one. First of all I exactly know the reason, data reader turns the null value. But the question is: why it turns null value sometimes but not always? The second question is, even I put IsNothing(IsDBNull) control,it still returns the same error.. – meeteine May 27 '14 at 19:46
  • 1
    You would do better using a data-bound control instead of "manually" running through the SqlDataReader – John Saunders May 27 '14 at 21:20
  • probably you're right John but since i have few data, i selected datareader, it is easier for me. if i cant find a solution to this problem i'll change my coding. – meeteine May 27 '14 at 21:40
  • IsDBNull is not doing a null check. It is doing an evaluation on your SqlDataReader to DBNull.Value. Your null check should be `If not mRs Is Nothing Then` ref: http://msdn.microsoft.com/en-us/library/tckcces5(v=vs.90).aspx – InbetweenWeekends May 28 '14 at 14:07

1 Answers1

0

The way I see your code, you will likely fill up your SQL connections, because you're not closing your connection. +1 @John Saunders. You would be better off binding your data to a control. That's the whole point of having them. I refreshed this working sample (with modified connection string/query) over 100x with no problems.

Dim baglanti As String = "Data Source=xxx.xxx.xxx;Initial Catalog=xxVT;Persist Security Info=True;User ID=xxx;Password=xxx"
Public mRs As SqlDataReader
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim Conn As SqlConnection
    Conn = New SqlConnection(baglanti)
    Try
        Conn.Open()
        Using lcSQLCommand1 As New SqlCommand("Select * from Fiyatlar", Conn)
            repeater.DataSource = lcSQLCommand1.ExecuteReader()
            repeater.DataBind()
        End Using
    Catch ex as Exception
        ' handle your exception
    Finally
        Conn.Close()
        Conn.Dispose()
    End Try
End Sub

ASPX

    <asp:Repeater ID="repeater" runat="server" EnableViewState="false">
        <ItemTemplate>
            <%# Container.DataItem(0)%>...<%# Container.DataItem(1)%><br />
        </ItemTemplate> 
    </asp:Repeater>    
InbetweenWeekends
  • 1,382
  • 2
  • 21
  • 26
  • I edit my question, i was closing my datareader but not connection so it was creating over connection( is there such a word?). Any case, i decide to change the way of my coding as you and John suggest. i used repeater and so far there is no problem. thank you for your detailed answer. – meeteine May 28 '14 at 21:04