protected void Page_Load(object sender, EventArgs e)
{
Session["ID"] = "2";
string strConnString = ConfigurationManager.ConnectionStrings["BSDConnectionString"].ConnectionString;
var con = new SqlConnection(strConnString);
using (var sda = new SqlDataAdapter())
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT CONVERT(DATE,proj_start_date),CONVERT(DATE,proj_end_date) FROM [BSD].[dbo].[d_project_p] WHERE proj_id = " + Session["ID"] + " ";
con.Open();
cmd.Connection = con;
sda.SelectCommand = cmd;
using (var dt = new DataTable())
{
sda.Fill(dt);
con.Close();
List<string> arr = new List<string>();
arr.Add(dt.Rows[0][0].ToString());
arr.Add(dt.Rows[0][1].ToString());
Response.Write(arr[0] + " <br/>" + arr[1] ); ;
}
}
}
[WebMethod]
public static List<string> GetDates()
{
string strConnString = ConfigurationManager.ConnectionStrings["BSDConnectionString"].ConnectionString;
var con = new SqlConnection(strConnString);
using (var sda = new SqlDataAdapter())
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT CONVERT(DATE,proj_start_date) AS S,CONVERT(DATE,proj_end_date) AS E FROM [BSD].[dbo].[d_project_p] WHERE proj_id = " + HttpContext.Current.Session["ID"] + "";
using (var dt = new DataTable())
{
con.Open();
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
List<string> arr = new List<string>();
foreach (DataRow row in dt.Rows )
{
arr.Add(row["S"].ToString());
arr.Add(row["E"].ToString());
}
con.Close();
con.Dispose();
return arr;
}
}
//List<string> arr = new List<string>();
//arr.Add("2014-07-01");
//arr.Add("2015-02-05");
//return arr;
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BindDates_TESTING.aspx.cs"
Inherits="BindDates_TESTING" %>
<!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">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/redmond/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function() {
$.ajax({
type: "POST",
contentType: "application/json",
data: "{}",
url: "BindDates_TESTING.aspx/GetDates",
dataType: "json",
success: function(data) {
$('#TextBox1').datepicker({
defaultDate: new Date(data.d[0]),
minDate: new Date(data.d[0]),
maxDate: new Date(data.d[1]),
hideIfNoPrevNext: true,
showAnim: "fold",
changeMonth: true,
changeYear: true,
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(textStatus + ' ' + errorThrown);
debugger;
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
That was my code , now lets see what my problem is :
I am getting only two dates in a single row from sql to my asp.net webform and then bindng those dates in jQuery UI Datepicker. Now if i set session to 1 then date picker works well but if i put session = 2 , it shows the end date till 2020 which is wrong.
Below are the dates which are fetched from database and i have copied here for your ease.
When Session["ID"] = "1";
Start Date : 01/07/2014 00:00:00
End Date : 05/02/2015 00:00:00
When Session["ID"] = "2";
Start Date : 07/04/2015 00:00:00
End Date : 27/08/2016 00:00:00
I have set my mindate to startdate and maxdate to end date. please check and see where the error is happening.
Also point of interest is that if i don't fetch values from database and use only List<string> in my web method then every thing works well. like this :
[WebMethod]
public static List<string> GetDates()
{
List<string> arr = new List<string>();
arr.Add("2014-07-01");
arr.Add("2015-02-05");
return arr;
}