Thursday, February 2, 2012

Wizard Example 2

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default13.aspx.cs" Inherits="Default13" %>
<!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 id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function showtext() {
         
            var len = document.getElementById('AddPropertyWizard_lst').value.length;
            if (len <= 0) {
                alert('enter something');
                return false;
            }
        }
      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
    <table border="1">
    <tr>
    <td>
    
    
     <asp:Wizard ID="AddPropertyWizard" runat="server" ActiveStepIndex="0" 
           
           Width="98%" margin-left="10px"
            DisplaySideBar="False">
           
            <StartNavigationTemplate>
                <asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext" OnClick="AddPropertyWizard_NextButtonClick"
                    Text="Next" />
            </StartNavigationTemplate>
            <WizardSteps>
            
                <asp:WizardStep ID="WizardStep1" runat="server">
                <table>
                <tr>
                <td>UserName:</td>
                <td>
                <asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
                </td>
                </tr>
                <tr>
                <td> Password:</td>
               <td>
               <asp:TextBox ID="txtpass" runat="server"></asp:TextBox>
                </td>
                </tr>
                </table>    
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep2" runat="server">
               <table>
                <tr>
                <td>Address:</td>
                <td>
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                </td>
                </tr>
                <tr>
                <td>Phone:</td>
                
                <td>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
                
                </td>
                </tr>
                </table> 
                </asp:WizardStep>
               <asp:WizardStep ID="WizardStep3" runat="server">
               <table>
                <tr>
                <td> Office:</td>
               
                <td>
               <asp:TextBox ID="txtOffice" runat="server"></asp:TextBox>
                </td>
                </tr>
                
                <tr>
                <td> Map:</td>
                
                <td>
               <asp:TextBox ID="txtMap" runat="server"></asp:TextBox>
                
                </td>
                </tr>
                <tr>
                <td>
                <asp:ListBox ID="lst" runat="server" Width="100px" onKeyDown='showDialog("Down")' onKeyPress='showDialog("Press")'>
        
        </asp:ListBox>
        <asp:Button ID="fill" runat="server" Text="Fill" OnClick="fill_Click" />
                </td>
                </tr>
                </table> 
                </asp:WizardStep>
                </WizardSteps>
                   <FinishNavigationTemplate>
                <asp:Button ID="FinishPreviousButton" runat="server" CausesValidation="False" 
                    CommandName="MovePrevious" Text="Previous" />
                <asp:Button ID="FinishButton" runat="server" CommandName="MoveComplete"  OnClick="AddPropertyWizard_FinishButtonClick"
                    Text="Finish" />
            </FinishNavigationTemplate>
            <HeaderTemplate>
                Wizard Example
            </HeaderTemplate>
            <StepNavigationTemplate>
                <asp:Button ID="StepPreviousButton" runat="server" CausesValidation="False" CommandName="MovePrevious" OnClick="AddPropertyWizard_PreviousButtonClick"
                    Text="Previous" />
                <asp:Button ID="StepNextButton" runat="server" CommandName="MoveNext" Text="Next" OnClick="AddPropertyWizard_NextButtonClick" />
            </StepNavigationTemplate>
                </asp:Wizard>
               
             </td>
    </tr>
    </table>
</div>
<div>
<table>
<tr>
<td>
<asp:Label ID="lbl1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl3" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl4" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl5" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl6" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ClickButton();
       
    }
    public void ClickButton()
    {
       
        Button btn = AddPropertyWizard.FindControl("StartNavigationTemplateContainerID").FindControl("StartNextButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('Are you sure you want to move away from the beginning?')";
        }
        btn = AddPropertyWizard.FindControl("StepNavigationTemplateContainerID").FindControl("StepNextButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('Are you positive you want to move next?')";
        }
        btn = AddPropertyWizard.FindControl("StepNavigationTemplateContainerID").FindControl("StepPreviousButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('Are you sure you want to go back?')";
        }
        btn = AddPropertyWizard.FindControl("FinishNavigationTemplateContainerID").FindControl("FinishPreviousButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "confirm('Almost there!?')";
              
        }
        btn = AddPropertyWizard.FindControl("FinishNavigationTemplateContainerID").FindControl("FinishButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return showtext()"; //confirm('Almost there!?')
        }
    }
    protected void fill_Click(object sender, EventArgs e)
    {
        lst.Items.Add("One");
        //when listbox2 is filled then make SelectedIndex = 0, otherwise dont set SelectedIndex;
         lst.SelectedIndex = 0;
    }
    protected void AddPropertyWizard_NextButtonClick(object sender, EventArgs e)
    {
        int Index = Convert.ToInt32(AddPropertyWizard.ActiveStepIndex);
        switch (Index)
        {
            case 0:
                AddPropertyWizard.ActiveStepIndex = 1;
                break;
            case 1:
                AddPropertyWizard.ActiveStepIndex = 2;
                break;
            case 2:
                AddPropertyWizard.ActiveStepIndex = 3;
                break;
        }
    }
    protected void AddPropertyWizard_PreviousButtonClick(object sender, EventArgs e)
    {
        int Index = Convert.ToInt32(AddPropertyWizard.ActiveStepIndex);
        switch (Index)
        {
            case 1:
                AddPropertyWizard.ActiveStepIndex = 0;
                break;
            case 2:
                AddPropertyWizard.ActiveStepIndex = 1;
                break;
            case 3:
                AddPropertyWizard.ActiveStepIndex = 2;
                break;
        }
    }
    protected void AddPropertyWizard_FinishButtonClick(object sender, EventArgs e)
    {
        lbl1.Text = "The UserName is :" + txtuser.Text + "\n";
        lbl2.Text = "The Password is:" + txtpass.Text + "\n";
        lbl3.Text = "The Address is:" + txtAddress.Text + "\n";
        lbl4.Text = "The Phone is:" + txtPhone.Text + "\n";
        lbl5.Text = "The Office is:" + txtOffice.Text + "\n";
        lbl6.Text = "The Map is:" + txtMap.Text + "\n";
       // 
    }
}


