如何在3层架构的gridview中显示图像?

时间:2014-10-16 13:05:38

标签: c# asp.net gridview

对于数据访问层:

来源:

public string insert_details(bisuness_object user_details)
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("insert into tbl_rgsthome values(@firstname,@lastname,@emailid,@password,@address,@upload)", con);
    try
    {
        cmd.Parameters.AddWithValue("@firstname", user_details.firstname_value);
        cmd.Parameters.AddWithValue("@lastname", user_details.lastname_value);
        cmd.Parameters.AddWithValue("@emailid", user_details.emailid_value);
        cmd.Parameters.AddWithValue("@password", user_details.pass_value);
        cmd.Parameters.AddWithValue("@address", user_details.addr_value);
        cmd.Parameters.AddWithValue("@upload", user_details.fileupl_value);
      //cmd.Parameters.AddWithValue("@imagepath", user_details.imgpth_value);
        return cmd.ExecuteNonQuery().ToString();
    }
    catch (Exception show_error)
    {
        throw show_error;
    }
    finally
    {
        cmd.Dispose();
        con.Close();
        con.Dispose();
    }

}

home.aspx:

public string getimage(object ob)
{
    string img = @"/image/" + ob.ToString();
    return img;
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.PostedFile != null  && FileUpload1.PostedFile.FileName != "")
    {

        byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile Image = FileUpload1.PostedFile;
        Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
        bisuness_object bo = new bisuness_object();
        // cmd.Parameters.AddWithValue("@imagepath", ("@uploadimage") + filename);
        bo.firstname_value = TextBox1.Text;
        bo.lastname_value = TextBox2.Text;
        bo.emailid_value = TextBox3.Text;
        bo.pass_value = TextBox4.Text;
        bo.addr_value = TextBox6.Text;
        bo.fileupl_value = FileUpload1.FileName.ToString();
        bisuness_layer bl = new bisuness_layer();
        bind();

        try
        {
            string result = bl.record_insert(bo);
        }
        catch (Exception info)
        {
            throw info;
        }
        finally
        {
            bo = null;
            bl = null;
            bind();
        }
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
    }
}

如何解决它.....请帮帮我 我想在gridview中进行成像

1 个答案:

答案 0 :(得分:1)

动态绑定gridview内的image标签 通过动态提供路径

 <asp:Image ID="image" Style="width:100px; height:100px;" runat="server" ImageUrl='<%# "~/folder/subfolder/" + Eval("id") +"."+ Eval("imagetype") %>' />

和绑定gridview的方法

 protected void GetDayoffer()
{

    gridview1.DataSource = bl.method();
    gridview1.DataBind();
}

希望这会帮助您或将字节再次恢复到图像并将其绑定到gridview