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

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

问题描述

我相信这个问题被问了几个月回来,但我相信我的情况是不同的,同样的规则可能不适用。

每次我执行此方法同样的错误弹出。没有排位置0如果我改变[0] [1]或[15];没有一行[1]等。难道这意味着我的心不是数据库连接,甚至?我应该写某种形式的if语句来决定是否行甚至有?

 公共BOOL UpdateOrderToShipped(串序)
{
    ORDERNUMBER =秩序;
    串批= ConfigurationManager.AppSettings [SuccessfulOrderBatch];
    字符串声明=UPDATE SOP10100 SET BACHNUMB ='+ +批阿凡SOPNUMBE = @SOPNUMBE;
    的SqlCommand COMM =新的SqlCommand(声明connectionPCI);
    comm.Parameters.Add(SOPNUMBE,订单编号);
    尝试
    {
        comm.Connection.Open();
        comm.ExecuteNonQuery();
        comm.Connection.Close();
    }
    赶上(例外五)
    {
        comm.Connection.Close();
        KaplanFTP.errorMsg =数据库错误:+ e.Message;
    }    声明=SELECT SOPTYPE FROM SOP10100 WHERE SOPNUMBE = @SOPNUMBE;
    comm.CommandText =语句;
    SqlDataAdapter的大=新SqlDataAdapter的(通讯);
    DataTable的DT =新的DataTable();
    da.Fill(DT);
    soptype = dt.Rows [0] [SOPTYPE]的ToString()。 // errror这里    返回true;
}


解决方案

这是非常简单的...这意味着没有结果从查询返回。你总是要code防守和检查,看看是否行数组中有任何项目试图索引之前。是这样的:

 如果(dt.Rows.Length大于0)
    soptype = dt.Rows [0] [SOPTYPE]的ToString()。
其他
    出了些问题();

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

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

解决方案

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.Length > 0)
    soptype = dt.Rows[0]["SOPTYPE"].ToString();
else
    somethingWentWrong();

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

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