locked
Int.32 Parsing with dataSource RRS feed

  • Question

  • User-1931474774 posted

    Hi guys i am following a tutorial in a book the code works but I dont understand how and why enough. I have two snippets of code the first one immediately below I can see that orderRow is holding the results from table.rows and that the values in [] brackets are being identified or extracted based on orderRow.

    I dont quite understand why the values are being parsed I dont know if the are being turned into a string or int when they are parsed. Could someone explain what the code is doing.

     

    public static OrderInfo GetInfo(string orderID)
        { 
            //get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "OrderGetInfo";
            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@OrderID";
            param.Value = orderID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);
            // obtain the results
            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            DataRow orderRow = table.Rows[0];
            
    // save the results into an OrderInfo object
            OrderInfo orderInfo;
            orderInfo.OrderID = Int32.Parse(orderRow["OrderID"].ToString());
            orderInfo.TotalAmount = Decimal.Parse(orderRow["TotalAmount"].ToString());
            orderInfo.DateCreated = orderRow["DateCreated"].ToString();
            orderInfo.DateShipped = orderRow["DateShipped"].ToString();
            orderInfo.Verified = bool.Parse(orderRow["Verified"].ToString());
            orderInfo.Completed = bool.Parse(orderRow["Completed"].ToString());
            orderInfo.Canceled = bool.Parse(orderRow["Canceled"].ToString());
            orderInfo.Comments = orderRow["Comments"].ToString();
            orderInfo.CustomerName = orderRow["CustomerName"].ToString();
            orderInfo.ShippingAddress = orderRow["ShippingAddress"].ToString();
            orderInfo.CustomerEmail = orderRow["CustomerEmail"].ToString();
            // return the OrderInfo object
            return orderInfo;
     
    I have the same lack of understanding for the second snippet of code below, but unlike orderRow above that holds the values from the table.row 
    instance based on the stored procedure I cant see where orderDetailRow is getting its information values from. see code below
     
    public class CommerceLibOrderDetailInfo
    {
        public int OrderID;
        public int ProductID;
        public string ProductName;
        public int Quantity;
        public double UnitCost;
        public string ItemAsString;
    
        public double Subtotal
        {
            get
            {
                return Quantity * UnitCost;
            }
        }
    
        public CommerceLibOrderDetailInfo(DataRow orderDetailRow)
        {
            OrderID = Int32.Parse(orderDetailRow["OrderID"].ToString());
            ProductID = Int32.Parse(orderDetailRow["ProductId"].ToString());
            ProductName = orderDetailRow["ProductName"].ToString();
            Quantity = Int32.Parse(orderDetailRow["Quantity"].ToString());
            UnitCost = Double.Parse(orderDetailRow["UnitCost"].ToString());
            // set info property
            Refresh();

     In the first code snippet orderRow gets its value from the DataTable (table) object based on the execution and result of the OrderGetInfo stored procedure

    In the second code snippet I cant figure out how orderDetailRow obtains its data values.

    Can someone please explain how parsing works and how orderDetailRow in the second code snippet gets its data.

     

    Sunday, October 9, 2011 3:27 PM

Answers

  • User3866881 posted

    My question now is, why isn't that parameter  orderDetail used as the parameter in CommerceLibOrderDetailInfo function instead of the name orderDetailRow, do they represent the same thing which would be the data held in the DetailsData.Rows. Instead of having

    public CommerceLibOrderDetailInfo(DataRow orderDetailRow)
    {
    }

    as the consrructor we could have

    public CommerceLibOrderDetailInfo(DataRow orderDetail)

    Hello again:)

    Both of the constructor's defination are OK, but the only difference, in my view, is just the parameter name. The latter is lacking of "Row". In fact, the reason why we define orderDetailRow means this is a type of DataRow, which is clearer than the previous one.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2011 9:16 PM

All replies

  • User3866881 posted

    I dont quite understand why the values are being parsed I dont know if the are being turned into a string or int when they are parsed. Could someone explain what the code is doing.

    Hello:)

    The first snippet of codes you've offered to us means to Convert From a DataTable into an Object (OOP-based conversion).  And

    DataRow orderRow = table.Rows[0];

    The statement I've referred with above means you fetch the first row of the DataTable, and DataTable is a memory-based table that may have many rows, these rows are called "RowCollection" inside it.

    orderInfo.OrderID = Int32.Parse(orderRow["OrderID"].ToString());

    Next the codes mean to convert the OrderId into Int32——you should know that any type of values in DataTable is always of type of object, but we don't want to do this because this will make us feel confused with which type we REALLY want. So we should do a conversion——And Int32.Parse is just converting from object to int.

    Can someone please explain how parsing works and how orderDetailRow in the second code snippet gets its data.

    The second way is very similar to the 1st one but it's been packaged as the form of function. What you do is just pass a DataRow instance into the function by calling it exclipitly, and then assign each column's value belonging to the row to the public properties or variables.

    PS: What's your OrderID  or ProductId? Are they properties of your object?

    Monday, October 10, 2011 9:35 PM
  • User-1931474774 posted

    Hi Decker, first I must say thanks for replying, You have explained well and seem to know what you are talking about. However where the second code snippet is concerned my question still remains. where does orderDetailRow gets its values from I can see that it's an instance of DataRow in a function

    In the CommerceLibOrderDetailInfo function (seecond snippet) all the values are column headings of a table called Orderdetails, but unlike the first code  snippett that actually runs a stored procedure "OrderGetInfo" to gets its data that is then being held in orderRow, orderDetailRow does not get its data from a stored procedure or anything so when this function is called how will it know what information to run. its parsing all the column names and is called in the program and actually works but I cant understand how the values are being given to the variables in this function. How does DataRow obtain ita actual data values that it's currently holding.

    Or is there some oher function that is holding the values fore these data members somewhere else. There is another  function with the same name however it returns a List . Have a look and tell me if it is this function that is actually settting the values for the data members in the CommerceLibOrderDetailInfo

    function. (talking about the second snippet in my initial post)     see code below 

    It is creating a new CommerceLibOrderDetailInfo instance if you look clearly using this line of code   see code below                                           orderDetails.Add(new CommerceLibOrderDetailInfo(orderDetail));

    public static List<CommerceLibOrderDetailInfo> GetOrderDetails(string orderId)
        {
            // use existing method for DataTable
            DataTable orderDetailsData = OrdersAccess.GetDetails(orderId);
            // create List<>
            List<CommerceLibOrderDetailInfo> orderDetails =
              new List<CommerceLibOrderDetailInfo>(orderDetailsData.Rows.Count);
            foreach (DataRow orderDetail in orderDetailsData.Rows)
            {
                orderDetails.Add(new CommerceLibOrderDetailInfo(orderDetail));
            }
            return orderDetails;
        }

    I am only guessing here so please let me know if my assumptions are wrong. If I am right would the parameter  orderDetail be the equivalent of orderDetailRow in the CommerceLibOrderDetailInfo function.

    PS: Thanks for explaining Parsing, my understand is that any values held in a DataTable is of type object which is not really useful, therefore its necessary to convert each value into the relevant datatype it was originally set to  when the database tables were created. Is my understanding correct? and if it is why do we bother to use the ToString method which turns it into a string before turning it back into an int.

    Tuesday, October 11, 2011 3:44 AM
  • User3866881 posted

    where does orderDetailRow gets its values from I can see that it's an instance of DataRow in a function

    Yes, what you think of is right. You cannot leave it null, or error will be thrown out. And the type of "orderDetailRow" is type of DataRow.

    DataTable orderDetailsData = OrdersAccess.GetDetails(orderId);

    This will get details information of orderDetailsData as a DataTable.

    foreach (DataRow orderDetail in orderDetailsData.Rows) { orderDetails.Add(new CommerceLibOrderDetailInfo(orderDetail)); }

    And this will loop the orderDetailsData.Rows to fetch each DataRow, and assign each row into CommerceLibOrderDetailInfo as it constructor's parameter.

    Tuesday, October 11, 2011 3:57 AM
  • User3866881 posted

    my understand is that any values held in a DataTable is of type object which is not really useful, therefore its necessary to convert each value into the relevant datatype it was originally set to  when the database tables were created. Is my understanding correct?

    yes, you are right. To specific a real type is better than a generic type of object.

    if it is why do we bother to use the ToString method which turns it into a string before turning it back into an int.

    Because every object must inherit ToString() method. So this can be reguarded as a temp site for you to continue your next step.

    Tuesday, October 11, 2011 3:59 AM
  • User-1931474774 posted

    Hi Decker you have been most helpful I do have a better understanding now, these two classes and functions have been baffling me for a while now.

    I still want to t make sure, you said, there will be looping through "orderDetailsData.Rows to fetch each DataRow, and assign each row into CommerceLibOrderDetailInfo as it constructor's parameter".

    In the CommerceLibOrderDetailInfo function the name of the DataRow instance is now called orderDetailRow.  In List<CommerceLibOrderDetailInfo> GetOrderDetails (my 3rd code snippet) ,which is the function that gets the values for  the function CommerceLibOrderDetailInfo, the parameter for the "orderDetails.Add(new CommerceLibOrderDetailInfo(orderDetail));" ,which I assume holds all the data ,is referred to as orderDetail.

    My question now is, why isn't that parameter  orderDetail used as the parameter in CommerceLibOrderDetailInfo function instead of the name orderDetailRow, do they represent the same thing which would be the data held in the DetailsData.Rows. Instead of having

    public CommerceLibOrderDetailInfo(DataRow orderDetailRow)
    {
    }

    as the consrructor we could have

    public CommerceLibOrderDetailInfo(DataRow orderDetail)

    If you study my first code snippet you will see a line "DataRow orderRow = table.Rows[0];"

    orderRow is holding the values of that  table.Rows object and when the information is being parsed it is the same name orderRow that is being refferenced to do the parsing. Compared to my third code snippett the variable orderDetail holding the  actual data seemed to have been renamed to

    orderDetailRow and it is orderDetailRow that is now being parsed in the function CommerceLibOrderDetailInfo. So how do you expalin that, and why did they chose now to use a List object in one function to then lood values into another function if a stored procedure could have obtained the values in one function like the first code snippet. I do understad that the OrderDetails table can hvae many lines of products about the same order but why do each row needs to now be held in a list object.

    Tuesday, October 11, 2011 2:16 PM
  • User3866881 posted

    My question now is, why isn't that parameter  orderDetail used as the parameter in CommerceLibOrderDetailInfo function instead of the name orderDetailRow, do they represent the same thing which would be the data held in the DetailsData.Rows. Instead of having

    public CommerceLibOrderDetailInfo(DataRow orderDetailRow)
    {
    }

    as the consrructor we could have

    public CommerceLibOrderDetailInfo(DataRow orderDetail)

    Hello again:)

    Both of the constructor's defination are OK, but the only difference, in my view, is just the parameter name. The latter is lacking of "Row". In fact, the reason why we define orderDetailRow means this is a type of DataRow, which is clearer than the previous one.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2011 9:16 PM