如何处理IndexOutOfRange异常

如何处理IndexOutOfRange异常

本文介绍了如何处理IndexOutOfRange异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

My code

string qyery1 = "SELECT Ticket_Id FROM Booking_table WHERE seats = '" + lblseats.Text + "' AND Journey_date = '" + lblDate.Text + "'";
OleDbCommand cmd1 = new OleDbCommand(qyery1, con);
OleDbDataReader dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
    lblTicketNo.Text = dr1["Ticket_Id"].ToString();

    //Error IndexOutOfRange Occured
}



Ticket_Id 是来自Booking_table的主键,



发生循环错误 IndexOutOfRange

如何处理此异常..


Ticket_Id is primary key from Booking_table,

In side While Loop error occurred IndexOutOfRange.
How to handle this Exception..

推荐答案

while (dr1.Read())
{
    if(rd1["Ticket)Id"] != DBNull.Value)
        lblTicketNo.Text = dr1["Ticket_Id"].ToString();
}





-KR



-KR


while (dr1.Read())
{
    lblTicketNo.Text = dr1[i].ToString();
    //you should mention the index of the reader not in primary key.
    i++;
    //Error IndexOutOfRange Occured
}


lblTicketNo.Text = dr1[0].ToString();


这篇关于如何处理IndexOutOfRange异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:21