Christopher Lim
Christopher Lim

Reputation: 35

"No value given for one or more required parameters."

when I click the btnCheckout, it will give me "No value given for one or more required parameters."

Does anyone know why?

protected void btnCheckOut_Click(object sender, ImageClickEventArgs e)
        {
            string strPay = DropDownList1.Items[DropDownList1.SelectedIndex].Text;
            string strDel = DropDownList2.Items[DropDownList2.SelectedIndex].Text;
            OleDbConnection mDB = new OleDbConnection();
            mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source="
                + Server.MapPath("~/App_Data/webBase.accdb");
            string strStatus = "CheckedOut";
            int intOrderNo = (int)Session["sOrderNo"];
            String strSqlUpdate = "UPDATE orderInfo SET orderPayMode = @Pay, "
                + "orderDelMode = @Del, orderStatus = @Status WHERE orderNo = " + intOrderNo;
            OleDbCommand cmd;
            cmd = new OleDbCommand(strSqlUpdate, mDB);
            cmd.Parameters.AddWithValue("@Pay", strPay);
            cmd.Parameters.AddWithValue("@Del", strDel);
            cmd.Parameters.AddWithValue("@Status", strStatus);
            mDB.Open();
            cmd.ExecuteNonQuery();
            mDB.Close();
            Response.Redirect("OrderDetails.aspx");
        }

Upvotes: 3

Views: 954

Answers (2)

Mike Perrenoud
Mike Perrenoud

Reputation: 67918

To add to what nhrobin is saying, consider changing your code to this:

cmd.Parameters.AddWithValue("@Pay", strPay ?? "");
cmd.Parameters.AddWithValue("@Del", strDel ?? "");
cmd.Parameters.AddWithValue("@Status", strStatus ?? "");

and that should help it succeed. However, a more striking question is this:

What should we be doing when these values are null?

That's a question you're going to have to answer. If what I've provided is satisfactory then you're set.

Upvotes: 1

nhrobin
nhrobin

Reputation: 903

One or more of the paremeters are null. Check if strPay, strDel or strStatus is null or not. If any of them is null you can send empty string or SqlString.Null.

If some parameter value is null that parameter doesn't get added that's why this error.

cmd.Parameters.AddWithValue("@Pay", strPay==null?SqlString.Null:strPay);

Upvotes: 3

Related Questions