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 291441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:05:22+00:00 2026-05-12T06:05:22+00:00

Probably a C# noob question, so don’t flame me. I was trying to do

  • 0

Probably a C# noob question, so don’t flame me. I was trying to do this:

if (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null)
{
    // ...
}

But I kept getting a System.NullReferenceException. I thought since it returns a string that I could just check for null and move on. It took me a while to find the right way to do it:

ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
    this.Connection.ConnectionString = cs.ConnectionString;
}

So in other instances, like checking a Session object for some value I would do a check for null like this:

if (Session["EmployeeID"] != null)
{
    _EmployeeID = System.Convert.ToInt32(Session["EmployeeID"]);
}

So I just wanted to know how do you know when you can or can’t do a check for null?

  • 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-12T06:05:22+00:00Added an answer on May 12, 2026 at 6:05 am

    Your null reference exception happened in parent object (ConfigurationManager.ConnectionStrings[“PrimaryConnectionString”]). Once you check it also, you are fine.

    try this:

    if ((ConfigurationManager.ConnectionStrings["PrimaryConnectionString"] != null)
    && (ConfigurationManager.ConnectionStrings["PrimaryConnectionString"].ConnectionString != null))
      { etc etc }
    

    Notice that if you use more methods from it, it would be useful to separate these checks.

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

Sidebar

Related Questions

This is probably a noob question, but how I've implemented the appendTo() function below
sorry for the (probably) noob question, but I', new at this stuff. i have
This is probably a noob question, but I can't seem to find the answer.
This is probably a noob question that I will get slated for but here
First of all, I am a WEB NOOB. Which probably explains this question. Anyway,
Probably a noob question, but I'll go for it nevertheless. For sake of example,
Probably a noob question but interop isn't one of my strong points yet. Aside
Probably a long question for a simple solution, but here goes... I have a
This probably has a simple answer, but I must not have had enough coffee
This probably sounds really stupid but I have noo idea how to implement jquery's

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.