0

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;
        }

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by twaink
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.