0

I have a method that reads data from a database table using SqlDataReader. i assign data retrieved by SqlDataReader into a List and return it. when i want to assign the List data from method into the List on Code behind file, i encounter the following error:enter image description here

and here is my code

public List<string> displayCustoemrhShipOrder()
    {
        List<string> drList = new List<string>();
        int i = 0;

        string sConnectionString = ConfigurationManager.ConnectionStrings["LGDB"].ToString();
        SqlConnection SqlCOn = new SqlConnection(sConnectionString);
        SqlCommand SqlCmd = new SqlCommand();
        SqlCmd.Connection = SqlCOn;
        SqlCmd.CommandText = "displayCustomerShipOrder";
        SqlCmd.CommandType = CommandType.StoredProcedure;
        SqlCOn.Open();
        SqlCmd.Parameters.AddWithValue("ShipOrderID",shipOrderID);
        SqlDataReader reader = SqlCmd.ExecuteReader();
        while (reader.Read())
        {
            drList.Insert(i, reader.GetValue(i).ToString());
            i++;               
        }
        //reader.Close();
        //sqlCon.Close();
        reader.Dispose();
        SqlCOn.Dispose();
        return (drList);
    }

code behind code:

protected void Page_Load(object sender, EventArgs e)
    {

       // string shipOrderID = Session["shipOrderID"].ToString();
       //  int ID = Convert.ToInt32(shipOrderID);
        int ID = 700;
         Classes.CustomerShipOrder cuShipOrder = new Classes.CustomerShipOrder(ID);
         List<string> ordrList = new List<string>(16);
          ordrList= cuShipOrder.displayCustoemrhShipOrder();
          cuid.Text = ordrList[0];            
          Label2.Text = ordrList[1];
          Label3.Text = ordrList[2];
          Label4.Text = ordrList[3];
          Label5.Text = ordrList[4];
          Label6.Text = ordrList[5];
          Label7.Text = ordrList[6];
          Label8.Text = ordrList[7];
          Label9.Text = ordrList[8];
          Label10.Text = ordrList[9];
          Label11.Text = ordrList[10];

stored procedure used :

@ShipOrderID int

as
begin 

select * from customerShipOrder where shipOrderID = @ShipOrderID;
end
Iatrochemist
  • 256
  • 7
  • 19
  • It looks like you may be having trouble with the SQL connection and the elements in the list aren't being populated. What does visual studio have for what is assigned to those elements? It's stopping at `ordrList[1]`, so you only have one element in the list (`ordrList[0]`). On an unrelated note, what exactly are you doing with `Label2.Text`? As it currently is, it serves no purpose repeatedly assigning the same property to the same value. Also, the convention is to have the first word of an object not capitalized. – Daniel Underwood Jul 27 '13 at 05:03
  • i try to assign each value of list elements to labels that are reside in presentation layer, the stored procedure return whole columns of a table, – Iatrochemist Jul 27 '13 at 05:25

0 Answers0