System.ArgumentException: Parameter is not valid: The following exception occurred in datagriedview

JESUS EDUARDO CHAVARIN ROCHA 81 Reputation points
2022-08-16T18:56:39.247+00:00

It happens when I change an image already stored in my database in SQL server, as type IMAGE, when I want to show it again in a picturebox, I get that error

The way I change the image is as follows:

231744-codigo.txt

ERROR:

view

This is how the employe appers at the data base:

view

SQL Server Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-08-17T21:02:22.87+00:00

    Looking a little closer at your code, it seems that the issue is exactly that you are inlining parameters - that is a more difficult to way to do it.

    When I read it last night, I though file was a file name, but I see now that it is the image itself. Thus, the parameters should be passed this way:

       query.Parameters.Add("@file", SqlDbType.VarBinary, -1)Value = file;  
    

    Also, I have a very old code sample on my web site for how to read binary data: https://www.sommarskog.se/blobload.txt. I don't know how useful it is, but you can look at it.

    (But when it comes to the pure .NET part, you should listen more to Karen, because she knows .NET a lot better than I do. I'm an SQL Server guy.


2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-08-16T21:43:55.55+00:00

    The "view" links lead me to a login window. You may want to make sure that your links are publicly available.

    I leave it to the .NET people to debug you datagridview problem, but I want to call out how you build you SQL statements:

       SqlCommand query = new SqlCommand($"UPDATE Empleados3 set NOMBRE = '{textBox1.Text}', IMAGEN = '{file}' WHERE ID = '{textBox2.Text}'", conn);  
    

    This is an absolute no-no! You must never interleave parameter values in the query text. This opens for SQL injection and it can have sever performance impact on SQL Server. It is a lot more difficult to get right, not the least with date values.

    The correct way of writing the above is:

       SqlCommand query = new SqlCommand($"UPDATE Empleados3 set NOMBRE = @name, IMAGEN = @file WHERE ID = @id", conn);  
       query.Parameters.Add("[@](/users/na/?userId=4c3710fa-15a7-0001-0000-000000000000)", SqlDbType.NVarChar, 40).Value = textBox1;  
       query.Parameters.Add("[@](/users/na/?userId=07da7035-7ffe-0003-0000-000000000000)", SqlDbType.NVarChar, 260).Value = file;  
       query.Parameters.Add("@id", SqlDbType.VarChar, 40).Value = textBox2;  
    

    Note that the actual types - that is, the second parameter - should match the types in SQL Server.


  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-16T22:56:51.867+00:00

    See my project which shows how to read and update an image using a PictureBox. The control does not matter so don't get hung up not using a DataGridView. Also I'm using a class but this will work with other containers such as a DataTable.

    Update code from a class which the form calls

    public static void UpdateImage(int id, byte[] imageBytes)  
    {  
        using (var cn = new SqlConnection(ConnectionString))  
        {  
            using (var cmd = new SqlCommand("UPDATE [dbo].[Fruits] SET [Picture] = @ByteArray  WHERE Id  = @Id", cn))  
            {  
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id;  
                cmd.Parameters.Add("@ByteArray", SqlDbType.VarBinary).Value = imageBytes;  
                cn.Open();  
                cmd.ExecuteNonQuery();  
      
            }  
      
        }  
    }  
    

    Code which calls the above method

    private void UpdateButton_Click(object sender, EventArgs e)  
    {  
        using (var ms = new MemoryStream())  
        {  
            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
            var imageData = ms.ToArray();  
            DataOperations.UpdateImage(3, imageData);  
        }  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.