This is a situation where I would propose using foreach instead of a traditional for loop.
foreach( DataRow row in dtIpAddresses.Rows )
{
if( row[0] != null && row[0].ToString() == clientAddress )
{
Flag = true;
break;
}
}
But there is a deeper issue. That is the inefficiency of selecting all the allowed ip addresses when you are simply trying to match 1. A more efficient way would be this:
if (clientAddress != string.Empty)
{
var dbConn = new SqlConnection(strConnection);
var cmd = new SqlCommand("SELECT count(*) FROM t2security.AuthorizedIP WHERE IP=@clientAddress", dbConn);
cmd.Parameters.AddWithValue("@clientAddress", clientAddress);
var dsResults = new DataSet("Results");
var daResults = new SqlDataAdapter();
daResults.SelectCommand = cmd;
daResults.Fill(dsResults);
if(dsResults.Rows.Count == 1)
{
Flag = ( Convert.ToInt32(dsResults.Rows[0].ToString()) == 1 );
}
(Note: There may be typos in the code, but it should give you the general idea.)