I want to get value by using parameterized method, but l am getting this error
Must declare the scalar variable @Category
I don't know where and how to declare the scalar variable in the code below.
See the code that I have tried
[Route("TotalValue")]
[HttpGet]
public decimal TotalValue()
{
var query = "Select * from OrderDetail where Category = @Category";
string sqlDataSource = _configuration.GetConnectionString("DefaultConnection");
decimal total = 0;
using (SqlConnection con = new SqlConnection(sqlDataSource))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
total = dr.GetDecimal(0);
}
}
}
con.Close();
}
return total;
}
1条答案
按热度按时间szqfcxe21#
You're almost there.
After creating the command, add the @Category parameter to the command's parameters collection: