none
Unable to save to image field RRS feed

  • Question

  • Hi

    I have a table as below;

     CREATE TABLE [dbo].[iv_users_test](
     [PKey] [int] IDENTITY(1,1) NOT NULL,
     [user_name] [nvarchar](255) NULL,
     [CVFileContent] [image] NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    

    I am using below code to try to insert a byte array into the image filed;

    byte[] bytes = new byte[1000];
    
    //fill array bytes here
    
    SQLStr = "INSERT INTO [dbo].[iv_users_test] ([user_name], [CVFileContent]) " +
      "VALUES (@user_name, @CVFileContent)";
    
    SqlCommand SQLComm = new SqlCommand(SQLStr, SQLConn);
    
    SQLComm.Parameters.AddWithValue("@user_name", "a user");
    
    SQLComm.Parameters.AddWithValue(" @CVFileContent", bytes);
    
    SQLComm.ExecuteNonQuery();
    

    Problem is I get the below error on the line SQLComm.ExecuteNonQuery().

    System.Data.SqlClient.SqlException was unhandled by user code
     Message=Incorrect syntax near '@CVFileContent'.
    Must declare the scalar variable "@CVFileContent".
     Source=.Net SqlClient Data Provider
     ErrorCode=-2146232060
     Class=15
     LineNumber=1
     Number=102
     Procedure=""
     Server=MYSERVER\SQLEXPRESS
     State=1
    

    What is the problem and how can I fix it?

    Thanks

    Regards

     

    Thursday, February 10, 2011 12:27 AM

Answers

  • First, I would remove the space preceding @CVFileContent in the AddWithValue statement and try running your code again.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by Y a h y a Thursday, February 10, 2011 7:33 PM
    Thursday, February 10, 2011 1:52 PM

All replies