?
Solved

c#

Posted on 2009-04-20
20
Medium Priority
?
256 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:onebite2
  • 10
  • 5
  • 4
  • +1
20 Comments
 
LVL 8

Expert Comment

by:OBonio
ID: 24186427

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

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24186545
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
 
LVL 75

Expert Comment

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

Nullable<bool> ddlApprovalStatus;

-or-

bool? ddlApprovalStatus;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Expert Comment

by:abel
ID: 24186890
@kaufmed: you probably mean the Approved field/property in the Details class?

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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24186945
I probably screwed it up : P...

in any event, the name is arbitrary, the syntax is the main idea :)
0
 

Author Comment

by:onebite2
ID: 24187196
@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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24187256
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
 
LVL 39

Expert Comment

by:abel
ID: 24187261
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
 
LVL 39

Expert Comment

by:abel
ID: 24187265
parenthesis missing, sorry (didn't test it):

if (DbNull.Value.Equals(reader["Approved"])) {
    datadetails.Approved = null;
} else {
    datadetails.Approved = Convert.ToBoolean(reader["Approved"]);
}
0
 
LVL 39

Expert Comment

by:abel
ID: 24187272
(i like kaufmeds solution better: more concise)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24187343
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
 
LVL 39

Expert Comment

by:abel
ID: 24187389
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
 

Author Comment

by:onebite2
ID: 24188162
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
 
LVL 39

Expert Comment

by:abel
ID: 24188435
> 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
 

Author Comment

by:onebite2
ID: 24188650


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
 
LVL 39

Accepted Solution

by:
abel earned 1000 total points
ID: 24188736
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
 

Author Comment

by:onebite2
ID: 24188762
Hmm....that's correct.....I didn't copy paste but I was typing..it's a typo mistake....I appreciate your help:)
0
 
LVL 39

Expert Comment

by:abel
ID: 24188783
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
 

Author Comment

by:onebite2
ID: 24188805
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
 
LVL 39

Expert Comment

by:abel
ID: 24188858
> 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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question