Click here to Skip to main content
13,198,727 members (42,149 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
for (int i = 0; i < dt.Rows.Count; i++)
               {
                   tkbsonsmerchant SaveProducts = new tkbsonsmerchant()
                   {
                       CustomerID = Convert.ToInt32(dtResult.Rows[0][0]),
 
                       //CustomerID = Convert.ToInt32(dtResult.Rows[i]["CustomerID"]),
                       ProductID = Convert.ToInt32(dt.Rows[i]["ProductID"]),
                       TotalProducts = Convert.ToInt32(dt.Rows[i]["ProductQuantity"])
 
                   };
 


Hello above lines are my code - i got error for above lines

IndexOutOfRange Exception was unhandled by user code
There is no row at position 0


How to solve above error please any one guide me.
Posted 31-Jan-16 22:11pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

When you're getting an error always say what line the error happens on, and use the debugger to step through your code as it will help solve these issues. If the error is on this line

CustomerID = Convert.ToInt32(dtResult.Rows[0][0]),


Then chances are dtResult has no rows in it. Did you mean

CustomerID = Convert.ToInt32(dt.Rows[0][0]),


instead?
  Permalink  
Comments
Boopalslm 1-Feb-16 4:29am
   
It's working thanks a lot
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

kindly check parameters which are you passing through code are matching to tables parameters and datatype. If you are using procedure then execute it by adding parameters which are u taking @ run time..You will find out the problem easily.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It simply means that the number of rows is zero. Before addressing some element of the array or a collection by index, you can check up the length of it, number of elements.

—SA
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I think its a copy/paste mistake.
look at your code and correct following line of code

change below line to
CustomerID = Convert.ToInt32(dtResult.Rows[0][0]),


This line
CustomerID = Convert.ToInt32(dt.Rows[0][0]),
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Please check the row count of the "dtResult" , if it is not used by mistake else change it to dt.Rows[0][0]...
  Permalink  
Comments
Boopalslm 12-Feb-16 1:49am
   
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";
}




Again error came,please give me any one idea.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171020.1 | Last Updated 1 Feb 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100