rahigoswami 0 Light Poster

how to select a date from calender check that date from database; display those only those data which selected date consist

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.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace manage_logs
{
    public partial class Time : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.Visible = false;
            if (!IsPostBack)
            {


                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                SqlCommand cmd = new SqlCommand("select * from [Logs]", conn);
                using (conn)
                {
                    conn.Open();
                   GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                }

             }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("select * from [Logs] where DateColumn=@DateColumn", conn);

            try
            {
                using (conn)
                {
                    conn.Open();
                    SqlParameter param1 = new SqlParameter();
                    param1.ParameterName = "@DateColumn";
                    param1.SqlDbType = System.Data.SqlDbType.Date;
                    param1.Value = Calendar1.SelectedDate;
                    cmd.Parameters.Add(param1);
                    GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                  GridView1.Visible = true;

                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

    }
}

source code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Date.aspx.cs" Inherits="manage_logs.Time" %>

<!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>

        DateView</div>
    <asp:Label ID="Label1" runat="server" 
        style="z-index: 1; left: 61px; top: 105px; position: absolute; width: 89px; height: 23px" 
        Text="Select Date"></asp:Label>
    <asp:Calendar ID="Calendar1" runat="server" 

        style="z-index: 1; left: 211px; top: 38px; position: absolute; height: 188px; width: 259px">

    </asp:Calendar>
    <asp:Button ID="Button1" runat="server" 
        style="z-index: 1; left: 210px; top: 268px; position: absolute" Text="Submit" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" EnableModelValidation="True" 
        style="z-index: 1; left: 648px; top: 168px; position: absolute; height: 244px; width: 345px">
        <Columns>
            <asp:BoundField DataField="MangerId" HeaderText="MangerId" 
                SortExpression="MangerId" />
            <asp:BoundField DataField="UserName" HeaderText="UserName" 
                SortExpression="UserName" />
            <asp:BoundField DataField="CheckIntime" HeaderText="CheckIntime" 
                SortExpression="CheckIntime" />
            <asp:BoundField DataField="CheckOuttime" HeaderText="CheckOuttime" 
                SortExpression="CheckOuttime" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT * FROM [Logs]"></asp:SqlDataSource>
    </form>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.