Connecting to a DB
-
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
Are you sure
myreader
is set? In yourcreatereader
sub you havemyconn.Open()
in a try/catch block. Is it possible you're getting an exception when opening the connection and yourmyreader
is never getting set? -
Are you sure
myreader
is set? In yourcreatereader
sub you havemyconn.Open()
in a try/catch block. Is it possible you're getting an exception when opening the connection and yourmyreader
is never getting set?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.
-
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.
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 ofmyreader
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: LoopThis 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.
-
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.
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 SubIf 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 usemyreader
. 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. -
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 SubIf 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 usemyreader
. 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.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?
-
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?
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. -
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.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
-
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
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.
-
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.
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 -
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 againI 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