Jack Huynh
Jack Huynh

Reputation: 33

Must Declare Scalar Value?

I am working with SQL and c# for my assignment. For this assignment I am running a movie database and I am stuck on this part where I have to check out a movie. I keep getting a 'must declare scalar value' but I am unsure how to do that. My code:

 <asp:Panel ID="panel5" runat="server" Visible="false">
        <asp:GridView id="one_data" AutoGenerateColumns="false" runat="server" DataKeyNames="MovieID">
            <Columns>

                <asp:BoundField DataField="MovieTitle"
                    HeaderText="Movie"/>
                <asp:BoundField DataField="DateChecked"
                    HeaderText="Date Checked"/>
                <asp:BoundField DataField="CheckedOut"
                    HeaderText="Checked Out"/>
            </Columns>
        </asp:GridView>
        <asp:Button runat="server" Text="Choose another Movie" OnClick="GoBack" />
        <asp:Button runat="server" Text="Check Out" OnClick="CheckOut" />
    </asp:Panel>

Panel 5 gets a selected movie that the user chooses and when they hit the check out button the fields 'DateChecked' and 'CheckedOut' are supposed to be filled with the date and a 'Y' for yes. CS Code:

public void CheckOut(object sender, EventArgs e)
    {
        get_connection();
        try
        {

            connection.Open();
            command = new SqlCommand("UPDATE Content SET DateChecked=@DateChecked, CheckedOut=@CheckedOut WHERE MovieID=@MovieID", connection);
            command.Parameters.AddWithValue("@DateChecked", DateTime.Now);
            command.Parameters.AddWithValue("@CheckedOut", 'Y');

            reader = command.ExecuteReader();
            one_data.DataSource = reader;
            one_data.DataBind();



            reader.Close();
        }

I left out the catch part because it's unimportant. When I hit the checkout movie button I keep getting the error and if I remove the WHERE clause, it works except that all movies are updated instead of the one I chose. What would be the fix for this?

Upvotes: 1

Views: 424

Answers (1)

NibblyPig
NibblyPig

Reputation: 52952

Your UPDATE command has three values, which are defined using @

You have specified two of them, but you didn't specify the @MovieID

command = new SqlCommand("UPDATE Content SET DateChecked=@DateChecked, CheckedOut=@CheckedOut WHERE MovieID=@MovieID", connection);
            command.Parameters.AddWithValue("@DateChecked", DateTime.Now);
            command.Parameters.AddWithValue("@CheckedOut", 'Y');

As a result it won't be able to form some valid SQL. You need to add the movie ID

command.Parameters.AddWithValue("@MovieID", movieId);

Presumably you'll get this from your boundfield for movie title.

Upvotes: 2

Related Questions