Wednesday, September 28

ImageBinding In GridView Where ImageUrl taking From SqlServer DataBase

I have taken One 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" "">

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Image ID="Image1" runat="server" Width="273px" />
        <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" />
    <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">
        <asp:ImageButton ID="ImageButton1" ImageUrl='<%#Eval("image1") %>' runat="server" />

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)
        SqlCommand cmd3 = new SqlCommand("insert into MyImage(image1) values('" + Image1.ImageUrl + "')", conn1);
    protected void Button1_Click1(object sender, EventArgs e)
        SqlDataAdapter da = new SqlDataAdapter("select image1 from MyImage", conn1);
        DataSet ds = new DataSet();
        GridView1.DataSource = ds;

I used this connectionstrins in Web.config file:

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

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 

   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.


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

shibashish mohanty