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

onebite2Asked:
Who is Participating?
 
abelCommented:
If your copy/paste skills are correct, then

ddlApprovalStatus.SelectedValue = "Pending";
will not select anything and

ddlApprovalStatus.SelectedValue = "pending";
will. Just change the word in the ternary operator and you should be fine.
0
 
OBonioCommented:

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

Open in new window

0
 
abelCommented:
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");



0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
käµfm³d 👽Commented:
In conjunction with what abel said, to define as nullable:

Nullable<bool> ddlApprovalStatus;

-or-

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

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

0
 
käµfm³d 👽Commented:
I probably screwed it up : P...

in any event, the name is arbitrary, the syntax is the main idea :)
0
 
onebite2Author Commented:
@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...
0
 
käµfm³d 👽Commented:
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"]);
0
 
abelCommented:
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"]);
}
0
 
abelCommented:
parenthesis missing, sorry (didn't test it):

if (DbNull.Value.Equals(reader["Approved"])) {
    datadetails.Approved = null;
} else {
    datadetails.Approved = Convert.ToBoolean(reader["Approved"]);
}
0
 
abelCommented:
(i like kaufmeds solution better: more concise)
0
 
käµfm³d 👽Commented:
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).
0
 
abelCommented:
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.
0
 
onebite2Author Commented:
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

0
 
abelCommented:
> 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 --

0
 
onebite2Author Commented:


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........
0
 
onebite2Author Commented:
Hmm....that's correct.....I didn't copy paste but I was typing..it's a typo mistake....I appreciate your help:)
0
 
abelCommented:
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 --
0
 
onebite2Author Commented:
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();
0
 
abelCommented:
> 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.