Hey people,


I have a database that is stored on a remote machine.
In my application i have used connection string with
IP address of that remote machine. Now, the problem is
every time the remote machine is turned on it has a new
IP address. Is there any way that i can get that newly
generated IP address stored in connection string automatically
every time i try to connect to database.

Here's my connection string :

Data Source=xxx.xxx.xxx.xxx,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

NOTE : Its not a web application.


thanks

You Can create app.config file in your application
In that file you will see the below code

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	
</configuration>

In this you need to add

<appSettings>
		<add key="database" value="Data Source=xxx.xxx.xxx.xxx,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
"/>
	</appSettings>

Check this will work

And in your cs file to get this use the below
System.Configuration.ConfigurationManager.AppSettings["database"];

But everytime you need to changes in this app.config file
which will help you not to build the solution
At runtime it will take the ip address from the app config file
Hope this help you

You need some form of DNS service to resolve the IP address. If you can't run your own, there is a service called DynDNS that specializes in resolving dynamic IP addresses. You just have to run their software on the machine with the dynamic IP and you are good to go :)

Another solution is to write a service for the machine with the changing IP address that connects to a service on the machine you wish to run your software and have it transfer the IP address. But I'm guessing that both machines actually have dynamic IPs, which means the above solution is your best bet.

Hey guys,

Check out this code.
It gives me errors.
Can you rectify it?

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetIP());
        }
        public string GetIP()
        {
            string strhostname = System.Net.Dns.GetHostByName();
            IPHostEntry iphostinfo = Dns.Resolve(Dns.GetHostByName());
            IPAddress ipad = iphostinfo.AddressList[0];
            return ipad.ToString();
        }
    }

Errors :


Error 1 No overload for method 'GetHostByName' takes '0' arguments

Error 2 No overload for method 'GetHostByName' takes '0' arguments

Error 3 The best overloaded method match for 'System.Net.Dns.Resolve(string)' has some invalid arguments


Error 4 Argument '1': cannot convert from 'System.Net.IPHostEntry' to 'string'

Errors 1-2 :You need to supply a host name to GetHostByName (lines 14 and 15) something like "GetHostByName("www.google.com"); Error 3: Line 15: Since the GetHostByName fails, this call fails. Fixing one should fix the other.

take another string variable.
Get the connection string from app.config file
and pass this as parameter

Hey guys i think i got the answer.
which was very much simple than i had thought.
But still i want you people to check it and tell
that if it works for you.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        { 
            
            string myhost = System.Net.Dns.GetHostName();
            string myip = System.Net.Dns.GetHostEntry(myhost).AddressList[0].ToString();
            MessageBox.Show(" "  +myhost+ ""  +myip);
        }
            
    }

Thanks for replying

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.