Having a spot of trouble with the following code. The method NextIPID() should simply look to a table in SQL Server, which returns the Max Value of field IPID, and then adds 1 to it, to get the next number in the sequence.
public int NextIPID()
{
string strConnect = BuildConnectionString();
SqlConnection linkToDB = new SqlConnection(strConnect);
linkToDB.Open();
string sqlStat = "SELECT MAX(IPID) FROM tblInterestedParties;";
SqlCommand sqlCom = new SqlCommand(sqlStat, linkToDB);
int intNextIPID = (Int32)sqlCom.ExecuteScalar();
linkToDB.Close();
return intNextIPID + 1;
}
However I keep getting the error message
“When casting from a number, the value must be a number less than infinity”.
I suspect this is because the table is currently empty, but then I expected the Execute Scalar to return ‘0’ and hence adding one to this would mean the method returns the next value i.e. 1. However this is not happening.
Assistance greatly appreciated.
If the table is empty, your query won’t return 0, it will return NULL. What you could use is