Saturday, December 31, 2011

Save Image without Upload Control

Table Structure 
-------------------

DatabaseTable

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
        &nbsp;<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /><br />
        <br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label><br />
        <br />
        <br />
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" DataKeyNames="ID"
              DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" 
                InsertVisible="False" ReadOnly="True"
                               SortExpression="ID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName" 
                               SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="Image1" runat="server" 
           ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</td>
</tr>
<tr>
<td>
<%--<asp:Image ID="Image2" runat="server" 
           ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>--%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [ImageName], [Image] 
              FROM [Images]"></asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    public static byte[] ImageToBinary(string imagePath)
    {
        FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[fileStream.Length];
        fileStream.Read(buffer, 0, (int)fileStream.Length);
        fileStream.Close();
        return buffer;
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strImageName = txtName.Text.ToString();
        //if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        //{
            FileStream fileStream = new FileStream(@"D:\Aspnet Project\ImagesInGridViewDb\Images\tree.jpg", FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, (int)fileStream.Length);
            fileStream.Close();
             ImageToBinary(@"D:\Aspnet Project\ImagesInGridViewDb\Images\tree.jpg");
           
          //   byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
          //  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
           // uploadedImage.InputStream.Read(imageSize, 0, (int)fileStream.Length);
            // Create SQL Connection 
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            // Create SQL Command 
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Images(ImageName,Image) VALUES (@ImageName,@Image)";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);
            ImageName.Value = strImageName.ToString();
            cmd.Parameters.Add(ImageName);
            SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, buffer.Length);
            UploadedImage.Value = buffer;//imageSize;
            cmd.Parameters.Add(UploadedImage);
            con.Open();
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result > 0)
                lblMessage.Text = "File Uploaded";
            GridView1.DataBind();
        //}
    }
}



Handler.ashx

------------------

 

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

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        // Create SQL Command 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ImageName,Image from Images where ID =@ID";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["Image"]);
        dReader.Close();
        con.Close();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
    
}



Output
--------
GridViewImage1

Save Image to Folder and Database

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
        &nbsp;<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /><br />
        <br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label><br />
        <br />
        <br />
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" DataKeyNames="ID"
              DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" 
                InsertVisible="False" ReadOnly="True"
                               SortExpression="ID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName" 
                               SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="Image1" runat="server" 
           ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</td>
</tr>
<tr>
<td>
<%--<asp:Image ID="Image2" runat="server" 
           ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>--%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [ImageName], [Image] 
              FROM [Images]"></asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>


Default.aspx.cs
------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strImageName = txtName.Text.ToString();
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
            uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
            // Create SQL Connection 
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            // Create SQL Command 
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Images(ImageName,Image) VALUES (@ImageName,@Image)";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlParameter ImageName = new SqlParameter("@ImageName", SqlDbType.VarChar, 50);
            ImageName.Value = strImageName.ToString();
            cmd.Parameters.Add(ImageName);
            SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);
            UploadedImage.Value = imageSize;
            cmd.Parameters.Add(UploadedImage);
            con.Open();
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result > 0)
                lblMessage.Text = "File Uploaded";
            GridView1.DataBind();
        }
    }
}


Handler.ashx
---------------
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) 
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        // Create SQL Command 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ImageName,Image from Images where ID =@ID";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["Image"]);
        dReader.Close();
        con.Close();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
    
}


Output:
---------
GridViewImage1