how to select a date from calender check that date from database; display those only those data which selected data 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 UserView : System.Web.UI.Page
    {



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

        
                   
            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 Username = @Username", conn);
            try
            {
                using (conn)
                {
                    conn.Open();
                    SqlParameter Username = new SqlParameter();
                    Username.ParameterName = "@Username";
                    Username.SqlDbType = System.Data.SqlDbType.VarChar;
                    Username.Value = TextBox1.Text.Trim();
                    cmd.Parameters.Add(Username);
                    GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                    GridView1.Visible = true;

                }

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

    }
}

Recommended Answers

All 6 Replies

You need to write SQL statement,

string sql="select * from tableName where DateColumn=@DateColumn";

and use IsPostBack property to show all rows from the first load of page.

protected void Page_Load(object sender, EventArgs e)
        {

          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();
            }
           }
        }

i apply your instruction but after that it gives error
" Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition."

Remove DataSource and DataSourceID properties value from GridView1.

like that can u check it
i attached my code but i dubugging also its not giving any output

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);
            }
        }

    }
}

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

Do not use SqlDataSource control because in this code you are binding GridView manually.

can you tell me where i have to rid sqldatasource..

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.