Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the error "sqlexception occured as Parameterized Query '(@I_Name nvarchar(200))Select I_Image from Items where I_Name=@I' expects parameter @I_Name, which was not supplied."

My code:

<%@ WebHandler Language="C#" Class="Handler" %>
C#
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;

public class Handler : IHttpHandler 
{    
    public void ProcessRequest (HttpContext context) 
    {
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\.net ex\\ID68\\Bidding\\App_Data\\bidding.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        string sql = "Select I_Image from Items where I_Name=@I_Name";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("@I_Name", SqlDbType.NVarChar,200).Value = context.Request.QueryString["I_Name"];
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        context.Response.ContentType = "image/jpg";
        context.Response.BinaryWrite((byte[])dr["I_Image"]);
        dr.Close();
        con.Close();
   
    }
 
    public bool IsReusable 
    {
        get 
        {
            return false;
        }
    }
}
Posted
Updated 15-Feb-12 7:36am
v5

1 solution

There is a good chance that the Query string "I_Name" is returning null - it's difficult to tell from that code.
If so, then try passing it as an empty string:
C#
string s =  context.Request.QueryString["I_Name"];
s = s == null ? "" : s;

It would be worth changing to AddWithValue anyway - Add is depreciated (and has been for a long time!)
C#
string s =  context.Request.QueryString["I_Name"];
cmd.Parameters.AddWithValue("@I_Name", s == null ? "" : s);


[edit]Where did that extra semicolon come from? - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
[no name] 15-Feb-12 12:53pm    
+5

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