locked
System.IndexOutOfRangeException: UnitID RRS feed

  • Question

  • User1575078971 posted

    System.IndexOutOfRangeException: UnitID error occur when displaying data in a gridview using Joining data is configured by object datasource

    class file

     public static List<Item> GetAllItem()
            {
                List<Item> listItems = new List<Item>();
    
                string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection con = new SqlConnection(CS))
                {
                    SqlCommand cmdSelectQuery = new SqlCommand("spJoinItem_MasterAndUnitAndBrand", con);
                    cmdSelectQuery.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    SqlDataReader rdr = cmdSelectQuery.ExecuteReader();
                    while (rdr.Read())
                    {
                        Item item = new Item();
                        item.ItemID = Convert.ToInt32(rdr["ItemID"]);
                        item.ItemName = rdr["ItemName"].ToString();
                        item.PurchasePrice = Convert.ToDecimal(rdr["PurchasePrice"]);
                        item.SellingPrice = Convert.ToDecimal(rdr["SellingPrice"]);
                        item.UnitID = Convert.ToInt32(rdr["UnitID"]); //error is here
                        item.BrandID = Convert.ToInt32(rdr["BrandID"]);
    
                        listItems.Add(item);
                    }
                }
    
                return listItems;
            }

    aspx.cs

    protected void btnAddItem_Click(object sender, EventArgs e)
            {
                ObjectDataSource1.InsertParameters["ItemName"].DefaultValue = txtItemName.Text;
                ObjectDataSource1.InsertParameters["PurchasePrice"].DefaultValue = txtPurchagePrice.Text;
                ObjectDataSource1.InsertParameters["SellingPrice"].DefaultValue = txtSellingPrice.Text;
                ObjectDataSource1.InsertParameters["UnitID"].DefaultValue = ddlUnit.SelectedValue;
                ObjectDataSource1.InsertParameters["BrandID"].DefaultValue = ddlBrand.SelectedValue;
    
                ObjectDataSource1.Insert();
    
                }

    Alter proc spJoinItem_MasterAndUnitAndBrand
    AS
    BEGIN
    select ItemID,ItemName, PurchasePrice,SellingPrice,UnitName, BrandName
    from Item_Master
    JOIN Unit ON Item_Master.UnitID = Unit.Id
    JOIN Brand ON Item_Master.BrandID = Brand.BrandID
    END


    Exception Details: System.IndexOutOfRangeException: UnitID
    
    Line 41:                     item.UnitID = Convert.ToInt32(rdr["UnitID"]); //error is here
    



    Wednesday, September 24, 2014 8:27 AM

Answers

  • User2103319870 posted

    harshamrita

    item.BrandID = Convert.ToInt32(rdr["BrandID"]);

    You should change your query to retrieve BrandID also , otherwise you will face null reference exception in that line also,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 24, 2014 7:23 PM

All replies

  • User3690988 posted

    You are selecting:

    select ItemID,ItemName, PurchasePrice,SellingPrice,UnitName, BrandName
    

    UnitID is not in your select statement.

    Wednesday, September 24, 2014 10:43 AM
  • User2103319870 posted

    harshamrita

    item.BrandID = Convert.ToInt32(rdr["BrandID"]);

    You should change your query to retrieve BrandID also , otherwise you will face null reference exception in that line also,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 24, 2014 7:23 PM
  • User3690988 posted

    Good catch A2H. 

    harshamrita, do you want to Select the BrandName or BrandID?  Also, it is good practice to check if what you are reading is DBNULL.

    Thursday, September 25, 2014 11:03 AM
  • User1575078971 posted

    thanks for your help

    Saturday, September 27, 2014 4:16 AM