• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Update MySQL from .net with parameters

I have been having problems updating a MySQL database from c# code using parameters.

I have reduced it done to the code below.

This has the effect of updating the 'head' field with NULL

MySQL 5, Visual Studio 2010

I am populating strReturn with some info for debugging and I get this from it

update newsitems set head = @Head where id = 233 ; (command text)
@Head (parameter name)
Match Fees test (parameter value)
1 (rows affected)

Can someone tell me where I am going wrong?

      string strReturn = "";
      string strSql;
      string strHead = "Match Fees test";

      strSql = "update newsitems set head = @Head where id = 233 ;";

      using (MySqlConnection conn = new MySqlConnection(strConstring))
        MySqlCommand cmd = null;



          cmd = new MySqlCommand(strSql, conn);
          cmd.CommandType = CommandType.Text;

          cmd.Parameters.AddWithValue("@Head", strHead);

          strReturn += cmd.CommandText.ToString() + Environment.NewLine ;

          for (int i = 0; i < cmd.Parameters.Count; i++)
            strReturn += cmd.Parameters[i].ParameterName.ToString() + Environment.NewLine;
            strReturn += cmd.Parameters[i].Value.ToString() + Environment.NewLine;

          int intAffected = cmd.ExecuteNonQuery();
          strReturn += intAffected.ToString() + Environment.NewLine;

        catch (Exception ex)

      return strReturn;

Open in new window

George Hirst
George Hirst
  • 3
1 Solution
Rahul AgarwalTeam LeaderCommented:
You can use like this

Dim cmdText = "UPDATE contact_log SET note = @note WHERE customer = @cust"
Using conn = new MySqlConnection(connString)
Using cmd = new MySqlCommand(cmdText, conn)
    Dim rowsAffected = cmd.ExecuteNonQuery()
End Using
End Using
George HirstAuthor Commented:
So what's different there?

Why is mine not working?
George HirstAuthor Commented:
It turns out there was not anything wrong with my code but upgrading the MySQL dot net connector to the latest version sorted the problem.
George HirstAuthor Commented:
Solution not supplied. Found the answer myself as detailed in this comment
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now