Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Connecting to a DB

Connecting to a DB

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-nethelp
15 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jamesbronw

    but myReader is set i have Public myreader as oledbdatareader Public Sub createreader(ByVal SQL As String) gErrorOccurred = False myconn = New OleDbConnection(strconn) mycommand = myconn.CreateCommand mycommand.CommandText = SQL Try myconn.Open() Catch ex As Exception gErrorOccurred = True Exit Sub End Try Try myreader = mycommand.ExecuteReader Catch ex As Exception gErrorOccurred = True Exit Sub End Try End Sub

    M Offline
    M Offline
    Mike Ellison
    wrote on last edited by
    #6

    Are you sure myreader is set? In your createreader sub you have myconn.Open() in a try/catch block. Is it possible you're getting an exception when opening the connection and your myreader is never getting set?

    J 1 Reply Last reply
    0
    • M Mike Ellison

      Are you sure myreader is set? In your createreader sub you have myconn.Open() in a try/catch block. Is it possible you're getting an exception when opening the connection and your myreader is never getting set?

      J Offline
      J Offline
      jamesbronw
      wrote on last edited by
      #7

      Well here is the thing that gets me. I copied all of my code that does the database connection from the module in my ASP.NET application and put it into a VB.NET application. I then copied the same SQL command and put it into a form. When i opened the form the lable on the VB.NET form displayed the info i need. So i know myreader is getting set.

      M 2 Replies Last reply
      0
      • J jamesbronw

        Well here is the thing that gets me. I copied all of my code that does the database connection from the module in my ASP.NET application and put it into a VB.NET application. I then copied the same SQL command and put it into a form. When i opened the form the lable on the VB.NET form displayed the info i need. So i know myreader is getting set.

        M Offline
        M Offline
        Mike Ellison
        wrote on last edited by
        #8

        Well, you know it's getting set in your win forms application at any rate, but you need to prove to yourself whether or not it's really getting set in your ASP.NET application. I'm telling you - the exception you posted, in the context of the code you posted, says that myreader is not set to an instance of an object. That's a fact, based on what you posted. Okay - why don't you explicitly test the value of myreader in code. Turn tracing on for your web application, then add the following lines between lines 48 and 49 of what you posted earlier:

        Line 48: createreader(sql)
        --> added lines:
        if myreader is nothing then
        Trace.Write("MyReader is Nothing")
        else
        Trace.Write("MyReader is instantiated")
        end if
        --> end of added lines
        Line 49: Do While myreader.Read
        Line 50: descrip.Text = descrip.Text & "," & myreader.GetValue(0)
        Line 51: Loop

        This is pretty standard troubleshooting for these types of exceptions. You'll see one or the other message in your trace log on your web page. That should provide you with more information.

        1 Reply Last reply
        0
        • J jamesbronw

          Well here is the thing that gets me. I copied all of my code that does the database connection from the module in my ASP.NET application and put it into a VB.NET application. I then copied the same SQL command and put it into a form. When i opened the form the lable on the VB.NET form displayed the info i need. So i know myreader is getting set.

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #9

          One more thing - I'm betting you are not getting an open connection in the context of your ASP.NET application, and that you are not seeing the exception caused by this failure based on the code you posted earlier. I think you are triggering an exception when you attempt to call myconn.Open() and you are just ignoring it. Look at these lines you posted, where you attempt to make the connection:

          Try
          myconn.Open()
          Catch ex As Exception
          gErrorOccurred = True
          Exit Sub

          If an exception did occur when attempting to make the connection, how would you ever know it? You're setting a variable (gErrorOccured, then exiting the sub. And from what you posted, it didn't look like you were testing for this variable before attempting to use myreader. You really shouldn't just swallow an exception this way in a try/catch block - by doing so, you're probably missing where your problem really is. It makes perfect sense to me that your code wouldn't work in your ASP.NET application, but would work in your VB.NET Win Forms application. The two would likely be running under different user accounts... one common cause for failures in establishing database connections is if the user account used lacks the appropriate priviledges. You should really look into this. I'll bet you a :beer: right now that this is where you're experiencing your problem.

          J 1 Reply Last reply
          0
          • M Mike Ellison

            One more thing - I'm betting you are not getting an open connection in the context of your ASP.NET application, and that you are not seeing the exception caused by this failure based on the code you posted earlier. I think you are triggering an exception when you attempt to call myconn.Open() and you are just ignoring it. Look at these lines you posted, where you attempt to make the connection:

            Try
            myconn.Open()
            Catch ex As Exception
            gErrorOccurred = True
            Exit Sub

            If an exception did occur when attempting to make the connection, how would you ever know it? You're setting a variable (gErrorOccured, then exiting the sub. And from what you posted, it didn't look like you were testing for this variable before attempting to use myreader. You really shouldn't just swallow an exception this way in a try/catch block - by doing so, you're probably missing where your problem really is. It makes perfect sense to me that your code wouldn't work in your ASP.NET application, but would work in your VB.NET Win Forms application. The two would likely be running under different user accounts... one common cause for failures in establishing database connections is if the user account used lacks the appropriate priviledges. You should really look into this. I'll bet you a :beer: right now that this is where you're experiencing your problem.

            J Offline
            J Offline
            jamesbronw
            wrote on last edited by
            #10

            Well to the post before this one you are right!! lol it is getting set to nothing. I'll look through the accounts to see what the privleges might be. I haven't work a lot with privliges before though where would be a good place to start?

            M 1 Reply Last reply
            0
            • J jamesbronw

              Well to the post before this one you are right!! lol it is getting set to nothing. I'll look through the accounts to see what the privleges might be. I haven't work a lot with privliges before though where would be a good place to start?

              M Offline
              M Offline
              Mike Ellison
              wrote on last edited by
              #11

              Privilege issues will depend on which database you are using - but before even going there, make sure you confirm that the real problem is occuring when you attempt to .Open() the connection. Rework your code so that you are presenting the exception details when opening the connection. You have to prove to yourself where the real exception is happening and what its details are, so don't swallow up exceptions in your try/catch blocks - they are there to help you! :) Feel free to post the exception details if you would like additional troubleshooting help. If the exception does turn out to be a lack of privileges, then let us know if you are using SQL Server, or Oracle, or MySQL, or Access, or whatever. The details of security will be dependent on the database.

              J 1 Reply Last reply
              0
              • M Mike Ellison

                Privilege issues will depend on which database you are using - but before even going there, make sure you confirm that the real problem is occuring when you attempt to .Open() the connection. Rework your code so that you are presenting the exception details when opening the connection. You have to prove to yourself where the real exception is happening and what its details are, so don't swallow up exceptions in your try/catch blocks - they are there to help you! :) Feel free to post the exception details if you would like additional troubleshooting help. If the exception does turn out to be a lack of privileges, then let us know if you are using SQL Server, or Oracle, or MySQL, or Access, or whatever. The details of security will be dependent on the database.

                J Offline
                J Offline
                jamesbronw
                wrote on last edited by
                #12

                Ok well i tried to do the myconn.open at the page load of my web form and i errored out. This is what i got Could not lock file. 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.Data.OleDb.OleDbException: Could not lock file. Source Error: Line 51: mycommand = myconn.CreateCommand Line 52: mycommand.CommandText = sql Line 53: myconn.Open() Line 54: Line 55: 'createreader(sql) I am trying to open an access database

                M 1 Reply Last reply
                0
                • J jamesbronw

                  Ok well i tried to do the myconn.open at the page load of my web form and i errored out. This is what i got Could not lock file. 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.Data.OleDb.OleDbException: Could not lock file. Source Error: Line 51: mycommand = myconn.CreateCommand Line 52: mycommand.CommandText = sql Line 53: myconn.Open() Line 54: Line 55: 'createreader(sql) I am trying to open an access database

                  M Offline
                  M Offline
                  Mike Ellison
                  wrote on last edited by
                  #13

                  A couple of things when using Access databases for web applications - your ASP.NET user account will need read/write access not just to the .mdb file, but also the containing directory (so the .ldb file can be created when the file is accessed). You may also need to make sure the .mdb file is not already open in an instance of MS Access.

                  J 1 Reply Last reply
                  0
                  • M Mike Ellison

                    A couple of things when using Access databases for web applications - your ASP.NET user account will need read/write access not just to the .mdb file, but also the containing directory (so the .ldb file can be created when the file is accessed). You may also need to make sure the .mdb file is not already open in an instance of MS Access.

                    J Offline
                    J Offline
                    jamesbronw
                    wrote on last edited by
                    #14

                    Well i have managed to get it working!!!!! lol it turns out that i overlooked the fact that i didnt put the database into the inetpub folder. Thank you so much for your time and help and sorry for overlooking that detail. If i could bother you with one more thing. I have an imagebutton on my form. When i click on it i want to to open up another page. So i inserted around the asp imagebutton code in the HTML. When i click the button it does not load the page. If i right click on it then click open link it takes me to my home page. Do you know how i can fix this? or is there some property associated with the imagebutton? something like imagebutton.page.loadurl(just made that up for example sake) Thanks again

                    J 1 Reply Last reply
                    0
                    • J jamesbronw

                      Well i have managed to get it working!!!!! lol it turns out that i overlooked the fact that i didnt put the database into the inetpub folder. Thank you so much for your time and help and sorry for overlooking that detail. If i could bother you with one more thing. I have an imagebutton on my form. When i click on it i want to to open up another page. So i inserted around the asp imagebutton code in the HTML. When i click the button it does not load the page. If i right click on it then click open link it takes me to my home page. Do you know how i can fix this? or is there some property associated with the imagebutton? something like imagebutton.page.loadurl(just made that up for example sake) Thanks again

                      J Offline
                      J Offline
                      jamesbronw
                      wrote on last edited by
                      #15

                      I spoke to soon. the code that does it is imagebutton.Page.Response.Redirect("index.aspx") again thank you for all of your time and help

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups