title: Unable to cast object of type 'System.DBNull' to type 'System.String'
body: <p>I am using MVC3 ASP, and have configured my web.config file to login into the MYSQL DB as root. I have created many stored procedures which i can connect fine with. i now want to change this login user to a public user, called tempuser and NOT root anymore. </p>
  
  <p>However, when i change the login user from "root" to "tempuser", i get the error: 
  Unable to cast object of type 'System.DBNull' to type 'System.String' </p>
  
  <p>i get the above error at the execution of ExecuteNonQuery(). </p>
  
  <p>I have granted access to the functions by: 
  GRANT EXECUTE ON FUNCTION check_user_exists to tempuser@'%'; </p>
  
  <p>I have also granted 'select' and 'update' on this table that im accessing which the function uses. I can login to mysql command line as tempuser and call the function manually with no problems. But when i run ExecuteNonQuery() i get the above error. I am currently using Visual Web Developer 2010, Razor Engine, MVC3. </p>
  
  <p>ANy help, please. </p>
  
  <p>I have been trying for weeks now with no luck. </p>
  
  <p>Here is the code that is being executed. The ExecuteScalar() function is where the error is. The error is the subject of this question: However, if i login as "root" user, i dont get the error.</p>
  
  <pre><code>  [HttpPost]
      public ActionResult Register(RegisterModel model)
      {
          if (ModelState.IsValid)
          {
              // Attempt to register the user
  
              DBController dbcontroller = new DBController();
  
              if (dbcontroller.DBConnection())
              {
                  MySqlCommand command = new MySqlCommand("check_user_exists_signup", dbcontroller.conn);
                  command.CommandType = System.Data.CommandType.StoredProcedure;
  
                  // Add parameters for the check_user_exists_signup STORED FUNCTION
                  command.Parameters.Add(new MySqlParameter("@userName", model.UserName));
                  command.Parameters["@userName"].Direction = System.Data.ParameterDirection.Input;
  
                  // RETURN parameter for the insert_users STORED FUNCTION
                  MySqlParameter cnt_user = command.Parameters.Add("@cnt_user", MySqlDbType.Int32);
                  command.Parameters["@cnt_user"].Direction = System.Data.ParameterDirection.ReturnValue;
  
                  try
                  {
                      command.ExecuteScalar();
                      object ret = command.Parameters["@cnt_user"].Value;             
                      dbcontroller.conn.Close();
  </code></pre>
  
  <p>The stored proc is :</p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` FUNCTION `check_user_exists_signup`(
         userName varchar(20)) RETURNS int(11)
      DETERMINISTIC
  BEGIN
  DECLARE cnt_user int;
  
    select count(*) into cnt_user
      from users 
     where user_name = userName;
  
  RETURN cnt_user;
  
  END
  </code></pre>