0

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!

Paddyd
  • 1,870
  • 16
  • 26
JimmyK
  • 4,801
  • 8
  • 35
  • 47
  • 3
    Side note: replace `try`/`catch`/`finally` with a `using` block. (http://msdn.microsoft.com/en-us/library/yh598w02.aspx). Also, don't rethrow a new exception, just use `throw;` (http://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c). – sircodesalot Oct 31 '13 at 15:23
  • 1
    `if(ds.Tables[0].Rows.Count == 0)` don't do operation – Jonesopolis Oct 31 '13 at 15:27
  • 1
    Another side-note: you don't need to open or close the connection with `DataAdapter.Fill`. If you don't open it the connection will be openend and closed implicitely. – Tim Schmelter Oct 31 '13 at 15:48
  • Thanks for the help guys, just going to try these solutions out now before I pick a answer – JimmyK Nov 01 '13 at 08:58

6 Answers6

3

You are probably getting the error here:

DataRow item = ds.Tables[0].Rows[0];

because there is no row at this index, hence there is no row at all in the table.

You just have to check that with the Count property:

if(ds.Tables[0].Rows.Count > 0)
{

}

If no rows are returned the tables are also empty. The Tables property has also a Count property.

if(ds.Tables.Count > 0)
{

}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You need to validate if there is data before retrieving it.

Replace:

DataRow item = ds.Tables[0].Rows[0];
byte[] item1 = (byte[])row["FileImage"];

with

byte[] item1 = null;
if (ds.Tables.Count > 0)
{
   var table = ds.Tables[0];
   if (table.Rows.Count > 0)
   { 
      var row = table.Rows[0];
      if (row.Columns.Contains("FileImage")) 
      {
         item1 = (byte[])row["FileImage"];
      }
   }
}
if (item1 == null) 
{
    //handle error
}
Bas
  • 26,772
  • 8
  • 53
  • 86
0

You don't have to throw the exception in your catch block (from what I've seen in the code you posted).

You can simply show a message that something went wrong like this:

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)
        {
            MessageBox.Show("My error description");
// or write the message to a textBox.
        }
        finally
        {
            connection.Close();
        }
        return numArray;
    }
user1567896
  • 2,398
  • 2
  • 26
  • 43
0

You could do

    try
    {
       // your code....           

    }
    catch (Exception ex)
    {
        MessageBox.Show("My method failed, see inner excpetion",ex);
    }
    finally
    {
        connection.Close();
    }
    return numArray;
}
objecto
  • 54
  • 3
  • Oddly.. Attempting to use Message.Show always gives me an error: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Show' and no extension method 'Show' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?) – JimmyK Nov 01 '13 at 09:43
0

You need to see if .Rows contains elements.

if (ds.Tables[0].Rows.Any())
{
   // Has rows.
}
gleng
  • 6,185
  • 4
  • 21
  • 35
-1

You should check to see if there are rows to work with. Something like this:

try
    {
        connection.Open();



        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
           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;
}
Brandon
  • 339
  • 3
  • 11
  • This could break if there are no Tables. – Bas Oct 31 '13 at 15:31
  • Even if zero rows are returned there would still be a table. Try it create a SQL call that returns 0 records and then do the check. For reference see the accepted answer here: http://stackoverflow.com/questions/16985502/sqldataadapter-filldatatable-not-filling-datatable – Brandon Oct 31 '13 at 17:02