Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 427607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:35:35+00:00 2026-05-12T19:35:35+00:00

I have this in my code: SelectList(blah, blah, blah, cu.Customer.CustomerID.ToString()) It gives a error

  • 0

I have this in my code:

SelectList(blah, "blah", "blah", cu.Customer.CustomerID.ToString())

It gives a error when it returns null, how can I make it CustomerID is an empty string if it is null?

/M

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:35:35+00:00Added an answer on May 12, 2026 at 7:35 pm

    (Update for C# 6.0)

    If you are using C# 6 or newer (Visual Studio 2015 or newer), then you can achieve this using the null-conditional operator ?.:

    var customerId = cu.Customer?.CustomerId.ToString() ?? "";
    

    One useful property of the null-conditional operator is that it can also be “chained” if you want to test if several nested properties are null:

    // ensure (a != null) && (b != null) && (c != null) before invoking
    // a.b.c.CustomerId, otherwise return "" (short circuited at first encountered null)
    var customerId = a?.b?.c?.CustomerId.ToString() ?? "";
    

    For C# versions prior to 6.0 (VS2013 or older), you could coalesce it like this:

    string customerId = cu.Customer != null ? cu.Customer.CustomerID.ToString() : "";
    

    Simply check if the object is non-null before you try to access its members, and return an empty string otherwise.

    Apart from that, there are situations where null object pattern is useful. That would mean that you ensure that your Customer‘s parent class (type of cu in this case) always return an actual instance of an object, even if it is “Empty”. Check this link for an example, if you think it may apply to your problem: How do I create a Null Object in C#.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have this code in my view.. <%=Html.DropDownList(ProductTemplate,new SelectList(Model.ProductTemplate,Value,Text))%> I know if this
I have this code: ie1.link(:text, /Exception:/) It is producing an error message which I
i have this code in View @using (Html.BeginForm()) { @: Location @Html.DropDownList(territryID, (SelectList)ViewBag.Territory, choose
I have this code: @this.Html.DropDownListFor(vm => vm.FishId, new SelectList(this.Model.Fishies, FishId, FishName, this.Model.FishId), Please Select
I have this code: columns.ForeignKey(p => p.SomeProperty, new SelectList(new[] { 0, 1, 2, 3,
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have this code for changing the image of a button: - (void)mouseEntered:(NSEvent *)event
I have this code in XAML <Grid x:Name=LayoutRoot Background=Transparent> <Grid.RowDefinitions> <RowDefinition Height=Auto/> <RowDefinition Height=*/>
I have this code: EditText value = ( EditText )findViewById( R.id.editbox ); Integer int_value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.