Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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.
Posted

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.
 
Share this answer
 
Comments
Boopalslm 12-Feb-16 2:21am    
yes i am checking line by line my SaveCustomerDetails() table, but again same error is came.
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

C#
CustomerID = Convert.ToInt32(dtResult.Rows[0][0])
//Insetad
CustomerID = Convert.ToInt32(dt.Rows[0][0])
 
Share this answer
 
Comments
Boopalslm 12-Feb-16 2:24am    
k working, but data's are not saved in SaveCustomerDetails() table.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900