简体   繁体   中英

Getting error:Operand type clash: nvarchar is incompatible with image

I have image field in tblEmployee for employee's picture.

When i am trying to save employee's detail without image file in uploader it threw and error

"Operand type clash: nvarchar is incompatible with image"

while when i select any picture from dialog box then it works fine. I am using c# windows applications.

My Code looks like as:

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....!");`

Any one have any idea where am i making mistake?

Usually we get this error while passing DBNull.Value as the value. Can you try the following.

instead of

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 );

This error, datatype error in database(sql). So u change image datatype to nvarchar datatype.

ALTER TABLE Tbl_ProductFlip ALTER COLUMN FilePath nvarchar(100)

Filepath is ColumnName nvarchar(100) is new datatype

I solved this problem by using parameter like this :

    Byte[] imgtype = { 0 };
 if(bimage!=null)
                  cmd.Parameters.AddWithValue("@imgdata", SqlDbType.Image).Value = bimage;
                else
                    cmd.Parameters.AddWithValue("@imgdata", SqlDbType.Image).Value = imgtype ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM