Click here to Skip to main content
15,888,241 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hey guys I am relay rather stuck, I have a data-grid showing some data then I have a on select button active you click on the button and the titles selection is printed bellow.

problem is I now want the on select selection chose to run a update query after to reduce the stock buy one. I am having some problems with this

My Code

C#
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

C#
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 + ".";
}
Posted

1 solution

Write a proper data layer. Don't dispose in a using block. Write a method that does one dB update and call it. Your two SQL statements don't make sense. If stock is a column you at to a text value, it's not a number you can subtract, nor would your SQL be worth anything unless your table has only one row.
 
Share this answer
 

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