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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:35:53+00:00 2026-05-28T20:35:53+00:00

We have a block of code that creates a list of sqlParameters and then

  • 0

We have a block of code that creates a list of sqlParameters and then passes them to the stored procedure. A requirement came in to conditionally null a few of the values based on whether or not the input textbox is blank.

Base code:

var SqlParams = new List<SqlParameter> {
    new SqlParameter("@SomeFloat", SqlDbType.FLoat) {Value = f},
    ....
}

I tried a few variations on something, but it didn’t work and VS gives an error about no implicit conversion between float and null.

new SqlParameter("@SomeFloat", SqlDbType.Float) {Value = (!string.IsNullOrEmpty(tb.Text) ? double.Parse(tb.Text) : DBNull.Value)},

Is there a way to do this maintaining the list of parameters so that we dont have to rewrite the whole module?

Thanks

  • 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-28T20:35:54+00:00Added an answer on May 28, 2026 at 8:35 pm

    You’re almost there, you just have to make sure the conditional operator knows what its result should be. Cast one of the double.Parse or DBNull.Value to object

    I’ve also removed some brackets and swapped the condition to make it easier to remove.

    new SqlParameter("@SomeFloat", SqlDbType.Float) 
        {
            Value = string.IsNullOrEmpty(tb.Text) 
                ? DBNull.Value
                : (object)double.Parse(tb.Text)
        }
    

    However I would normally do something like this

    if (!string.IsNullOrEmpty(tb.Text))
         command.Parameters.AddWithValue("@SomeFloat", double.Parse(tb.Text));
    

    And not specify the value if the textbox is blank and let the stored procedure handle that case.

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

Sidebar

Related Questions

I have a block of code that is repeated within a DB2 stored procedure.
I have a block of code that works and I wanted to ask what
I have a block of code that basically intializes several classes, but they are
I have a block of code that serializes a type into a Html tag.
I have a block of JSP code that needs to be used in several
I have a small code block that should append text to the beg of
I have a submit button that is a block of HTML code because of
I have a code snippet written in PHP that pulls a block of text
I have written code that automatically creates CSS sprites based on the IMG tags
If you have some blocks of code that you would like to prevent execution

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.