Link to home
Start Free TrialLog in
Avatar of onebite2
onebite2

asked on

c#

below code says when details.approved = true the selected value will be approved,if it is false it is denied...Now i want an other clause if it is null it should be equal to pending.....how can I add this in the below line....not sure about c# syntax.....
ddlApprovalStatus.SelectedValue = (details.Approved == true ? "Approved" : "Denied");

Open in new window

Avatar of OBonio
OBonio
Flag of United Kingdom of Great Britain and Northern Ireland image


ddlApprovalStatus.SelectedValue = (details.Approved == null ? "Pending" : (details.Approved == true ? "Approved" : "Denied"));

Open in new window

if Approved is a boolean, then it cannot be null. If it is a Nullable class, then it can be null, but needs a bit tweaking:

ddlApprovalStatus.SelectedValue =
           !details.Approved.HasValue ? "Pending" :
           (details.Approved == true ? "Approved" : "Denied");



Avatar of kaufmed
In conjunction with what abel said, to define as nullable:

Nullable<bool> ddlApprovalStatus;

-or-

bool? ddlApprovalStatus;
@kaufmed: you probably mean the Approved field/property in the Details class?

private bool? _bool = null;public bool? Approved{    get    {        return _bool;    }}

I probably screwed it up : P...

in any event, the name is arbitrary, the syntax is the main idea :)
Avatar of onebite2
onebite2

ASKER

@abel

Approved field is actually a boolean....

details class has this :
private bool? _bool = null;
public bool? Approved
{
   get
   {
       return _bool;
   }
}

In the data layer where I am reading this field value from database do I need to add any line of code....below is the line in the datalayer....
datadetails.Approved = Convert.ToBoolean(reader["Approved"]);

now it is throwing an error.....
Object cannot be cast from DBNull to other types.

I appreciate if you can tell me a solution...
null and DBNull are not the same things. So you have to check for DBNull when reading from a reader:

    datadetails.Approved = reader.IsDbNull(columnIndex_of_Approved_in_reader) ? null : Convert.ToBoolean(reader["Approved"]);
yeah, that's on the other end of the line... try this

if (DbNull.Value.Equals(reader["Approved"]) {
    datadetails.Approved = null;
} else {
    datadetails.Approved = Convert.ToBoolean(reader["Approved"]);
}
parenthesis missing, sorry (didn't test it):

if (DbNull.Value.Equals(reader["Approved"])) {
    datadetails.Approved = null;
} else {
    datadetails.Approved = Convert.ToBoolean(reader["Approved"]);
}
(i like kaufmeds solution better: more concise)
I don't know though, I don't like switching between column index and column name when working with readers (seems cluttered to me), so I might be inclined to incorporate you suggestion. (I've haven't really used the DBNull.Value approach before).
I never use the reader at all, anyway, lol. Whenever I do DB, I let it be done by the ORM so that I don't have to worry about these little things. It takes more time to setup, but it gives you more value in the end. Personally, I like NHibernate for that task.
If I use kaufmed solution of :    datadetails.Approved = reader.IsDbNull(columnIndex_of_Approved_in_reader) ? null : Convert.ToBoolean(reader["Approved"]);

  Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'bool'      

it is throwing an error...

But when I use abel's code as the code snippet attached it is not displaying the selected value in the drop down list....


in the ddlapprovalstatus.selectedvalue it is reading blank""...what should I do????



datalayer.cs:(here it is going through and reading through the clause correctly) 
   if (DBNull.Value.Equals(reader["Approved"]))
                            {
                                datadetails.Approved = null;
                            }
                            else
                            {
                                datadetails.Approved = Convert.ToBoolean(reader["Approved"]);
                            }
                            
 
Businessobjects:
 private bool? _approved=null;
    public bool? Approved
        {
            get { return _approved; }
            set { _approved = value; }
        }
 
.aspx.cs:
 
            ddlApprovalStatus.SelectedValue = !details.Approved.HasValue ? "Pending" :(details.Approved == true ? "Approved" : "Denied");

Open in new window

> there is no implicit conversion between '<null>' and 'bool'  

that probably means the receiving boolean is not nullable. Or there's another error. You can split the statement in an if-statement (which is always a good idea if the "?:" ternary operator is not well readable).

> it is not displaying the selected value in the drop down list....
> in the ddlapprovalstatus.selectedvalue it is reading blank""...what should I do????

take a beer and some sunshine ;)

No, seriously. What you should do is check what's currently in the dropdownlist. Can it be that the dropdownlist does not contain a selectedvalue of type Pending|Approved|Denied? You can list the items by:

foreach (ListItem item in ddlApprovalStatus.Items)
    Debug.WriteLine(item.value);
which should show all the options that are currently loaded in the output window.

-- Abel --



dropdownlist does not contain a selectedvalue of type Pending|Approved|Denied?

Yeah the drop down list has another value.....these are all hard coded Not set|pending|Approved|Denied...the not set may not come in any criteria that happens when there is no record in that particular table........
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm....that's correct.....I didn't copy paste but I was typing..it's a typo mistake....I appreciate your help:)
Glad you found it. You know, it is often better to use the Index property instead of the Value property, because the index is less error prone to typo's... :)

glad it helped

-- Abel --
abel

I have a similar simple question ....I appreciate if you can help me that.....posting a different question doesn't help at all......

I have a   string which reads the data from the database correctly but it displays blank in the

ddlFileType.selectedvalue.....can you help me with this???

     ddlFileType.SelectedValue = details.FileType.ToString();
> posting a different question doesn't help at all......

I don't agree. Posting a related question will give me an extra notification (and any experts listening in on this thread) and it will give others the opportunity to help you and you have more chance of getting the correct help.

As to your question, the answer is similar to my earlier comment. Create a simple debug.write loop to find out what's in and what's not in that dropdownlist,. Put a breakpoint just after that loop and copy your details.FileType.ToString() inside the Immediate window. Now you'll likely find that the one is not in the other and cannot be selected.