Tuesday 31 January 2012

Date Validation in Asp.Net to Retrive Data from Database

Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DateDifference.aspx.cs" Inherits="DateDifference" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Date"></asp:Label>
        <asp:TextBox ID="txtdate1" runat="server"></asp:TextBox>
        <asp:CalendarExtender ID="txtdate1_CalendarExtender" runat="server"
            Enabled="True" TargetControlID="txtdate1">
        </asp:CalendarExtender>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Sale"></asp:Label>
        <asp:TextBox ID="txtdate2" runat="server"></asp:TextBox>
       
       
        <br />
        <br />
        <asp:Button ID="btn_Insert" runat="server" onclick="btn_Insert_Click"
            Text="Insert" />
       
        <asp:Button ID="btn_grid" runat="server" Text="Gridfill"
            onclick="btn_grid_Click" />
        <br />
        <br />
   
        <br />
        <asp:GridView ID="GridView1" runat="server" BackColor="White"
            BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
            onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing">
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <RowStyle BackColor="White" ForeColor="#003399" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <SortedAscendingCellStyle BackColor="#EDF6F6" />
            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
            <SortedDescendingCellStyle BackColor="#D6DFDF" />
            <SortedDescendingHeaderStyle BackColor="#002876" />

            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text='<%#Eval("ID")%>' CommandArgument='<%#Eval("ID")%>' />
            </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="From"></asp:Label>
        <asp:TextBox ID="TxtFromDate" runat="server"></asp:TextBox>
       
        <asp:CalendarExtender ID="TxtFromDate_CalendarExtender" runat="server"
            Enabled="True" TargetControlID="TxtFromDate">
        </asp:CalendarExtender>
       
        <asp:Label ID="Label4" runat="server" Text="To"></asp:Label>
        <asp:TextBox ID="Txttodate" runat="server"></asp:TextBox>
    
        <asp:CalendarExtender ID="Txttodate_CalendarExtender" runat="server"
            Enabled="True" TargetControlID="Txttodate">
        </asp:CalendarExtender>
        <asp:Button ID="btn_Retrive" runat="server" Text="Show"
            onclick="btn_Retrive_Click" />
    
        <br />
        <asp:GridView ID="GridView2" runat="server" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>
        <br />
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
    </div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    </form>
</body>
</html>
Design :-



RunTime Design
 
C# Coading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Globalization;

public partial class DateDifference : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=SWASH-DBS\\SWASHSQLINT;database=kenCampus;uid=ken;pwd=kc@2011");
    SqlDataAdapter da;
    SqlCommand cmd;
    DataTable dt;
    string fromdate = "";
    string todate = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            con.Open();
        }
    }
    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        string ins = "Insert into Demo1 values('"+txtdate1.Text+"','"+txtdate2.Text+"')";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand(ins, con);
        cmd.ExecuteNonQuery();
    }
    protected void btn_grid_Click(object sender, EventArgs e)
    {
        string sel = "select ID,convert(varchar(20),Date1,106) as Date,Sale from Demo1";//Example date retrive [12/Jan/2012]
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(sel, con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            string se = "Select Date1,Sale from Demo1 where ID='"+int.Parse(e.CommandArgument.ToString())+"'";
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(se, con);
            dt = new DataTable();
            da.Fill(dt);
            txtdate1.Text = dt.Rows[0]["Date1"].ToString();
            txtdate2.Text = dt.Rows[0]["Sale"].ToString();
        }

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    private bool ValidateDate()
    {
       
        if (TxtFromDate.Text != "")
        {
            fromdate = string.Format("{0:yyyy/MM/dd}", TxtFromDate.Text);
        }
        if (Txttodate.Text == "")
        {
            DateTime dt = DateTime.Now.Date;
            //todate = dt.ToString("dd/MM/yyyy");
            todate = dt.ToString("yyyy/MM/dd");
        }
        else
        {
            todate = string.Format("{0:yyyy/MM/dd}", Txttodate.Text);
        }

     
        return true;

    }
    protected void btn_Retrive_Click(object sender, EventArgs e)
    {
        if (ValidateDate() == true)
        {
            string sel = "select convert(varchar(20),Date1,106) as Date,Sale from Demo1 where date1 >= '" + fromdate + "' and date1 <= '" + todate + "'";//Example date retrive [12/Jan/2012]
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(sel, con);
            dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {

                GridView2.DataSource = dt;
                GridView2.DataBind();
            }
            else
            {
                Label5.Text = "From Date Should Not be less than todate";
            }
        }
    }
}


Table Design



2 comments:

  1. Satya, It's a good effort.
    One thing you need to remember while delivering a post is- it is accessible globally by anyone. So, you should post only the intended code snippet. Putting the whole code of a page will mess-up the reader. Instead of it you can provide a link to download the sample project/page.

    ReplyDelete
    Replies
    1. Thanku Sir for your suggestion.But one thig sir that type of facilities are not avlaible in Blogspot.so .......

      Delete