hello,
i would like filter by year/ month
there is a simple guide like this Filter DataTable and Bind to GridView in ASP.Net using C# and VB.Net
i want change country to datetimepicker, so if i want filter by year / month ( 2020 / 5 )
but when i use my code, i get different
when i select 1-8-2021 i get from 5-8-2021
when i select 12-8-2021 i get also 5-8-2021
c#
protected void OnSearch(object sender, EventArgs e)
{
string dob = Request.Form[txtBoxDatestart.UniqueID];
DataTable dt = this.GetData("SELECT * FROM Table_vacation");
if (!string.IsNullOrEmpty(dob))
{
DateTime selectedDate = Convert.ToDateTime(dob);
string query = string.Format("SELECT * FROM Table_vacation WHERE DATEPART(YEAR,dateend) = {0} AND DATEPART(MONTH,dateend) = {1}",
selectedDate.Year, selectedDate.Month);
dt = this.GetData(query);
}
gvFiles.DataSource = dt;
gvFiles.DataBind();
txtBoxDatestart.Text = dob;
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["kankonConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = conn;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
return dt;
}
<asp:TextBox ID="txtBoxDatestart" runat="server" AutoPostBack="True"
BackColor="#99CCFF" BorderColor="#99CCFF" CssClass="auto-style182"
Font-Bold="True" Font-Names="Arial" Font-Size="Medium"
Style="text-align: center"></asp:TextBox>
<script type="text/javascript">
$(function () {
$("#<%= txtBoxDatestart.ClientID %>").datepicker({
dateFormat: 'yy/mm/dd',
changeMonth: true,
changeYear: true,
yearRange: '2015:2035',
showButtonPanel: true,
beforeShowDay: function (date) {
// Sets Weekend Friday and Saturday.
var weekend = date.getDay() == 5 || date.getDay() == 6;
return [!weekend, 'myweekend'];
},
// Sets Weekend position.
firstDay: 6,
closeText: "إغلاق",
prevText: "<السابق",
nextText: "التالي>",
currentText: "اليوم",
monthNames: ["يناير", "فبراير", "مارس", "أبريل", "مايو", "يونيو", "يوليو", "أغسطس", "سبتمبر", "أكتوبر", "نوفمبر", "ديسمبر"],
monthNamesShort: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
dayNames: ["الأحد", "الاثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة", "السبت"],
dayNamesShort: ["ح", "ن", "ث", "ر", "خ", "ج", "س"],
dayNamesMin: ["أحد", "اثنين", "ثلاثاء", "أربعاء", "خميس", "جمعة", "سبت"],
weekHeader: "أسبوع",
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ""
});
});
</script>
<asp:Button Text="Search" runat="server" OnClick="OnSearch" CssClass="button" />