try to access MySQL in ASP.NET but output error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laxgb
    New Member
    • Sep 2007
    • 2

    try to access MySQL in ASP.NET but output error

    Hi

    I tried to use following code to access MySQL database but the error 'Object reference not set to an instance of an object' was happened:
    [code=vb]
    Dim MyConString As String = "DRIVER={My SQL ODBC 3.51 Driver}; SERVER=localhos t; DATABASE=xxxx; UID=xxxx; PASSWORD=xxxx; OPTION=3"
    Dim MyConnection As New System.Data.Odb c.OdbcConnectio n(MyConString)

    Dim SQL As String = "SELECT * FROM registration_co de"
    Dim MyCommand As New System.Data.Odb c.OdbcCommand
    MyCommand = New System.Data.Odb c.OdbcCommand(S QL, MyConnection)

    MyConnection.Op en()

    Dim dr As System.Data.Odb c.OdbcDataReade r
    dr = MyCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection Or System.Data.Com mandBehavior.Si ngleResult)

    While dr.Read()
    ...
    Loop

    MyConnection.Cl ose()
    [/code]


    The error shown as like:
    Code:
    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.
    Is there anybody know where my codes wrong?

    Thanks

    Gary
    Last edited by Atli; Sep 8 '07, 04:23 AM. Reason: Added [code] tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by laxgb
    Hi

    I tried to use following code to access MySQL database but the error 'Object reference not set to an instance of an object' was happened:
    [code=vb]
    Dim MyConString As String = "DRIVER={My SQL ODBC 3.51 Driver}; SERVER=localhos t; DATABASE=xxxx; UID=xxxx; PASSWORD=xxxx; OPTION=3"
    Dim MyConnection As New System.Data.Odb c.OdbcConnectio n(MyConString)

    Dim SQL As String = "SELECT * FROM registration_co de"
    Dim MyCommand As New System.Data.Odb c.OdbcCommand
    MyCommand = New System.Data.Odb c.OdbcCommand(S QL, MyConnection)

    MyConnection.Op en()

    Dim dr As System.Data.Odb c.OdbcDataReade r
    dr = MyCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection Or System.Data.Com mandBehavior.Si ngleResult)

    While dr.Read()
    ...
    Loop

    MyConnection.Cl ose()
    [/code]


    The error shown as like:
    Code:
    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.
    Is there anybody know where my codes wrong?

    Thanks

    Gary
    You are derefencing a null variable somewhere in your code.
    You may want to ask in the .NET forum if you can't figure out where the null pointer is occuring.

    Comment

    Working...