Getting Date Picker value in a grid view

Posted by Lokanadh999 under ASP.NET on 9/24/2011 | Points: 10 | Views : 3895 | Status : [Member] | Replies : 5
Hi,

I have a web page, this page have two date picker controls(one for startdate,another end date) and one button control,gridview control,sqldata source contrl.. I created a table (expenses) in sql server2005 contains three columns(itemname,itemprice,dateloaded).

I need to display the data in grid view when I clikck button, the result must be based on the given start date and end date, like

select * from expenses where dateloaded between '&datepicker1' and '&datepicker2'

Is it possible?

would you please help me, how can I do this




Responses

Posted by: Blessyjees on: 9/26/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Yes. I am not sure this is possible with sql datasource, I didn't use sqldatasource with grid view. You can easily implement this with dataset.
SqlConnection sqlConnection = new SqlConnection("server=(local);database=[dbname];user id=username;password=password;connection reset=false;connection lifetime=5;min pool size=1;max pool size=50;");

sqlConnection.Open();
DataSet ds = new DataSet();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select * from expenses where dateloaded between '&datepicker1' and '&datepicker2'
", sqlConnection );// you should set datevalue
sqlDataAdapter .Fill(ds);
dataGrid.DataSource=ds;
dataGrid.Bind();


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

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

Posted by: Lokanadh999 on: 9/26/2011 [Member] Starter | Points: 25

Up
0
Down
Thank you.

I tried but I got an error like "Conversion failed when converting datetime from character string.",
plz find the attachment I given my default.aspx code and default.aspx.cs :



Default.aspx Page:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="BasicFrame.WebControls.BasicDatePicker" namespace="BasicFrame.WebControls" tagprefix="BDP" %>

<!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>
<BDP:BasicDatePicker ID="BasicDatePicker3" runat="server" />
<BDP:BasicDatePicker ID="BasicDatePicker4" runat="server" />
<asp:Button ID="Button3" runat="server" Text="submit" onclick="Button3_Click" />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" >
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:BoundField DataField="sno" HeaderText="sno" InsertVisible="False"
ReadOnly="True" SortExpression="sno" />
<asp:BoundField DataField="Dateloaded" HeaderText="Dateloaded"
SortExpression="Dateloaded" />
<asp:BoundField DataField="Itemname" HeaderText="Itemname"
SortExpression="Itemname" />
<asp:BoundField DataField="Itemprice" HeaderText="Itemprice"
SortExpression="Itemprice" />
</Columns>
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>

</form>
</body>
</html>


Default.aspx.cs:



using System;
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;


public partial class _Default : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString);

sqlConnection.Open();

DataSet ds = new DataSet();

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select * from expenses where dateloaded between '&datepicker3' and '&datepicker4' ", sqlConnection );
sqlDataAdapter.Fill(ds);



GridView2.DataSource = ds;

GridView2.DataBind();

}
}

I hope u will give me correct path to solve this one.



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

Posted by: Blessyjees on: 9/27/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Please use selected value of datepickers in the case datepicker3 and datepicker4. I can't see these variables in your code.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

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

Posted by: Lokanadh999 on: 9/27/2011 [Member] Starter | Points: 25

Up
0
Down
sorry for confussion I used in code as follows:
<BDP:BasicDatePicker ID="datepicker3" runat="server" />
<BDP:BasicDatePicker ID="datepicker4" runat="server" />

in.cs file as follows:

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select * from expenses where dateloaded between '&datepicker3' and '&datepicker4' ", sqlConnection );


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

Posted by: Blessyjees on: 9/28/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Are you getting the value of datepicker while using only ID like "datepicker3"? just debug and find the value of datepicker or how to get the value and use that value in the query

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

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

Login to post response