用户代码未处理Indexoutofrange异常,位置0处没有行 [英] Indexoutofrange exception was unhandled by user code there is no row at position 0

查看:299
本文介绍了用户代码未处理Indexoutofrange异常,位置0处没有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  productids =  string  .Empty; 
DataTable dt;

if (会话[ MyCart]!= null
{
dt =(DataTable)Session [ MyCart];

tkbsonsmerchant k = new tkbsonsmerchant()
{
CustomerName = txtCustomerName.Text,
CustomerEmailID = txtCustomerEmailID.Text,
CustomerAddress = txtCustomerAddress.Text,
CustomerPhoneNo = txtCustomerPhoneNo.Text,
TotalPrice = Convert.ToInt32(txtTotalPrice.Text),
ProductList = productids,
PaymentMethod = rblPaymentMethod.SelectedItem.Text
};

DataTable dtResult = k.SaveCustomerDetails();


for int i = 0 ; i < dt.Rows.Count; i ++)
{
tkbsonsmerchant SaveProducts = new tkbsonsmerchant()
{

// 以下是这里的错误名称 - 超出范围的指数不受用户代码的影响 - 位置为0的行。
CustomerID = Convert.ToInt32(dtResult.Rows [ 0 ] [ 0 ]),
ProductID = Convert.ToInt32(dt.Rows [i] [ ProductID]),
};
SaveProducts.SaveCustomerProducts();
}

Session.Clear();



lblTransactionNo.Text = 您的交易编号: - + dtResult.Rows [ 0 ] [ 0 ];

pnlOrderPlacedSuccessfully.Visible = true ;


sendOrderPlacedAlert(txtCustomerName.Text,txtCustomerEmailID.Text,Convert.ToString(dtResult.Rows [ 0 ] [ 0 ]));




txtCustomerAddress.Text = string .Empty;
txtCustomerEmailID.Text = string .Empty;
txtCustomerName.Text = string .Empty;
txtCustomerPhoneNo.Text = string .Empty;
txtTotalPrice.Text = 0;
txtTotalProducts.Text = 0;
}





我的尝试:



IndexOutOfRange异常未被用户代码处理在位置0没有行,如何解决错误任何一个请引导给我。

解决方案

数据表中没有行dtResult

调试代码并检查天气你的函数SaveCustomerDetails()

是否在你的数据表中添加记录。


错误很简单,您可以访问数据集/数据表集合中不存在的行/列索引,在您的代码中,您看起来使用的是 dtResult 变量,该变量未定义代码中的任何地方,我认为您需要更改代码如下



 CustomerID = Convert.ToInt32(dtResult.Rows [< span class =code-digit> 0 ] [ 0 ])
// Insetad
Cus tomerID = Convert.ToInt32(dt.Rows [ 0 ] [ 0 ])


string productids = string.Empty;
            DataTable dt;

            if (Session["MyCart"] != null)
            {
                dt = (DataTable)Session["MyCart"];

                tkbsonsmerchant k = new tkbsonsmerchant()
                {
                    CustomerName = txtCustomerName.Text,
                    CustomerEmailID = txtCustomerEmailID.Text,
                    CustomerAddress = txtCustomerAddress.Text,
                    CustomerPhoneNo = txtCustomerPhoneNo.Text,
                    TotalPrice = Convert.ToInt32(txtTotalPrice.Text),
                    ProductList = productids,
                    PaymentMethod = rblPaymentMethod.SelectedItem.Text
                };

                DataTable dtResult = k.SaveCustomerDetails();


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tkbsonsmerchant SaveProducts = new tkbsonsmerchant()
                    {
                        
  //BELOW LIEN HERE IS ERROR CAME  -  INDEX OUT OF RANGE EXCEPTION WAS UNHANDLED BY USER CODE - THERE IS ROW AT POSITION 0.
                      CustomerID = Convert.ToInt32(dtResult.Rows[0][0]),
                        ProductID = Convert.ToInt32(dt.Rows[i]["ProductID"]),
                    };
                    SaveProducts.SaveCustomerProducts();
                }

                Session.Clear();



                lblTransactionNo.Text = "Your Transaction No :-" + dtResult.Rows[0][0];

                pnlOrderPlacedSuccessfully.Visible = true;


              sendOrderPlacedAlert(txtCustomerName.Text, txtCustomerEmailID.Text, Convert.ToString(dtResult.Rows[0][0]));

              


                txtCustomerAddress.Text = string.Empty;
                txtCustomerEmailID.Text = string.Empty;
                txtCustomerName.Text = string.Empty;
                txtCustomerPhoneNo.Text = string.Empty;
                txtTotalPrice.Text = "0";
                txtTotalProducts.Text = "0";
            }



What I have tried:

IndexOutOfRange Exception was unhandled by user code There is no row at position 0, How to solve the error any one please guide to me.

解决方案

There would be no rows in datatable dtResult
debug your code and check weather your function SaveCustomerDetails()
is adding record to your datatable or not.


Error is simple you have access a rows/column index which is not exist in dataset/datatable collection, in your code it looks you have used 'dtResult' variable, which is not defined anywhere in your code, I think you need to change your code as follows

CustomerID = Convert.ToInt32(dtResult.Rows[0][0])
//Insetad
CustomerID = Convert.ToInt32(dt.Rows[0][0])


这篇关于用户代码未处理Indexoutofrange异常,位置0处没有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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