locked
Convert Boolean Values to STring RRS feed

  • Question

  • User-371148474 posted

    Helo guys,

    I have this two columns in an Order table, orderSHipped and orderCancelled....both are of data type bit with default value of (0) but, I allow IsPost to post false...

    Thing is this, for every customer order list, I want to convert the false to string "No",""Awaiting Shipment or whatever string I want and also convert true to "Yes","Already Shipped","Cancelled" or whatever string I want...

    Any tips please

    Monday, July 14, 2014 11:22 AM

Answers

  • User281315223 posted

    You can use the ternary operator for this which basically functions as an inline if-statement :

    var x = (condition) ? (value if true) : (value if false);

    This in your case would simply involve you using your boolean value as the condition and your string values as both the "true" and "false" values as seen below :

    // Set your status
    var status = orderShipped ? "Order has been shipped!" : "Order has not been shipped.";

    If you wanted to account for canceled, you could use a more complex set of ternaries :

    var status = orderCancelled ? "Order cancelled." : orderShipped ? "Order shipped." : "Order has not been shipped.";

    This would set the status as "Canceled" if the order had been canceled and if it hadn't it would set it to indicate if it had been shipped or not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 14, 2014 1:17 PM

All replies

  • User-760709272 posted

    string textToShow = boolFromDatabase ? "Thing to show if bool is true" : "Thing to show if bool is false";

    Monday, July 14, 2014 11:24 AM
  • User-1360095595 posted

    Alternatively, this can be done in the SQL select statement via a CASE block, or IIF (http://msdn.microsoft.com/en-us/library/hh213574.aspx).

    Monday, July 14, 2014 11:35 AM
  • User379720387 posted

    @metalasp.net

    only if sql 2012 and up, not for sql ce.

    Monday, July 14, 2014 1:08 PM
  • User281315223 posted

    You can use the ternary operator for this which basically functions as an inline if-statement :

    var x = (condition) ? (value if true) : (value if false);

    This in your case would simply involve you using your boolean value as the condition and your string values as both the "true" and "false" values as seen below :

    // Set your status
    var status = orderShipped ? "Order has been shipped!" : "Order has not been shipped.";

    If you wanted to account for canceled, you could use a more complex set of ternaries :

    var status = orderCancelled ? "Order cancelled." : orderShipped ? "Order shipped." : "Order has not been shipped.";

    This would set the status as "Canceled" if the order had been canceled and if it hadn't it would set it to indicate if it had been shipped or not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 14, 2014 1:17 PM
  • User-371148474 posted

    how do i use it in a foreach statement

    var order=db.Query(@"SELECT UserID,Orders.* FROM Orders
    INNER JOIN UserProfile ON Orders.oUserId = UserProfile.UserID
    WHERE UserProfile.UserID = @0
    ORDER BY OrderDateTime DESC",UserID);
     
     
     
     <div class="col-sm-12 col-md-12">
    @foreach (var o in order){
        
       string shippedstatus = o.orderShipped ? "Thing to show if bool is true" : "Thing to show if bool is false";
    }
    Monday, July 14, 2014 1:41 PM