2

Since MySQL doesn't allow default values for parameters, i have been trying to send Null values from the code but unfortunately nothing is working.

I have read many questions here and i know that there is a workaround by sending empty string and then doing an if-statement in the stored procedure.

My question is, isn't there anyway i could send a value from C# that MySql could interpret as a null value. Reason i am asking this cause i am using IS NULL checks which is already implemented in all of sp's becuase i have migrated from MSSQL to MySQL.

I have tried regular null and DBNull.Value but i keep getting an Unhandled type encountered exception.

CREATE DEFINER=`root`@`localhost` PROCEDURE `User_Check_Avialability`(  
   IN Username NVARCHAR(50),
   IN Email NVARCHAR(50)
    )

BEGIN   
    SELECT  COUNT(*) AS NumberOfUsers
    FROM    User
    WHERE   (( User.Username = Username OR Username IS NULL ) AND ( User.Email = Email OR Email IS NULL ));

END

Here is my code:

    try
{
    using (MySqlConnection mySqlConnection = new MySqlConnection(Settings.Default.ConnectionString))
    {
        using (MySqlCommand mySqlCommand = new MySqlCommand())
        {
            mySqlCommand.Connection = mySqlConnection;
            mySqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
            mySqlCommand.CommandText = "User_Check_Avialability";

            if (!string.IsNullOrEmpty(objUsers.Username))
                mySqlCommand.Parameters.AddWithValue("Username", objUsers.Username);
            else
                mySqlCommand.Parameters.AddWithValue("Username", DBNull.Value); //Also tried null                      

            if (!string.IsNullOrEmpty(objUsers.Email))
                mySqlCommand.Parameters.AddWithValue("Email", objUsers.Email);
            else
                mySqlCommand.Parameters.AddWithValue("Email", DBNull.Value); //Also tried null

//Also tried
mySqlCommand.Parameters["Username"].IsNullable = true;
mySqlCommand.Parameters["Email"].IsNullable = true;

            bool isAvailable = false;

            mySqlConnection.Open();
            using (MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
            {
                if (!mySqlDataReader.HasRows)
                    isAvailable = true;
                else
                {
                    while (mySqlDataReader.Read())
                    {
                        if (int.Parse(mySqlDataReader["NumberOfUsers"].ToString()) == 0)
                            isAvailable = true;
                    }
                }
            }

            return isAvailable;
        }
    }
}
catch (Exception ex)
{
    throw new Exception(ex.ToString());
}
6
  • sp posted please. 5 lines
    – Drew
    Oct 29, 2015 at 4:23
  • 1
    Have you tried using isNullable parameter? dev.mysql.com/doc/connector-net/en/… Oct 29, 2015 at 4:23
  • @shree.pat18, is there any special way to do it. I tried both null & DBNull.Value along with setting parameters to IsNullable but same exception occurs.
    – ykh
    Oct 29, 2015 at 4:32
  • Can you share the C# code as well? I can't test myself, but looking at the code may help others to answer also. Oct 29, 2015 at 4:33
  • 3
    Did you try using Add instead of AddWithValue? That would let you set the type of parameter, which I think is what's missing when using AddWithValue. Oct 29, 2015 at 4:39

1 Answer 1

3

Thanks to the valuable comments, the solution is that you should specify the type of the parameter and make it Nullable.

Here is the sample code:

                    mySqlCommand.Parameters.Add("Username", MySqlDbType.VarChar);
                    mySqlCommand.Parameters["Username"].Direction = System.Data.ParameterDirection.Input;
                    mySqlCommand.Parameters["Username"].IsNullable = true;

                    if (!string.IsNullOrEmpty(objUsers.Name))
                        mySqlCommand.Parameters["Username"].Value = objUsers.Name;

As you can see here, even if you don't pass a null value there will be no problem because the parameter is already added and if there is no value then it will be null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.