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;
    }
share|improve this question

60% accept rate
Why are you commenting out the parameterized lines? Why is every argument in your method an object? Which one's a byte[]? – BoltClock May 31 '11 at 21:15
Use a parametrized placeholder (it looks like this was originally attempted). It will clean up the code and get rid of the error. – pst May 31 '11 at 21:16
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 @Image – T4mer May 31 '11 at 21:32
parametrized query resulting the same error ! – T4mer May 31 '11 at 21:34
feedback

2 Answers

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;
}
share|improve this answer
Data type mismatch in criteria expression. ????? – T4mer May 31 '11 at 21:35
feedback
up vote 0 down vote accepted

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 .

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.