how does highlighted lines work.............

public List<clsproductprp> RetAllCol_rec()
        { 
            List<clsproductprp> obj = new List<clsproductprp>();
            SqlDataReader dr=null;
            SqlCommand discmd = new SqlCommand();
             try
             {
                 if (con.State == ConnectionState.Closed)
                 {
                     con.Open();
                 }

                 discmd.CommandText = "spDisplayALLProduct";
                 discmd.CommandType = CommandType.StoredProcedure;
                 discmd.Connection = con;

                [B] dr = discmd.ExecuteReader();[/B]
                
                 while (dr.Read())
                 {
                     clsproductprp prp = new clsproductprp();
                     prp.productnameprp = dr["Producname"].ToString();
                     prp.Quantityperunitprp = Convert.ToInt32(dr["Quantityperunit"].ToString());
                     prp.unitinStockprp = Convert.ToInt32(dr["unitinstock"].ToString());
                     prp.unitPriceprp = Convert.ToInt32(dr["unitPrice"].ToString());
                     prp.productidprp = Convert.ToInt32(dr["Productid"].ToString());
                    [B] obj.Add(prp);[/B]
                 }
             }
             catch { }
             finally
             {
                 dr.Close();
                 discmd.Dispose();
             }
             return obj;
        }

how does highlighted lines work.............

public List<clsproductprp> RetAllCol_rec()
        { 
            List<clsproductprp> obj = new List<clsproductprp>();
            SqlDataReader dr=null;
            SqlCommand discmd = new SqlCommand();
             try
             {
                 if (con.State == ConnectionState.Closed)
                 {
                     con.Open();
                 }

                 discmd.CommandText = "spDisplayALLProduct";
                 discmd.CommandType = CommandType.StoredProcedure;
                 discmd.Connection = con;

                [B] dr = discmd.ExecuteReader();[/B]
                
                 while (dr.Read())
                 {
                     clsproductprp prp = new clsproductprp();
                     prp.productnameprp = dr["Producname"].ToString();
                     prp.Quantityperunitprp = Convert.ToInt32(dr["Quantityperunit"].ToString());
                     prp.unitinStockprp = Convert.ToInt32(dr["unitinstock"].ToString());
                     prp.unitPriceprp = Convert.ToInt32(dr["unitPrice"].ToString());
                     prp.productidprp = Convert.ToInt32(dr["Productid"].ToString());
                    [B] obj.Add(prp);[/B]
                 }
             }
             catch { }
             finally
             {
                 dr.Close();
                 discmd.Dispose();
             }
             return obj;
        }

the ExecuteReader method of the Command object executes the command and creates the Reader object, which now contains your data. This works in a read-only, forward-only way, storing all your data in a buffer. Then each row is read while there are still rows to read. You may wish to check that the reader contains something before you attempt to read, using something like

if(dr.HasRows())

. It looks like properties of a retrieved row are assigned to an object and that object is added to the List obj by the lower highlighted line.
Here is a link that may also help:
http://msdn.microsoft.com/en-us/library/haa3afyz(VS.71).aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.