Getting error: collision of type Operand: nvarchar is incompatible with image

I have an image field in tblEmployee for an employee image.

When I try to save employee details without image file in uploader, it threw and went wrong

Operand type collision: nvarchar incompatible with image

when i select any image from the dialog then it works fine. I am using C # windows applications.

My code looks like this:

byte[] bimage=null;
           if(txtPic.Text!="")
            {
               string  image=txtPic.Text;
                Bitmap bmp=new Bitmap(image);
                FileStream fs=new FileStream(image,FileMode.Open,FileAccess.Read);
                bimage=new byte[fs.Length];
                fs.Read(bimage,0,Convert.ToInt32(fs.Length));
                fs.Close();
            }
                cmd = new SqlCommand("sp_insert_new_employee",conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name",txtName.Text.Trim());
                cmd.Parameters.AddWithValue("@fname",txtFName.Text.Trim());
                cmd.Parameters.AddWithValue("@dob",Convert.ToDateTime(dtDOB.Text));
                cmd.Parameters.AddWithValue("@nic",txtCNIC.Text.Trim());
                cmd.Parameters.AddWithValue("@add",txtAdd.Text.Trim());
                cmd.Parameters.AddWithValue("@emptype",cmbEmpType.SelectedItem.ToString());
                if(bimage!=null)
                  cmd.Parameters.AddWithValue("@imgdata", SqlDbType.Image).Value = bimage;
                else
                    cmd.Parameters.AddWithValue("@imgdata", SqlDbType.Image).Value = DBNull.Value;
                cmd.Parameters.AddWithValue("@descr",txtDescr.Text.Trim());
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Successfully added....!");`

      

Does anyone have an idea where I am making the mistake?

+3


source to share


2 answers


We usually get this error by passing DBNull.Value as a value. Can you try the following.

instead

cmd.Parameters.AddWithValue("@imgdata", SqlDbType.Image).Value = DBNull.Value;

      



use the following.

SqlParameter imageParameter = new SqlParameter("@imgdata", SqlDbType.Image);
imageParameter .Value = DBNull.Value;
cmd.Parameters.Add(imageParameter );

      

+3


source


This error, data type error in database (sql). Thus, u changes the data type of the image to the data type nvarchar.

ALTER TABLE Tbl_ProductFlip ALTER COLUMN FilePath nvarchar(100)

      



Filepath - Column Name nvarchar (100) - New Data Type

0


source







All Articles