数据库错误:位置 0 处没有行 [英] Database Error: There is no row at position 0

查看:26
本文介绍了数据库错误:位置 0 处没有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这个问题是几个月前被问到的,但我相信我的情况不同,同样的规则可能不适用.

I believe this question was asked several months back, but i believe my situation is different and the same rules may not apply.

每次执行此方法时都会弹出相同的错误.位置 0 处没有行.如果我将 [0] 更改为 [1] 或 [15];[1] 等处没有行.这是否意味着我的数据库甚至没有连接?我是否应该编写某种 if 语句来确定检查行是否存在?

Everytime I execute this method that same error pops up. There is no row at position 0. If I change [0] to [1] or [15]; There is no row at [1] and etc. Could this mean that my database isnt even connecting? Should I write some kind of if statement to determine to check if the rows are even there?

    public bool UpdateOrderToShipped(string order)
{
    orderNumber = order;
    string batch = ConfigurationManager.AppSettings["SuccessfulOrderBatch"];
    string statement = "UPDATE SOP10100 SET BACHNUMB = '"+ batch +"' WHERE SOPNUMBE = @SOPNUMBE";
    SqlCommand comm = new SqlCommand(statement, connectionPCI);
    comm.Parameters.Add("SOPNUMBE", orderNumber);
    try
    {
        comm.Connection.Open();
        comm.ExecuteNonQuery();
        comm.Connection.Close();
    }
    catch(Exception e)
    {
        comm.Connection.Close();
        KaplanFTP.errorMsg = "Database error: " + e.Message;
    }

    statement = "SELECT SOPTYPE FROM SOP10100 WHERE SOPNUMBE = @SOPNUMBE";
    comm.CommandText = statement;
    SqlDataAdapter da = new SqlDataAdapter(comm);
    DataTable dt = new DataTable();
    da.Fill(dt);
    soptype = dt.Rows[0]["SOPTYPE"].ToString();    //errror here

    return true;
}

推荐答案

这很简单……这意味着您的查询没有返回任何结果.在尝试对其进行索引之前,您始终必须进行防御性编码并检查 Rows 数组中是否有任何项目.类似的东西:

This is very simple ... it means that no results were returned from your query. You always have to code defensively and check to see if the Rows array has any items in it before trying to index into it. Something like:

if (dt.Rows.Count > 0)
    soptype = dt.Rows[0]["SOPTYPE"].ToString();
else
    somethingWentWrong();

这篇关于数据库错误:位置 0 处没有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