none
MySQL Connector & Update with Parameters problem RRS feed

  • Question

  • I'm trying to upload a picture to a database (actually to change it), but I encounter some problems. I use .NET 2.0 and MySQL Connector 5.0.9.

    Here's the source code:

     

    int FileSize;
       byte[] rawData;
       FileStream fs;
    
       statics.conn.Open();
       MySqlCommand cmd = new MySqlCommand();
    
       if (pathToPicture != String.Empty)
       {
        try
        {
         fs = new FileStream(pathToPicture, FileMode.Open, FileAccess.Read);
         FileSize = (int)fs.Length;
    
         rawData = new byte[FileSize];
         fs.Read(rawData, 0, FileSize);
         fs.Close();
    
         cmd.Connection = statics.conn;
         cmd.CommandText = "UPDATE main SET filesize=@size, picture=@file WHERE id=" + cat;
         cmd.Prepare();
         cmd.Parameters["@size"].Value = FileSize; //here it crashes
         cmd.Parameters["@file"].Value = rawData;/*
         cmd.Parameters.AddWithValue("@size", FileSize);
         cmd.Parameters.AddWithValue("@file", rawData);*/
         
    
    
         cmd.ExecuteNonQuery();
    
         statics.mainChanged = true;
         this.Close();
    
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
         MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
       }
    
       statics.conn.Close();
    

     

    If I use the commented code, then the parameters value remains unchanged.

    I did something wrong? Please help me, I need to resolve this urgently. Thanks in advance.

    Wednesday, November 3, 2010 1:25 PM

Answers

  • I finally resolved the problem. The MySQL Connector documentation contains mistakes. Instead of '@', it should be '?'.
    • Marked as answer by edy_3dz Wednesday, November 3, 2010 8:52 PM
    Wednesday, November 3, 2010 8:52 PM

All replies

  • For sure, it will crash because you did not create the Parameter yet to set a value to it. It should be like this

       cmd.Connection = statics.conn;
       cmd.CommandText = "UPDATE main SET filesize=@size, picture=@file WHERE id=" + cat;
       cmd.Parameters.AddWithValue("@size", FileSize);
       cmd.Parameters.AddWithValue("@file", rawData);

     


    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    Wednesday, November 3, 2010 1:45 PM
  • I wasn't clear enough, sorry. I have already tried to use this code, that I have it commented out, instead of the previous 2 line, but without any success. 
    Wednesday, November 3, 2010 2:15 PM
  • Not sure what the problem is. Are you getting an error? If so, what is it?

    If a row is not being updated (no error), then I would suspect there is no row with the specified ID (cat variable) in your table.


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Wednesday, November 3, 2010 7:04 PM
  • I finally resolved the problem. The MySQL Connector documentation contains mistakes. Instead of '@', it should be '?'.
    • Marked as answer by edy_3dz Wednesday, November 3, 2010 8:52 PM
    Wednesday, November 3, 2010 8:52 PM