8

I have a C# method that accepts a clientId (int) and hasPaid (boolean) that represents if the client has paid or not. The MS SQL Server stored procedure expects a BIT value (1 or 0) for the @HasPaid parameter yet the method expects a boolean type (true/false) for hasPaid. Will the ADO.NET code take care of converting the boolean to a bit type for SQL Server or do I need to convert the value of hasPaid into a 1 or 0?

public void UpdateClient(int clientId, bool hasPaid)
{
    using (SqlConnection conn = new SqlConnection(this.myConnectionString))
    {
        using (SqlCommand sqlCommand = new SqlCommand("uspUpdatePaymentStatus", conn))
        {
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("@ClientID", clientId);
            sqlCommand.Parameters.AddWithValue("@HasPaid", hasPaid);
            sqlCommand.Connection.Open();
            var rowsAffected = sqlCommand.ExecuteNonQuery();
        }
    }
}
6
  • Have you tried it? It should do the conversion for you Commented Jun 25, 2015 at 15:55
  • 2
    It should but be careful with the AddWithValue as it can exhibit some weird behavior, you're better off declaring the types explicitly Commented Jun 25, 2015 at 15:57
  • .NET will take care of this for you. SQL Server will implicitly convert most junk that resembles a Boolean type to bit, so this will be no exception. Commented Jun 25, 2015 at 15:58
  • @Dustin - I have tried it and it seems to work but I was wondering if this is the best way to do it or should I pass in 1 or 0 Commented Jun 25, 2015 at 15:59
  • 3
    Here is an article discussing the issues with AddWithValue as @bumble_bee_tuna was mentioning. blogs.msmvps.com/jcoehoorn/blog/2014/05/12/… Commented Jun 25, 2015 at 16:23

2 Answers 2

12

When working with SQL parameters I find AddWithValue's auto detection feature of the type too unreliable. I find it better to just call Add a explicitly set the type, Add also returns the new parameter it creates from the function call so you can just call .Value on it afterward.

public void UpdateClient(int clientId, bool hasPaid)
{
    using (SqlConnection conn = new SqlConnection(this.myConnectionString))
    {
        using (SqlCommand sqlCommand = new SqlCommand("uspUpdatePaymentStatus", conn))
        {
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.Add("@ClientID", SqlDbType.Int).Value = clientId;
            sqlCommand.Parameters.Add("@HasPaid", SqlDbType.Bit).Value = hasPaid;
            sqlCommand.Connection.Open();
            var rowsAffected = sqlCommand.ExecuteNonQuery();
        }
    }
}

Using the correct type is doubly important when using stored procedures and it is expecting a specific type, I just got in to the habit of always doing it this way.

Sign up to request clarification or add additional context in comments.

3 Comments

but shouldn't a Bit Parameter accept 0 or 1? It only accepts Boolean.
@MikeW it only shows up as 0 or 1 in Sql Management Studio because that's how they wanted to display it. It actually is a Boolean value.
a Bit is 0 or 1 but if you do: sqlCommand.Parameters.Add("@HasPaid", SqlDbType.Bit).Value = 1; you wil get an error when you Execute.
0

Just Convert Bool to int before sending in EF Queury Convert.ToInt16(boolValue);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.