Shankiyan Shanki
Shankiyan Shanki

Reputation: 30

exception occurence in mvc

****I am getting an exception of type 'System.IndexOutOfRangeException' in System.Data.dll but was not handled in user code****

    public List<ItemModel> med()
            {
                List<ItemModel> itemList = new List<ItemModel>();
                connection();
                SqlCommand cmd = new SqlCommand("procmedication_dropdown1", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PstrOperationFlag", "S-drugname");
                con.Open();
                SqlDataAdapter sd = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sd.Fill(dt);
                ItemModel item = new ItemModel();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    ItemModel io = new ItemModel();
                    while (sdr.Read())
                    {
                        io = new ItemModel();
                        io.medication = sdr["medications"].ToString();


                        itemList.Add(io);

                    }
                }
                con.Close();
                return itemList;
            }
        }
    }

Upvotes: 0

Views: 47

Answers (1)

Shahjahan
Shahjahan

Reputation: 552

Try this

    public List<ItemModel> med()
    {
        List<ItemModel> itemList = new List<ItemModel>();
        ItemModel io;
        connection();
        SqlCommand cmd = new SqlCommand("procmedication_dropdown1", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PstrOperationFlag", "S-drugname");
        con.Open();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                io = new ItemModel();
                io.medication = sdr["medications"].ToString();
                itemList.Add(io);
            }
        }
        con.Close();
        return itemList;
    }

Upvotes: 1

Related Questions