I am currently storing the values retrieved from the user form including image supplied by the user to sql database. The image is stored in the database but at the time when am trying to retrieve that image am getting the following exception
Parameterized Query '(@name varchar(8000))select firstname,image from register where ' expects parameter @name, which was not supplied.

my code is
first i have created a register web form where user can store his data

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.FileContent != null)
            {
                string fname = txtfname.Text.ToString();
                string lname = txtlname.Text.ToString();
                string logname = txtdesiredlname.Text.ToString();
                string pass = txtpass.Text.ToString();
                string secq = Ddlsecq.Text.ToString();
                string seca = txtsecans.Text.ToString();
                string email = txtemail.Text.ToString();
                string location = Ddllocation.Text.ToString();
                byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile uploadimage = FileUpload1.PostedFile;
                uploadimage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
                string dbstring = ConfigurationManager.ConnectionStrings["cnstring"].ConnectionString;
                SqlConnection cn = new SqlConnection();
                cn.ConnectionString = dbstring;
                cn.Open();
                // SqlDataAdapter da = new SqlDataAdapter("select * from register", cn);
                // SqlCommandBuilder cmd = new SqlCommandBuilder(da);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "insert into register(firstname,lastname,loginname,password,image,securityq,securitya,email,location)" + "values (@fname,@lname,@logname,@pass,@image,@secq,@seca,@email,@loc)";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = cn;
                SqlParameter firstname = new SqlParameter("@fname", SqlDbType.VarChar, 20);
                firstname.Value = fname.ToString();
                cmd.Parameters.Add(firstname);
                SqlParameter lastname = new SqlParameter("@lname", SqlDbType.VarChar, 20);
                lastname.Value = lname.ToString();
                cmd.Parameters.Add(lastname);
                SqlParameter loginname = new SqlParameter("@logname", SqlDbType.VarChar, 20);
                loginname.Value = logname.ToString();
                cmd.Parameters.Add(loginname);
                SqlParameter password = new SqlParameter("@pass", SqlDbType.VarChar, 20);
                password.Value = pass.ToString();
                cmd.Parameters.Add(password);
                SqlParameter uploadedimage = new SqlParameter("@image", SqlDbType.Image, imagesize.Length);
                uploadedimage.Value = imagesize;
                cmd.Parameters.Add(uploadedimage);
                SqlParameter securityq = new SqlParameter("@secq", SqlDbType.VarChar, 50);
                securityq.Value = secq.ToString();
                cmd.Parameters.Add(securityq);
                SqlParameter securitya = new SqlParameter("@seca", SqlDbType.VarChar, 50);
                securitya.Value = seca.ToString();
                cmd.Parameters.Add(securitya);
                SqlParameter emailid = new SqlParameter("@email", SqlDbType.VarChar, 20);
                emailid.Value = email.ToString();
                cmd.Parameters.Add(emailid);
                SqlParameter loc = new SqlParameter("@loc", SqlDbType.VarChar, 20);
                loc.Value = location.ToString();
                cmd.Parameters.Add(loc);
                cmd.ExecuteNonQuery();
                cn.Close();
                Response.Redirect("~/profile.aspx?logname='" +txtdesiredlname.Text+ "'");
            }
        }
        catch (Exception)
        {
            lblimnotfound.Enabled = true;
        }
    }

then i have created a profileimage.ashx i.e. a handler
the code is:

<%@ WebHandler Language="C#" Class="profileimage" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class profileimage : IHttpHandler 
{

    public void ProcessRequest(HttpContext context)
    {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["cnstring"].ConnectionString;
            cn.Open();
            SqlCommand cmd = new SqlCommand("select firstname,image from register where loginname=@name");
            //cmd.CommandText = "select firstname,image from register where loginname=@name";
           // cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = cn;
            SqlParameter logname = new SqlParameter("@name", System.Data.SqlDbType.VarChar);
            logname.Value = context.Request.QueryString["logname"];
            cmd.Parameters.Add(logname);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                context.Response.BinaryWrite((byte[])dr["image"]);
                cn.Close();
            }
          }
 
    public bool IsReusable
    {
        get 
        {
            return false;
        }
    }

}

coming back to the web page where i need the data displayed in grid view :

<div>
    <asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" DataKeyNames="loginname"
              DataSourceID="SqlDataSource1">
     <Columns>
    <asp:TemplateField HeaderText="Image">
    <ItemTemplate>
    <asp:Image ID="Image1" runat="server" 
           ImageUrl='<%# "profileimage.ashx?loginname=" + Eval("loginname")%>'/>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:cnstring %>"
    SelectCommand="SELECT * FROM register"></asp:SqlDataSource>
    </div>

am getting exception in the handler page. Any help regarding this is appreciated and i hope you get the whole issue...

Recommended Answers

All 4 Replies

I think you should use logname,

ImageUrl='<%# "profileimage.ashx?loginname=" + Eval("loginname")%>'/>

thanx a lot dude. m able to retrieve the image. Just a one thing if i need to retrieve the image of a particular user not all the users in the database what am suppose to do then. if possible please let me know the code...

>if i need to retrieve the image of a particular user not all the users in the database what am suppose to do then.

select firstname,image from register where loginname=@name

not it doesnt work. will you elaborate?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.