Drop down list ,Based on two dates , display data in grid view based on selection

Posted by Raja_89 under ASP.NET on 3/14/2013 | Points: 10 | Views : 3946 | Status : [Member] | Replies : 1
Hai
I am having drop down list,Grid view ,in web application.
Based on dates selected on two separated drop down list grid view should be displayed from sql server

Thanks




Responses

Posted by: Satyapriyanayak on: 3/14/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx.cs" Inherits="WebApplication16.WebForm12" %>

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

</div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication16
{
public partial class WebForm12 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
SqlDataAdapter sqlda;
DataSet ds;

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from t3 where ddate between '" + DropDownList1.SelectedItem.Text + "' AND '" + DropDownList2.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds,"t3");
GridView1.DataMember = "t3";
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();

}

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList1.Items.Add("Choose date");
con.Open();
str = "select * from t3";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["ddate"].ToString());
}
reader.Close();
con.Close();
}

if (!IsPostBack)
{
DropDownList2.Items.Add("Choose date");
con.Open();
str = "select * from t3";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList2.Items.Add(reader["ddate"].ToString());
}
reader.Close();
con.Close();
}
}
}
}


If this post helps you mark it as answer
Thanks

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response