locked
thers is no row at position RRS feed

  • Question

  • User-265772647 posted

    Hi,

    In the below code it is giving the error thereis no row at this position. in my table  I have  count =22.When I debug the code when it reaches I=22 and count=22 it exits the loop.If I put  i <=dt.Rows.Count  then it is entering into the loop  but it is giving the error thers is no  row at position

      for (int i = 0; i < dt.Rows.Count; i++)
                        {
    
                            ProductName = dt.Rows[i]["ProductName"].ToString();//thers is no  row at position 22
    
                            Serialno = dt.Rows[i]["ProductID"].ToString();
                            // newProd = ProductName;
                            newProd = Serialno + "---" + ProductName;
                              ddlProductInfo.Items.Add(new ListItem(newProd, Serialno));
                              ddlProductInfo.SelectedItem.Value = Serialno;
                         
                        }
    

    Thursday, August 13, 2015 9:53 AM

All replies

  • User2103319870 posted

    emaak

    In the below code it is giving the error thereis no row at this position. in my table  I have  count =22.When I debug the code when it reaches I=22 and count=22 it exits the loop.If I put  i <=dt.Rows.Count  then it is entering into the loop  but it is giving the error thers is no  row at position

    You looping counter starts with 0, so you just need loop from 0 to 21 to loop through all 22 rows. for that reduce the count of datatable in your for loop dt.Rows.Count-1

    You can try with the below code

    //Here change your code like given below
                for (int i = 0; i < dt.Rows.Count-1; i++)
                {
    
                    ProductName = dt.Rows[i]["ProductName"].ToString();//thers is no  row at position 22
    
                    Serialno = dt.Rows[i]["ProductID"].ToString();
                    // newProd = ProductName;
                    newProd = Serialno + "---" + ProductName;
                    ddlProductInfo.Items.Add(new ListItem(newProd, Serialno));
                    ddlProductInfo.SelectedItem.Value = Serialno;
    
                }

    Thursday, August 13, 2015 10:05 AM
  • User2103319870 posted

    Another option is to use foreach loop instead of for loop like below.

                foreach (DataRow row in dt.Rows)
                {
                    //access the product name froim each datarow
                    ProductName = row["ProductName"].ToString();//thers is no  row at position 22
                    //access the ProductID froim each datarow
                    Serialno = row["ProductID"].ToString();
                    // newProd = ProductName;
                    newProd = Serialno + "---" + ProductName;
                    ddlProductInfo.Items.Add(new ListItem(newProd, Serialno));
                    ddlProductInfo.SelectedItem.Value = Serialno;
                }

    Thursday, August 13, 2015 10:12 AM
  • User-265772647 posted

    foreach is working for me .Actually it is linking with the Dropdownlist selected index.It has 10 values  when I click on the 10th value for the first time it is not populating data in textboxes ,if I select previous one and then select the 10th value then it is populating values in textboxes but in dropdownlist  it is going to value'Select 'it is not showing selected one.

      if (dt.Rows.Count > 0)
                    {
                        //for (int i = 0; i < dt.Rows.Count; i++)
                        //{
    
                        //    ProductName = dt.Rows[i]["ProductName"].ToString();
    
                        //    Serialno = dt.Rows[i]["ProductID"].ToString();
                        //    // newProd = ProductName;
                        //    newProd = Serialno + "---" + ProductName;
                        //      ddlProductInfo.Items.Add(new ListItem(newProd, Serialno));
                        //      ddlProductInfo.SelectedItem.Value = Serialno;
                         
                        //}
                        foreach (DataRow row in dt.Rows)
                        {
                            //access the product name froim each datarow
                            ProductName = row["ProductName"].ToString();//thers is no  row at position 22
                            //access the ProductID froim each datarow
                            Serialno = row["ProductID"].ToString();
                            // newProd = ProductName;
                            newProd = Serialno + "---" + ProductName;
                            ddlProductInfo.Items.Add(new ListItem(newProd, Serialno));
                            ddlProductInfo.SelectedItem.Value = Serialno;
                        }
                    }

    SqlConnection cn = new SqlConnection(Connection);
            // cn.Open();
            SqlCommand cmd = new SqlCommand("select ProductID, PMNO,ProductDesc,ProductName from R2InventoryTable where ProductID=@ProductID", cn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@ProductID", ddlProductInfo.SelectedItem.Value);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "R2InventoryTable");
    
            foreach (DataRow dr in ds.Tables["R2InventoryTable"].Rows)
            {
                txtid.Text = dr["ProductID"].ToString();
                txtModel.Text = dr["PMNO"].ToString();
                txtdesc.Text = dr["ProductDesc"].ToString();
                txtProductName.Text = dr["ProductName"].ToString();
            }
    

    Thursday, August 13, 2015 10:54 AM
  • User461988170 posted

    Hi emaak,
    According to your code, I create the sample to test your code.

    emaak

    ddlProductInfo.SelectedItem.Value = Serialno;


    I think you could annotate this line code. You could add the foreach code in the Page_Load event. Add the SqlConnection in the ddlProductInfo_SelectedIndexChanged event. Then, the program is well.
    I hope this will help you.
    Best Regards,
    Candy Zhou

    Friday, August 14, 2015 6:23 AM