String Connection in Web Config

pgi

New Member
Hi there,<BR><BR>I set my webconfig file like this:<BR><BR><appsettings><BR><add key="MyConn" value=http://aspmessageboard.com/archive/index.php/"server=PLMNBKMYSQLSERVER;uid=sa;pwd=123;database=d atasource" /><BR></appsettings><BR><BR>I created a simple datagrid in a web form and called my database connection like this:<BR><BR>Dim myConnection As SqlConnection<BR>Dim Config As HashTable<BR>Config = Context.GetConfig("appSettings")<BR>myConnection = New SqlConnection(Config("MyConn"))<BR> <BR>But when I run this page through my browser I receive the following error message:<BR><BR>Exception Details: System.InvalidCastException: Specified cast is not valid.<BR><BR>Source Error: <BR><BR><BR>Line 9: <BR>Line 10: Dim Config As HashTable<BR>Line 11: Config = Context.GetConfig("appSettings")<BR>Line 12: myConnection = New SqlConnection(Config("MyConn"))<BR>Line 13: <BR><BR>Anyone to help me to understand what it means and how can I solve this coding error.<BR><BR>Thanks .<BR><BR>Pascal.<BR><BR>Why don't you just import the System.Web.Configuration Namespace and do:<BR><BR>myConnection = New SqlConnection(ConfigurationSettings.AppSettings("MyConn"))I know that this is asp old school, but why not use an application variable in the global.asax?Becaaaaaaaaaauuuuuse it IS "Old School"! All the cool kids are using Web.Config, man!!! LOL:)Chris,<BR><BR>I just did exactly what i suggested to me, but it still doesn't work, I am receiving the same error message.<BR><BR>I still try to figure out what is this "Cast" issue.<BR><BR>Pascal.I have little experience in ASP classic, and try to learn ASP.NET, therefore I have no idea of how to set this application variable.<BR>Thanks for helping.<appSettings><BR><add key="MyConn" value="server=PLMNBKMYSQLSERVER;uid=sa;pwd=123;database=d atasource" /><BR></appSettings>I tried it out before posting my request. My code in the web.config is exactly as you wrote it... exact web.config (w/o server and username/password info).<BR><BR>And, what your code is that you are using to get the data from it.Since you know how my web.config is configured. here is my page code:<BR><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.SqlClient" %><BR><%@ Import Namespace="System.Web.Configuration" %><BR><BR><Script language="VB" Debug="true" Runat="server"><BR><BR>Sub Page_Load(s As Object, e As EventArgs)<BR><BR>Dim myConnection As SqlConnection<BR><BR>Dim Config As HashTable<BR>Config = Context.GetConfig("appSettings")<BR>myConnection = New SqlConnection(Config("MyConn"))<BR><BR>Dim myCommand As SqlCommand<BR>myCommand = New SqlCommand("Select PersonID, FamilyName, GivenName from Person", myConnection)<BR>myConnection.Open()<BR>myDataGrid.DataSource = myCommand.ExecuteReader()<BR>myDataGrid.DataBind()<BR>myConnection.Close()<BR>End Sub <BR><BR></Script><BR><html><BR><head><BR><title>Paging</title><BR></head><BR><body><BR><BR><Form Runat="Server"><BR><BR><asp:DataGrid id="myDataGrid"<BR>Runat="Server"><BR></asp:DataGrid><BR></Form><BR></body><BR></html>chris,<BR><BR>I have it working now! Thanks<BR>At first I actually did not remove the line:<BR>Config=Context.GetConfig("appSettings")<BR>before replacing with your code.<BR>rgds.<BR>PascalWhat purpose does the HashTable serve? Again, per my previous post:<BR>myConnection = New SqlConnection(ConfigurationSettings.AppSettings("MyConn"))<BR>Glad you got it working. I did not see this reply when I posted my last reply.
 
Back
Top