Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello friend,


i have an problem i have two textbox one is startno and another is endno
and i want insert ip in these text box and when i am click on add button then add all ip in my database for example from [1.1.1.1] to [1.1.1.5] and when on click on add all ip form 1 to 5 in the database.

i am find the last digit and i am using for loop but i dont know how can i increment in ipno can any one guide me??
Posted

Assuming that your text box contains complete IP addresses: "n.n.n.n" then the first thing to do is convert them from strings to IP addresses you can work with. There is a .NET method for this: IPAddress.Parse Method[^]

string s = "1.1.1.2";
IPAddress i = IPAddress.Parse(s);
byte[] bytes = i.GetAddressBytes();
for (int count = 0; count < 5; count++)
    {
    bytes[3]++;
    i = new IPAddress(bytes);
    Console.WriteLine(i.ToString());
    }
 
Share this answer
 
Comments
guptaadeepak 27-Jul-11 4:57am    
Thanks for update but my problem still not solved
OriginalGriff 27-Jul-11 4:58am    
What part of it are you having problems with?
thank griff my problem is solve i am using this
C#
string myString = Txtfrom.Text.ToString();
       char[] separator = new char[] { '.' };
       string[] colorList = myString.Split(separator);
 
Share this answer
 
Hi,

Thanks to OriginalGriff for the part, the complete / dynamic solution would be:

C#
string address1 = "192.168.1.1";
string address2 = "192.168.5.254";

IPAddress startAddress = IPAddress.Parse(address1);
IPAddress endAddress = IPAddress.Parse(address2);

byte[] startAdrArr = startAddress.GetAddressBytes();
byte[] endAdrArr = endAddress.GetAddressBytes();

for (byte octet1 = startAdrArr[0]; octet1 <= endAdrArr[0]; octet1++)
{
	for (byte octet2 = startAdrArr[1]; octet2 <= endAdrArr[1]; octet2++)
	{
		for (byte octet3 = startAdrArr[2]; octet3 <= endAdrArr[2]; octet3++)
		{
			for (byte octet4 = startAdrArr[3]; octet4 <= endAdrArr[3]; octet4++)
			{
				string currentAddress = string.Format(
								CultureInfo.CurrentCulture,
								"{0}.{1}.{2}.{3}",
								octet1,
								octet2,
								octet3,
								octet4);
				/* process the address */
			}
		}
	}
}


Hope this helps anyone :)

Best regards and happy coding,
Stops
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900