JCE Posted October 24, 2003 Posted October 24, 2003 Hi, If I use Server Explorer to create a connection to a SQL 2000 DB, how do I dynamically change the conectionstring property at run time based on my config file. It appears that the code to build teh connection is in the "hidden" or "Do not touch" section of code generated by the designer. So, I can't tell it to look in my config file. If I want the connectionstring to be dynamic, do I have to programatically create it, and not use Server Explorer. If I do that, then I have to programatically create the 38 Datasets I need for all my Drop Down List boxes, also. I'd prefer to use clidk and drag. Please share your wisdon with me. Thanks Jon Quote
Moderators Robby Posted October 24, 2003 Moderators Posted October 24, 2003 Are you using 38 different connection strings? Probably not, create a public property and whenever you need a new connection just call the property for it. Quote Visit...Bassic Software
JCE Posted October 24, 2003 Author Posted October 24, 2003 Robby, You are correct, I am not using 38 connections. I have one for each ASP page. I have a dataadapter for each drop down list. I am not certain what you mean by a creating a public property. I used the Server Explorer wizard to create the connection. Is it possible for me to chagne that between my local SQL db (for dev) and the production SQL server? Will the public property do this? Can I have a single connection that is used by all ASP .net pages? Thanks for your help. Jon Quote
Moderators Robby Posted October 24, 2003 Moderators Posted October 24, 2003 Here a simplified solution... Create a Class with a Property in it. This property should return your connection string. Then each page needs to create an instance of this class in order to use/get the connection string. Public Class DataConnectionString private connString as string = "place your conn string here" Public ReadOnly Property getConnectionString() As String Get Return connString End Get End Property End class Then near the top of your aspx pages' Code-Behind... Private CN as new DataConnectionString() 'then one of your routines can consume it as such... myString = CN.getConnectionString Quote Visit...Bassic Software
mocella Posted October 24, 2003 Posted October 24, 2003 Another way to do this would be to use the web.config file. To do this, in your web.config file you can add this entry at the top(you need to add the appSettings section - case sensitive): <configuration> <appSettings> <add key="DataSource" value="server=localhost;database=something"/> </appSettings> Somewhere in your ASP code, add the following (VB Style): Imports System.Configuration Dim strconn As String strconn = ConfigurationSettings.AppSettings.Get("DataSource") Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.