Any help is greatly appreciated...
Bump, still stuck on this problem. Any help would be greatly appreciated.<configuration>
<appSettings>
<add key="ConnectionString" value="Server=localhost;database=msdb;Trusted_Connection=yes" />
</appSettings>
</configuration
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
Dim MyConnection as new Sqlconnection(strConnectionString)
Dim MyCommand as new SqlCommand(strSQL, MyConnection)MyConnection.Open()
MyDataGrid.DataSource = MyCommand.ExecuteReader()
MyDataGrid.DataBind()
MyConnection.Close()
End Sub
Have you had anything work from your web.config file?? The only thing I can think of is that your IIS application is not configured correctly. If this is the case, your .aspx pages will work, but IIS will not know where to look for your web.config file. This may not be the case at all....but it is my best guess at your problem. Check your site in IIS an make sure your application is configured correctly. And also double check to see that your web.config file is in the root of said application.
Hope that helps
-Jason
Jason,
Thanks for the reply. I went into IIS and made sure everything was ok, and as far as I can tell it seemed to be set up correctly. However, I deleted the existing virtual directory and created a brand new directory and set up the application again. It is still not working as intended though.
I also attempted to obtain other information that was contained in the web.config file and that did not work either.
Lastly, I am creating all my .aspx files in the root directory and that is where web.config resides as well.
Can you think of a particular setting I can check to verify everything is set up correctly? Thanks for the input thus far.
-Corey
Have you imported System.Configuration?
If you are going to use ConfigurationSettings.AppSettings("ConnectionString") then you need to import System.Configuration. I usually import System.Configuration.ConfigurationSettings and then just use AppSettings("ConnectionString") so there is less code to write.
Yes, I have also tried that as well. Below is the complete code for my project.
<%@. Page Language="VB" %>
Imports System.Configuration<script runat="server"
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
Dim MyConnection as new System.Data.OleDb.OleDbConnection(strConnectionString)
Dim MyCommand as new System.Data.OleDb.OleDBCommand(strSQL, MyConnection)MyConnection.Open()
MyDataGrid.DataSource = MyCommand.ExecuteReader()
MyDataGrid.DataBind()
MyConnection.Close()
End Sub</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server"></asp:datagrid>
</form>
</body>
</html
Delete any web.configs that are outside of your virtual apps.
This can cause problems.
So inside wwwroot there will be no web.config file, they will only be inside your applications folders.
HTH
Not that it matters in this case, but you got the syntax for importing a namespace wrong. It should have been:
<%@. Page Language="VB" %>
<%@. Import Namespace="System.Configuration" %>
It doesn't matter in this case, as it's not necessary. Your code, as shown, is sufficient to call information from web.config.
So, your problem is not in code. It is likely a setup problem. For example, the web.config file must go in the root folder of your Application. Like this:
c:\
\inetpub
\wwwroot
\MyApp
web.config
default.aspx
If you've placed it inside wwwroot, rather than the root of your own Application, this would cause your problem.
Hence, can you confirm that you've placed your web.config and your ASPX page in their own App folder, as shown? And that that app has been made a Web Application with IIS?
I think the following link could be useful.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaspnetconfiguration.asp
The web.config is inheritable. This means you can have a web.config in wwwroot and in your application folder. When you call ConfigurationSettings.AppSettings("ConnectionString")
the MyApp web.config would be checked first. If that key isn't found then it would look in wwwroot. So, it is not required that you have a web.config in the app folder if you have one in wwwroot. I create a lot of applications that use a connection string so the connection string is placed in the wwwroot web.config that way it resides in one location. Then the web.config files in the app folder only hold application level settings.
Also, virtual roots or IIS applications do not affect this at all. I have tested it both ways. With or without a virtual root the web.config inheritance still works.
The last post is a good test but I don't think it's going to solve the problem. If you had a web.config in wwwroot and a connection string key in there, you wouldn't have to have a web.config in your application folder.
I actually am curious to see if the namespace correction mentioned above fixes things. SomeNewKid2, if he was using incorrect syntax on importing the namespace then I think it would matter, right? No namespace would mean no ConfigurationSettings.AppSettings exists to the application.
No, System.Configuration is a namespace that's implicitly imported with inline code. Hence, he can use ConfigurationSettings.AppSettings, without explicitly importing its namespace.
The way he had it meant it was simply considered literal text. No harm done, and it wouldn't have solved his problem even if his syntax was correct.
(I have used his own code to access a web.config. Works fine. The problem is not in his code.)
Below is where my web.config and default.aspx files reside:
c:\inetpub\wwwroot\training\web.config
c:\inetpub\wwwroot\training\default.aspx
Also, I have checked IIS to verify that it is set up as an application. I also deleted the virtual directory and setup it up as an application from scratch in IIS. I may very well be excluding a minor detail somewhere, but I can't seem to indetify it.
Thanks for all the help,
Corey
Back up your current default.aspx and web.config files. (Or just rename them temporarily.)
Try replacing your web.config with this very simple one:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration
<appSettings>
<add key="ConnectionString" value="Server=localhost;database=msdb;Trusted_Connection=yes" />
</appSettings
<system.web>
</system.web
</configuration>
Now, replace your default.aspx with this modified version:
<%@. Page Language="VB" Debug="true" %
<script runat="server"
Sub Page_Load(Sender as Object, E as EventArgs)
Dim strConnectionString as string = ConfigurationSettings.AppSettings("ConnectionString")
Dim strSQL As String = "SELECT * FROM log_shipping_databases"
'Dim MyConnection as new System.Data.OleDb.OleDbConnection(strConnectionString)
'Dim MyCommand as new System.Data.OleDb.OleDBCommand(strSQL, MyConnection)'MyConnection.Open()
'MyDataGrid.DataSource = MyCommand.ExecuteReader()
'MyDataGrid.DataBind()
'MyConnection.Close()
Message.Text = strConnectionString
End Sub</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="MyDataGrid" runat="server"></asp:datagrid>
<asp:Label id="Message" runat="server" />
</form>
</body>
</html>
This code works fine on my machine. Let me know if it works okay on yours. (It should simply show the ConnectionString value that it retrieved from web.config.)
It prints the connection string to the browser:
Server=localhost;database=msdb;Trusted_Connection=yes
Which means it worked.
Well, the .aspx file is just a simplified version of your own. Which suggests that there is merely an error in your web.config file.
Since the web.config file is an XML file, it is very sensitive to syntax and proper nesting of statements.
You probably just have a statement in the wrong place.
Do you want to post the code for your web.config file?
0 comments:
Post a Comment