5

I'm adding a parameter to be called with a MySQL stored procedure like

List<MySqlParameter> MyParams = new List<MySqlParameter>();    
MyParams.Add(new MySqlParameter("MyId", 0));

But for some reason when I look at MyParams, MyId value when stepping through my code, it is converted to null. Does anyone know why this is because if I assign the value from a int variable like below it is fine

int id = 0;
List<MySqlParameter> MyParams = new List<MySqlParameter>(); 
MyParams.Add(new MySqlParameter("MyId", id));
4
  • Post the code for MySqlParameter, without it this question is unanswerable. Commented Sep 13, 2013 at 14:17
  • MySqlParameter is part of the MySql .Net API :dev.mysql.com/doc/refman/5.0/en/… Commented Sep 13, 2013 at 14:21
  • OK, that My* stuff. The we still need to see how MyParams gets pushed to the Command. Commented Sep 13, 2013 at 15:13
  • 2
    @downvoter Can you give a reason for down-voting ? Commented Sep 13, 2013 at 15:38

3 Answers 3

10

Well, You fell into the corner case of c# that literal 0 can be converted to Enum implicitly

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type

Reference

So, new MySqlParameter("MyId", 0) is compiled into MySqlParameter(string,MySqlDbType) rather than MySqlParameter(string,object) as the result your value 0 is ignored.

new MySqlParameter("MyId", id) this works because implicit conversions to enum works only when the value is literal not for variables. So It is clear that this gets compiled into MySqlParameter(string,object) resulting the expected results.

new MySqlParameter("MyId", (object)0)//this solves the problem

or this

New MySqlParameter("MyId", MySqlDbType.Int).Value = 0

BTW as @Suraj Singh pointed you may have to use @MyId instead of MyId.

Hope this helps

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

Comments

2

Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero ---MSDN Hope it's applicable for MySql too.

MyParams.Add(New MySqlParameter("@MyId", MySqlDbType.int)).Value = 0;

or try

Parameters.AddWithValue("@MyId", 0);

Comments

0

In your database schema, Is MyId a Primary Key of type int? with allow null set to yes?

assigning a value of 0 to a PK with allow null, will result in NULL being assigned. I suspect its the database setup and not the code at fault.

5 Comments

MyId is part of a PrimaryKey of type INT. In the table schema the column is set to NOT NULL
But 0 is not null.
^agree. Why would you want to insert 0 as a PK? what happens when you insert '1' instead?
@AaronHopkins MyId is PART of a PrimaryKey, not a single column primary key
Your database, your rules. No worries.

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.