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

C#
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

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

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


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

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


instead?
 
Share this answer
 
Comments
Boopalslm 1-Feb-16 4:29am    
It's working thanks a lot
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.
 
Share this answer
 
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
 
Share this answer
 
I think its a copy/paste mistake.
look at your code and correct following line of code

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


This line
C#
CustomerID = Convert.ToInt32(dt.Rows[0][0]),
 
Share this answer
 
Please check the row count of the "dtResult" , if it is not used by mistake else change it to dt.Rows[0][0]...
 
Share this answer
 
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)



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