tjc1027
01-31-2005, 04:16 PM
I have a site that accesses a database to pull data. For Development I run the site on my local machine with a database server that sits beside me for quick access. The production server where the finished code is uploaded to is on a different subnet and must go over a vpn to access the database server so I set up the database on a server on the subnet where the production server is located and tested it with the new IP and all works ok. Here is my dilema I am constantly updating the code on the new server (the client wants to see the progress regularly) and I really don't want to have to change the IP address in my database connection class every time I upload to the new server so I am wondering if there is a way to get the IP address of the computer Hosting the Website with C# and ASP.NET. From the third octet of the IP I can determine which database server to use.
Thanks
tjc1027
This may not be your best solution but it is what I use since I come from classic ASP. Try...
Request.ServerVariables("SERVER_NAME")
This will return the IP or the server's URL depending on the setup. Either way it should help.
banner
02-01-2005, 04:42 AM
How are you setting your connection string? My recommendation (if possible) would be to use a config file of some sort. That way you don't need to recompile your code every time you move it to another new server. It would also let you (for the most part) just upload the changed files/binaries to the server from your dev machine and leave the configuration alone.
As for your actual question, you could use Server.MachineName to get the host name of the machine. Then you could call System.Net.Dns.GetHostByName to resolve the IP address of the server. It's not the most efficient but it should work, at least in .NET 1.1. I hope this helps.
unlucky1
02-01-2005, 03:32 PM
Why aren't you storing the connection string in the appSettings section of your web.config? That is definitely the easiest way to do it.
<configuration>
<appSettings>
<add key="connString" value="connection string data" />
</appSettings>
</configuration>
Then access it by System.Configuration.ConfigurationSettings.AppSettings["connString"];
My memory might be a little rusty but that should guide you in the right direction.
riziko
02-02-2005, 08:58 AM
Using Web.config is definately the best way to go.
There is also a good trick if you have two different configurations (e.g. development and production). You can use the file= option to specify an alternate config file. If the file doesnt exist (in production) then the standard web.config will be used.
<configuration file="..\DevelopmentWeb.config">
<appSettings>
<add key="connString" value="connection string data" />
</appSettings>
</configuration>
Ross
tjc1027
02-04-2005, 03:47 PM
Thanks for the help and sorry it took me so long to get back on your replies. I am still pretty new to the web.config file. Previously I was using a class to connect to the database and perform certain tasks on it but if I can just call the variable for the connection string within the class then I will do that. The reason I am not using the dns lookup for the IP address is that this is a host behind our corporate firewall and we have no DNS entries for it as we do not have a separate internal DNS Server.
Thank you