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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:08+00:00 2026-05-26T08:25:08+00:00

I am getting an SQL error when I try to do this: public static

  • 0

I am getting an SQL error when I try to do this:

    public static int GetOrderId(decimal totalprice, int userid)
    {
        string s = "SELECT * from orders where OrderUserId = " + userid + " and OrderTotalPrice = " + totalprice;
        cmd = new SqlCommand(s, con);
        int temporderid = Convert.ToInt32(cmd.ExecuteScalar());

        return temporderid;
    }

As far I can see its because it gets OrderTotalPrice back, the format is incompatible. But I cant figure out how to get it in the compatible format.

  • 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-26T08:25:09+00:00Added an answer on May 26, 2026 at 8:25 am

    It’s probably formatting totalprice with a comma, like 3,14, where SQL Server expects a dot, like 3.14.

    One way to fix that would be to specify InvariantCulture, which uses a dot:

    var s = string.Format(
        CultureInfo.InvariantCulture, 
        "SELECT * from orders where OrderUserId = {0} and OrderTotalPrice = {0:0.0}",
        42, 3.1415);
    

    This formats the price as 3.1 on any machine.

    By the way, it’s much nicer to pass the variables as parameters:

    var com = new SqlCommand();
    com.CommandType = CommandType.Text;
    com.CommandText = "SELECT * from orders where OrderUserId = @userid " +
        "and OrderTotalPrice = @totalprice";
    com.Parameters.AddWithValue("@userid", 42);
    com.Parameters.AddWithValue("@totalprice", 3.1415);
    var temporderid = com.ExecuteScalar();
    

    Then you don’t have to worry about formats because you send the database a double, not a double formatted as a string.

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

Sidebar

Related Questions

I'm getting the error below for this SQL statement in VB.Net 'Fill in the
I am getting this error when I try to query the database and write
I would like to understand this error I'm getting on a SQL Server 2005
I am getting this error when running a save insert/update command: SEVERE: null java.sql.SQLIntegrityConstraintViolationException:
every body. I am getting this error: You have an error in your SQL
I'm getting the error when accessing a Stored Procedure in SQL Server Server Error
I'm getting the following error when trying to setup SQL Server 2005 DB Mirroring
I am getting JDBC error when I attempt a commit through hibernate to SQL
I'm getting a timeout error when trying to execute a LINQ (-to-SQL) query System.Data.SqlClient.SqlException:
I'm on SQL Server 2005 and I am getting an error which I am

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.