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;

        try
        {

          conn.Open();

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

          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)
        {
        }
        finally
        {
          conn.Close();
        }
      }

      return strReturn;

Open in new window

LVL 13
Atdhe NuhiuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
    conn.Open()
    cmd.Parameters.AddWithValue("@note",TextBox2.Text)
    cmd.Parameters.AddWithValue("@cust",Request.QueryString("ID"))
    Dim rowsAffected = cmd.ExecuteNonQuery()
End Using
End Using
0
Atdhe NuhiuAuthor Commented:
So what's different there?

Why is mine not working?
0
Atdhe NuhiuAuthor 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Atdhe NuhiuAuthor Commented:
Solution not supplied. Found the answer myself as detailed in this comment
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.