Wednesday, September 28

ImageBinding In GridView Where ImageUrl taking From SqlServer DataBase

I have taken One Asp.net image control, one file upload control And two label,One gridView And Three buttons.


Here is the Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imageUpload.aspx.cs" Inherits="imageUpload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Image ID="Image1" runat="server" Width="273px" />
    <p>
        <asp:Button ID="upload" runat="server" onclick="Button1_Click" Text="upload" />
        <asp:Button ID="Add" runat="server" onclick="Add_Click" Text="Add" />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
        Text="GridImageBinding" />
    </p>
    <asp:Label ID="lblError" runat="server" Text="Label" Visible="False"></asp:Label>
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    <asp:GridView ID="GridView1" AutoGenerateColumns="true" runat="server">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
        <asp:ImageButton ID="ImageButton1" ImageUrl='<%#Eval("image1") %>' runat="server" />
    </ItemTemplate>
   
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </form>
</body>
</html>

Here is Your Page Behind Code:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class imageUpload : System.Web.UI.Page
{
    SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString);


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fileName = FileUpload1.FileName.ToString();
            string uploadFolderPath = "~/Image/";
            string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
            FileUpload1.SaveAs(filePath + "\\" + fileName);
            Image1.ImageUrl = "~/Image/" + "/" + FileUpload1.FileName.ToString();
        }
    }
    protected void Add_Click(object sender, EventArgs e)
    {
        conn1.Open();
        SqlCommand cmd3 = new SqlCommand("insert into MyImage(image1) values('" + Image1.ImageUrl + "')", conn1);
        cmd3.ExecuteNonQuery();
        conn1.Close();
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        conn1.Open();
       
        SqlDataAdapter da = new SqlDataAdapter("select image1 from MyImage", conn1);
        DataSet ds = new DataSet();
       da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}


I used this connectionstrins in Web.config file:

<connectionStrings>
        <add name="cs" connectionString="Data Source=DEV4;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=netserver" providerName="System.Data.SqlClient"/>
    </connectionStrings>



Following is my Database Details:




I have taken DataBase As Test,
One Table Name as MyImage
Having Columns 
1.equipment_id As int, NotNull (i,e Auto incremented columns true)
2.image1 As Varchar(50),Null 

Thanks
   Shibashish mohanty Software developer 




1 comment:

  1. Vikash i have written this article only for you. i think this was your doubt.if i left something plz tell me about that. All the best.

    ReplyDelete

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty