How to do it exactly the way I wanted it?
What I really wanted: Adding IP Range ex. Starting IP textbox [192.168.1.1] -> Netmask textbox [24] = (192.168.1.1 - 192.168.1.254) IPs added in database.
Also I wanted it to check if the user put the right IP syntax not just a random numbers and show a message if it's wrong.
I've only managed to do is this with 2 textbox [start ip] -> [end ip] then [add button]
if (CheckIPValid(txtstartip.Text)&&(CheckIPValid(txtendip.Text)))
{
if (!(txtstartip.Text.StartsWith("0"))&&(!(txtstartip.Text.StartsWith("0"))))
{
string startip = txtstartip.Text;
string endip = txtendip.Text;
string insertinip = "";
string usingip = startip.Substring(0, startip.LastIndexOf(".") + 1);
startip = startip.Substring(startip.LastIndexOf(".") + 1);
endip = endip.Substring(endip.LastIndexOf(".") + 1);
int endipCount = Convert.ToInt16(endip);
int startipCount = Convert.ToInt16(startip);
if (endipCount > startipCount)
{
int totalIpAdding = endipCount - startipCount;
int actualAddingIps = 0;
for (int i = startipCount; i <= endipCount; i++)
{
insertinip = "";
insertinip = usingip + "" + i.ToString();
if(checkDuplicateIP(insertinip))
{
MessageBox.Show("The IP "+ insertinip + " is already in Database");
}
else
{
query = "insert into tblIPAddress(IP_Address) values('" + insertinip + "')";
OleDbCommand cmd = new OleDbCommand(query);
cmd.Connection = myConn;
myConn.Open();
cmd.ExecuteNonQuery();
actualAddingIps++;
myConn.Close();
}
}
if(actualAddingIps==totalIpAdding )
MessageBox.Show("New IP Range Added");
else if(actualAddingIps > 0)
{
MessageBox.Show("IP Range Added");
}
else
{
MessageBox.Show("No IP Added");
}
}
else
{
MessageBox.Show("Invalid IP Range");
}
}
else
{
MessageBox.Show("InValid IP");
}
}
else
{
MessageBox.Show("InValid IP");
}