简体   繁体   English

位置0处无行错误处理

[英]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. 我的代码通过将GridView中的ID传递到SQL语句中来工作,以便找到与存储在表上的ID相关联的对应FileImage。 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). 我最近注意到,如果手动输入错误的ID,站点将崩溃,并引发异常“位置0处无行”,我发现这基本上意味着没有数据可提取(显然是因为我输入了伪造的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? 我以前从未真正考虑过错误处理,但我想从我读到的内容中我会做一些诸如if语句的事情? 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! 基本上,如果没有异常,则继续进行,但是,如果有异常,则可以将页面上TextBox的文本更改为错误消息,告诉用户“警告! the ID is invalid'? ID无效”?

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: 您只需要使用Count属性进行检查:

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. Tables属性还具有Count属性。

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). 您不必在catch代码块中引发异常(根据您在发布的代码中看到的内容)。

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. 您需要查看.Rows包含元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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