Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the operator for byte[] in query?
i know single quotes' ' for string, hashtag # # for date so what for byte[] array
I have c# form with access database and i try update image passing it as byte []

What I have tried:

i could insert it using tableAdapter newRow like

NewRow.Add["column name"]=image;

but i need to make update of it in certain row matches certain WHERE condition????
Posted
Updated 12-Mar-22 18:48pm
v2

1 solution

Since we have no idea what your query looks like, it's pretty much impossible to give you a definitive answer to anything.

But, I will say this. You should not be using a byte array in a WHERE clause.
 
Share this answer
 
Comments
Ism Oss 13-Mar-22 15:19pm    
byte[] image=imagetobyte(picturebox1.Image);

String sqltext="update [table] set [column]="+image+" where id='"+Textbox1.Text+"'";

Note:imagetobyte() is own method
Dave Kreskowiak 13-Mar-22 15:49pm    
Do not do it like that. Use parameterized queries only.

I'm typing this from memory, so it may not compile without fixes.
string sqltext = "UPDATE [table] set [column]=? WHERE id=?";

// These must be added in the order they appear in the query string OleDbParameter imageParameter = new OleDbParameter("@image", OleDbType.VarBinary, image.Length);
parameter.Value = command.Parameters.Add(imageParameter);

// This is a bad idea. You're not vetting the value of the TextBox before using it.
command.Parameters.AddWithValue("@id", TextBox1.Text);
Ism Oss 13-Mar-22 20:13pm    
still have -> Additional information: Syntax error (missing operator) in query expression 'System.Byte[]'.

my code->
using (OleDbCommand cmdimage = new OleDbCommand("update PatientVisit set Barcode = "+ Exportimage+" where DNo =" + Convert.ToInt32(DNo.Text) + " and VDate like '" + VDate.Text + "'",helper.con))
{
cmdimage.Parameters.Add("Barcode",OleDbType.VarBinary);
cmdimage.Parameters["Barcode"].Value = Exportimage;

cmdimage.ExecuteNonQuery();

}

also tried AddWithValue
Dave Kreskowiak 13-Mar-22 21:21pm    
Same thing. PARAMETERIZED QUERIES.

What you're doing to build a SQL query is called "string concatenation" and it's the number one cause of SQL errors and the destruction of databases. Google for "SQL Injection Attack" to see why.

You may have added parameter objects, but you failed to use parameters in the actual query statement.

string query = "UPDATE PatientVisit SET Barcode=? WHERE DNo=? AND VDate=?";
// NEVER store dates as Text!!
using (OleDbCommand cmd = new OldDbCommand(query, helper.con))
{
    // You cannot use AddWithValue with a binary type.
    cmd.Parameters.Add("Barcode",OleDbType.VarBinary);
    cmd.Parameters["Barcode"].Value = Exportimage;

    // Again, you MUST verify user input before using it!
    // Treat it like the spawn of Satan that it is.
    int dno;
    bool success = int.TryParse(DNo.Text, out dno);
    if (success)
    {
        cmd.Parameters.AddWithValue("@dno", dno);
    }
    
    DateTime vdate;
    success &= DateTime.TryParse(VDate.Text, out vdate);
    if (success)
    {
        cmd.Parameters.AddWithValue("@VDate", vdate);
    }

    if (success)
    {
        cmd.ExecuteNonQuery();
    }
    else
    {
        // Parsing failed somewhere so you're not executing the query.
    }
}
Ism Oss 14-Mar-22 20:30pm    
thanks this worked with me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900