简体   繁体   中英

No row at position 0 Error Handing

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!

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)
{

}

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
}

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;
    }

You could do

    try
    {
       // your code....           

    }
    catch (Exception ex)
    {
        MessageBox.Show("My method failed, see inner excpetion",ex);
    }
    finally
    {
        connection.Close();
    }
    return numArray;
}

You need to see if .Rows contains elements.

if (ds.Tables[0].Rows.Any())
{
   // Has rows.
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM