0
votes

Hi i was using stored procedure in SQL Server to pass parameters to the query , but now I'm changing my database to ms access and it's my first time to deal with.

how can i pass byte[] to sql query ? bacause i got this error Syntax error (missing operator) in query expression 'System.Byte[]'.

this is my code

    public static int EditWhois(object ID,object Image, object Ranswer, object Fanswer1, object Fanswer2, object Fanswer3)
    {
        int result = 0;
        String sql = "UPDATE Whois SET [Image]="+@Image+", Ranswer=" + Ranswer + ", Fanswer1=" + Fanswer1 + ",Fanswer2=" + Fanswer2 + ",Fanswer3=" + Fanswer3 + " WHERE ID=" + ID;
        System.Windows.Forms.MessageBox.Show(sql);
        cmd = new OleDbCommand(sql, con);

        //cmd.Parameters.AddWithValue("@ID", ID); 
        //cmd.Parameters.AddWithValue("@Image", Image);
        //cmd.Parameters.AddWithValue("@Ranswer", Ranswer);
        //cmd.Parameters.AddWithValue("@Fanswer1", Fanswer1);
        //cmd.Parameters.AddWithValue("@Fanswer2", Fanswer2);
        //cmd.Parameters.AddWithValue("@Fanswer3", Fanswer3);

        if (con.State != ConnectionState.Open)
        {
            con.Open();
            result = cmd.ExecuteNonQuery();
            con.Close();
        }
        return result;
    }
2
Why are you commenting out the parameterized lines? Why is every argument in your method an object? Which one's a byte[]?BoltClock♦
Use a parametrized placeholder (it looks like this was originally attempted). It will clean up the code and get rid of the error.user166390
i'm commenting the parametrized lines because i'm directly passing arguments to the query ,my arguments are objects to avoid casting , the byte[] is the @ImageT4mer
parametrized query resulting the same error !T4mer

2 Answers

0
votes

Use @ parameter substitution. Also as @BoltClock says, change you method signature.

public static int EditWhois(object ID,object Image, object Ranswer, 
    object Fanswer1, object Fanswer2, object Fanswer3)
{
    int result = 0;
    String sql = "UPDATE Whois SET [Image]=@Image, Ranswer=@Ranswer, " + 
      "Fanswer1=@Fanswer1, Fanswer2=@Fanswer2, Fanswer3=@Fanswer3 " +
      "WHERE ID=@ID";
    cmd = new OleDbCommand(sql, con);

    cmd.Parameters.AddWithValue("@ID", ID); 
    cmd.Parameters.AddWithValue("@Image", Image);
    cmd.Parameters.AddWithValue("@Ranswer", Ranswer);
    cmd.Parameters.AddWithValue("@Fanswer1", Fanswer1);
    cmd.Parameters.AddWithValue("@Fanswer2", Fanswer2);
    cmd.Parameters.AddWithValue("@Fanswer3", Fanswer3);

    if (con.State != ConnectionState.Open)
    {
        con.Open();
        result = cmd.ExecuteNonQuery();
        con.Close();
    }
    return result;
}
0
votes

result is still 0; i know the problem where it was . if the connection is closed the query will be executed successfully , but if it is opend it will not be executed due to this condition .

if (con.State != ConnectionState.Open)
{
    con.Open();
    result = cmd.ExecuteNonQuery();
    con.Close();
}

it must be

if (con.State != ConnectionState.Open)
{
    con.Open();
}
    result = cmd.ExecuteNonQuery();
    con.Close();

thanks all .