|
-
Dec 27th, 2002, 01:25 PM
#1
Thread Starter
Addicted Member
Anything in the table?
I have a function that opens the database and returns the highest id, I have been using executescalar as there is only one value to return.
The problem is though if there is nothing in the database then it crashes. is there a way to determine if there is anything there before I executescalar?
here is the code
<code>
//open the database and return the most recent alertid
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
connection.Open();
string commandString = "SELECT MAX(AlertId) FROM Alert";
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
command.CommandText = commandString;
command.Connection = connection;
int alertid = (int) command.ExecuteScalar();
//Release all resources
connection.Close();
command.Dispose();
//return the id of the largest alert
return(Convert.ToInt16(alertid));
</code>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|