I have experienced an error on
"The parametrized query '(@blue nvarchar(4000))SELECT blueBallImage FROM CorrespondingBal' expects the parameter '@blue',
which was not supplied."
I am doing a HttpHandler.
I want to retrieve the image from the database. My codes are as below.
public void ProcessRequest (HttpContext context) {
string image = context.Request.QueryString["image"];
SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyCloudGames;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT blueBallImage FROM CorrespondingBall WHERE objective = blue", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("blue", image);
con.Open();
byte[] ballImage = (byte[])cmd.ExecuteScalar();
con.Close();
context.Response.ContentType = "image/png";
context.Response.OutputStream.Write(ballImage, 78, ballImage.Length - 78);
}
The error occurred at
byte[] ballImage = (byte[])cmd.ExecuteScalar();
Use AddWithValue
instead of Add
method.
SqlCommand cmd = new SqlCommand("SELECT blueBallImage FROM CorrespondingBall WHERE objective = @blue", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@blue", image);
You should provide your parameter with @
in your command.
Soner Gönül, image is null. How can I correct it?
So, you should return DBNull.Value
if your image
is null
. You can't pass a null
on a required parameter. You can use a method as an alternative like;
SqlCommand cmd = new SqlCommand("SELECT blueBallImage FROM CorrespondingBall WHERE objective = @blue", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@blue", CheckForDbNull(image));
...
public static object CheckForDbNull(object value)
{
if(value == null)
{
return DBNull.Value;
}
return value;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.