0
Answer

Error in results of mysql routine called in c#

Matthew Cox

Matthew Cox

14y
4.3k
1


I am designing a Form for data entry to a mysql database. Nothing fancy. This is my first time using c# to communicate with a mysql db. I get a very strange error in results.

Whenever I try to utilize a stored procedure call 'Insert_manacost_bridge_entry(pColor VARCHAR, pCost INT, pCard_ID INT)' I get the following result: The value passed in the pCost parameter is in place of the pColor field in the db record.

I attached the C# code and the mysql routine.

Testing done with weird results:

if I invoke the routine by performing a Call from the MySqlCommand as shown below and pass the params right there, the result is recorded correctly. If I uncomment the Params.Add(..) lines ... this is when pCost is in the place of pColor while pCost will be a 0 value in the db.




MySqlCommand cmd2 = conn.CreateCommand();
cmd2.CommandText = "call Insert_Manacost_Bridge_Entry('halp',2,2)";

cmd2.CommandType = System.Data.CommandType.Text;

//cmd2.Parameters.Add("pColor", MySqlDbType.VarChar).Value = "RED";
//cmd2.Parameters.Add("pCost", MySqlDbType.Int32).Value = 1111;
//cmd2.Parameters.Add("pCard_ID", MySqlDbType.Int32).Value = 14;
//cmd2.ExecuteNonQuery();

CREATE DEFINER=`root`@`localhost` PROCEDURE `Insert_Manacost_Bridge_Entry`(pColor VARCHAR(4), pCost INT, pCard_ID INT)

BEGIN

        DECLARE card_mana_ID INT DEFAULT 0;
        
	DECLARE manacost_ID INT DEFAULT 0;
        
	SET manacost_ID = ((SELECT COUNT(*) FROM manacost_entry) + 1);
        
	SET card_mana_ID = ((SELECT COUNT(*) FROM bridge_card_manacost) + 1);
        

        INSERT INTO manacost_entry (manacost_ID, Color, Cost)VALUES (manacost_ID, pColor, pCost);
            

        INSERT INTO bridge_card_manacost (card_mana_ID, card_ID, manacost_ID) VALUES (card_mana_ID, pCard_ID, manacost_ID);
END