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

  • Home
  • SEARCH
  • 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 8733451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:39:14+00:00 2026-06-13T09:39:14+00:00

Possible Duplicate: ExecuteScalar returns null or DBNull (development or production server) I have a

  • 0

Possible Duplicate:
ExecuteScalar returns null or DBNull (development or production server)

I have a stored procedure that checks to see if a pre-existing file id is associated to an item. If the select statement returns values, it should be true and assign “true” to the bool. however when the select statement returns a null because it doesn’t exist, my code behind still makes the .Execute return “true”

This is my stored proc:

ALTER PROCEDURE [dbo].[Events_TaskIDExists] 
@EventID int
AS
BEGIN
    select TaskID from Events where EventID = @EventID
END

and here is my code behind:

public void hasTaskAssociatedToNote()
{
    String[] Notes = hidSelectedEventIDs.Value.Split(',');
    bool exists = false;
    foreach (var note in Notes)
    {
        var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
        var command = new SqlCommand("Events_TaskIDExists", connection);
        command.Parameters.Add(new SqlParameter("@EventID", SqlDbType.Int));
        command.Parameters["@EventID"].Value = Convert.ToInt32(note.Trim());
        command.CommandType = CommandType.StoredProcedure;
        try
        {
            connection.Open();
            exists = command.ExecuteScalar() != null;//causes true when it returns null......
            var temp = command.ExecuteScalar();//this was just to check something else
            if (exists)
            {
                exhibitWarning.Visible = true;
                Warning1.Text = "There is an existing Task associated 0.";
            }
        }
        catch (SqlException sql)
        {
            lblStatus.Text = "Couldn't connect to the Database - Error";
            lblStatus.ForeColor = System.Drawing.Color.Red;
        }
        catch (Exception ex)
        {
            lblStatus.Text = "An error occured";
            lblStatus.ForeColor = System.Drawing.Color.Red;
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
    }
}
  • 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-06-13T09:39:15+00:00Added an answer on June 13, 2026 at 9:39 am

    Your exists variable should be set as:

    object result = command.ExecuteScalar();
    exists = result != DBNull.Value && result != null;
    

    A null result from SqlCommand.ExecuteScalar() returns a DBNull.Value, not null. Only an empty result set will return a null.

    Since you’re selecting TaskID based on EventID, my guess is that you don’t have your database constrained to require a TaskID for every Event, and thus you have null TaskID fields. In other words, you have the Event record containing @EventID, but not an associated Task record (based on TaskID). This condition will return a DBNull.Value rather than a null.

    “Return Value Type: System.Object The first column of the first
    row in the result set, or a null reference (Nothing in Visual Basic)
    if the result set is empty. Returns a maximum of 2033 characters.” –
    MSDN –
    SqlCommand.ExecuteScalar()

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

Sidebar

Related Questions

Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: C# driver development? I would like to know if I can do
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: How to get last inserted id? I have a table Absences |
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Trying to get tables next to each other horizontal I have two

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.