CSharp Executing Select Commands – ADO.Net Database

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Photo Gallery</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataList
        id="dlstImages"
        DataSourceID="srcImages"
        RepeatColumns="3"
        Runat="server">
        <itemTemplate>
        <asp:Image ID="Image1"
            ImageUrl='<%# String.Format("DynamicImage.ashx?id={0}", Eval("Id")) %>'
            Width="250"
            Runat="server" />
            <%# Eval("Description") %>
        </itemTemplate>
    </asp:DataList>
    <asp:FormView
        id="frmImage"
        DataSourceID="srcImages"
        DefaultMode="Insert"
        Runat="server">
        <insertItemTemplate>
        <asp:Label
            id="lblImage"
            Text="Upload Image:"
            AssociatedControlId="upImage"
            Runat="server" />
        <asp:FileUpload
            id="upImage"
            FileBytes='<%# Bind("Image") %>'
            Runat="server" />
        <asp:Label
            id="lblDescription"
            Text="Description:"
            AssociatedControlID="txtDescription"
            Runat="server" />
        <asp:TextBox
            id="txtDescription"
            Text='<%# Bind("Description") %>'
            TextMode="MultiLine"
            Columns="50"
            Rows="2"
            Runat="server" />
        <asp:Button
            id="btnInsert"
            Text="Add Image"
            CommandName="Insert"
            Runat="server" />
        </insertItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource
        id="srcImages"
        SelectCommand="SELECT ID,Description FROM Images"
        InsertCommand="INSERT Images (Image,Description) VALUES (@Image,@Description)"
        ConnectionString="<%$ ConnectionStrings:Images %>"
        Runat="server" />
    </div>
    </form>
</body>
</html>File: Web.config<configuration>
  <connectionStrings>
    <add name="Images"
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>File: DynamicImage.ashx<%@ WebHandler Language="C#" Class="DynamicImage" %>
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;public class DynamicImage : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {
        string imageId = context.Request.QueryString["Id"];        SqlDataSource src = new SqlDataSource();
        src.ConnectionString = WebConfigurationManager.ConnectionStrings ["Images"].ConnectionString;
        src.SelectCommand = "SELECT Image FROM Images WHERE Id=" + imageId;        DataView view = (DataView)src.Select(DataSourceSelectArguments.Empty);
        context.Response.BinaryWrite( (byte[])view[0]["Image"]);
    }
    public bool IsReusable
    {
        get {
            return false;
        }
    }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.