When no data available in the table ("EnrollTbl"), it will return null value for the MAX(Register_ID) query and while trying to convert the null value into Int32 it throws the error. So change the query as follows;
[CODE]SqlCommand cmd = new SqlCommand("SELECT [COLOR="Green"][B]Isnull(MAX(Register_ID),9999)[/B][/COLOR] AS Register_ID FROM EnrollTbl", con);[/CODE]
By … Read More
this is wrong:
RegisterID.Text = (Convert.ToInt32(reader["Register_ID"]) + 1).ToString();
RegisterID.Text = ((Convert.IsDbNull(reader["Register_ID"]))?0:Convert.ToInt32(reader["Register_ID"])).ToString();
[/code] Read More
You are correct; reader.hasrows DOES check if the datareader has retrieved any rows, and if you had used "SELECT * FROM EnrollTbl" and the table was empty then reader.HasRows would have returned false.
However, you used an aggregate function (MAX). These ALWAYS return a value. If there is no data … Read More
You can also use the [ICODE]ExecuteScalar()[/ICODE] command instead of [ICODE]ExecuteReader()[/ICODE].
This does the reading for you and extracts the value (value can still be DBNull though).
To get aroung the DBNull issue it is possible to use a nullable type. [URL="http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx"]MSDN Nullable Types[/URL].
E.G.[CODE]int? value = cmd.ExecuteScalar() as int?;
label2.Text … Read More
[QUOTE=Ryshad;1288496]why not make it an [COLOR="Red"]identity[/COLOR] field in the database and seed it from 10000. That way every time you insert a new record it will automatically get assigned the next number in sequence and avoid you having to do this :)[/QUOTE]
Thats what i've been telling you to use … Read More