I have the following code:
try
{
connection.Open();
da.Fill(ds);
DataRow item = ds.Tables[0].Rows[0];
byte[] item1 = (byte[])item["FileImage"];
ds.Tables.Clear();
numArray = item1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
return numArray;
}
My code works by passing an ID from a GridView into a SQL statement in order to find the corresponding FileImage associated to the ID which is stored on a table. I noticed recently that if I manually enter a incorrect ID, the site crashes and an exception is thrown 'No row at position 0' which I found out basically means there is no data to fetch (obviously because I entered a fake ID).
My question is how can I handle this error? I've never really thought about error handling before, but I guess from what I read I would do something such as an if statement? Basically, if there is no exception then carry on, but if there is an exception then maybe change the text of a TextBox on my page to a error message telling the user that 'Warning! the ID is invalid'?
Thanks for any help!