You must Sign In to post a response.
  • Category: ASP.NET

    Data type mismatch in criteria expression.

    Hi, Developers

    first of all am very sorry for my repeated post.

    I need to insert a Attachments with that Attachment File Size.When i am try to insert it will display the following error message.

    Data type mismatch in criteria expression.

    My Insert code is below:

    string filePath = FileUpload1.PostedFile.FileName;
    string filename1 = Path.GetFileName(filePath);
    string ext = Path.GetExtension(filename1);
    string type = String.Empty;
    if (type != String.Empty)
    {
    OleDbConnection con=Connection.DBconnection();
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    string query = "insert into wordFiles (Name,type,data)" + " values (@Name, @type, @Data)";
    OleDbCommand com=new OleDbCommand(query, con);//insert query
    com.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1;
    com.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
    com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
    com.ExecuteNonQuery();
    Label2.ForeColor = System.Drawing.Color.Green;
    Label2.Text = " Word File Uploaded Successfully";
    }
    but if i am added the line
    com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;

    Replace with .ToString()

    com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes.ToString();
    it is inserted but the File Size value is Like (System.Byte[]) only

    then i am try Like
    com.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes.ToString();
    But the output is same

    So anyone know please help me to how i am inserted exactly with the File Size.

    Thanks with
    Paul.S
  • #764426
    Hai Paul,
    The File Size value will be shown like System.Byte[] only as this is the image type data so it will not have the value or the value will not be shown.
    You need to check while retrieving, is it returning the correct image and if not then it might be saving the empty null value which will also be same like System.Byte[].
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764431
    There error due to mismatch in database datatype, go through the below code snippet and see if it works for you
    public static void databaseFilePut(string varFilePath) {
    byte[] file;
    using (var stream = new FileStream(varFilePath, FileMode.Open, FileAccess.Read)) {
    using (var reader = new BinaryReader(stream)) {
    file = reader.ReadBytes((int) stream.Length);
    }
    }
    using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
    using (var sqlWrite = new SqlCommand("INSERT INTO Raporty (RaportPlik) Values(@File)", varConnection)) {
    sqlWrite.Parameters.Add("@File", SqlDbType.VarBinary, file.Length).Value = file;
    sqlWrite.ExecuteNonQuery();
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764435
    Hi Paul,
    If you want to store full file in the database column named 'data', you can follow the solution provided by Mr. Prasad Kulkarni sir, it is exact one.
    But if you want to store only file size in column 'data', use this:
    com.Parameters.Add("@Data", SqlDbType.VarBinary).Value =bytes.Length;//... using this file size is stored in terms of bytes, so if you want then you can convert it to KB or MB..

    Hope it helps.
    Regards,
    Shashikant Gurav[Programmer II]
    shashikantgurav22@gmail.com

  • #764484
    If you send like this it will store the time name.

    com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes.ToString();

    I think, In your data base the column "data" has the type varchar. Can you change that in to Binarry the then send the bites.

    By Nathan
    Direction is important than speed


Sign In to post your comments