Problem with where clause in update statement C#

UrKo

Member
Joined
May 12, 2009
Messages
5
HI,

I have written a statement to update hits per record in DB (im using access db).
However, if i add where clause i get an error "Parameter ?_1 has no default value".

This is my code:
Code:
protected void DataList1_Load(object sender, EventArgs e)
    {
        Label Label1 = DetailsView1.FindControl("Label1") as Label;
        Label hits = DataList1.FindControl("hits") as Label;
        TextBox TextBoxVideoId = DataList1.FindControl("TextBoxVideoId") as TextBox;

        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\CenterDB.mdb";
        OleDbConnection conn = new OleDbConnection(connectionString);
        conn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        cmd.CommandText = "UPDATE VIDEO_MOJI SET hits = hits + 1 WHERE VideoId = ?";
        cmd.Parameters.Add("@hits", OleDbType.Integer).Value = hits;
        cmd.ExecuteNonQuery();
        conn.Close();
    }

thanks,
 
IIRC the OleDb provider doesnt support named parameters, you just add the parameters in the correct order and do not bother to specify a name.
 
hi, thanks for the reply. Im new to .net so can you explain a little more of what name to specify?

If i just write upade without where clause, then it updates all records in the db.
 
ok i made it.
i added this and its working :)
Code:
string getVideoId = (string)Label1.Text;
        getVideoId = getVideoId.Trim();

        cmd.CommandText = "UPDATE VIDEO_MOJI SET hits= hits+ 1 WHERE VideoId = " + getVideoId  + "";
 
Have you tried creating the parameter and then setting its value before adding it to the parameters collection?

Also the parameters collection has a .AddWithValue method that might work.

You really want to avoid concatenating strings though as this can cause all sorts of odd security risks and is generally a bad practice.
 
Back
Top