문제

I am trying to select film by using on select command, print the title in a label once selected.. that works well.

next part is the selected film will then decrement 1 from the database stock on button click. This is where I think I am getting confused, its showing no errors until the button click takes place.

C# code for update query

protected void Button2_Click(object sender, EventArgs e)
{

    var myquery = string.Format("UPDATE DVD SET Stock = Stock - 1");
    da.InsertCommand = new OleDbCommand("INSERT INTO DVD (Stock) VALUES (@MessageLabel)", conn);
    {
        da.InsertCommand.Parameters.AddWithValue("@Stock", MessageLabel.Text);

        conn.Open();
            da.InsertCommand.ExecuteNonQuery();
            using (OleDbCommand cmd = new OleDbCommand(myquery, conn))
            cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
    }
}

Previous code for select event

public void Latest_DVD()
{
    {
        using (OleDbDataAdapter dataquer = new OleDbDataAdapter("SELECT Title,Category,Director,Stock,Year FROM DVD ", conn))
        {
            dataquer.Fill(dt);
        }
    }
    DG_Latest.ShowHeader = true;
    DG_Latest.DataSource = dt;
    DG_Latest.DataBind();
    conn.Close();
    conn.Dispose();
}

protected void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow row = DG_Latest.SelectedRow;
    MessageLabel.Text = "You selected to rent " + row.Cells[1].Text + ".";
}

so I am thinking I have the query wrong and possibly nor retrieve the update from the label but maybe the on select its self... I am not sure through.

the error it is showing is

Data type mismatch in criteria expression.

just after the connection is open

도움이 되었습니까?

해결책

As @afzalulh said, remove the insert part. And change myquery string to be:

var myquery = string.Format("UPDATE DVD SET Stock = Stock - 1 WHERE Title = @Title");
var row = DB_Latest.SelectedRow;
var title = row.Cells[0].Text;
var cmd = new OleDbCommand(myquery, conn);
cmd.Parameters.AddWithValue("@Title", title);

With that, you only update Stock for the selected DVD title only. Without adding WHERE clause, the query will decrease stock for all DVDs.

다른 팁

You want to update stock, there's no need to insert. I believe this is what you want:

protected void Button2_Click(object sender, EventArgs e)
{

    var myquery = string.Format("UPDATE DVD SET Stock = Stock - 1");
    conn.Open();
    using (OleDbCommand cmd = new OleDbCommand(myquery, conn))
        cmd.ExecuteNonQuery();
    conn.Close();
    conn.Dispose();
}

EDIT: myquery should include WHERE as suggested by har07. Otherwise it will reduce all DVD's stock by 1.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top