Reputation: 1310
I have Two Input Type = "Date" and one Input Type = "Button"
<input type="date" runat="server" id="dtp_from" />
<input type="date" runat="server" id="dtp_to" />
<input type="button" id="btnSubmit" CssClass="custom-btn" value="Submit" />
One GridView Table
<table id="gv_Datatable" class="table table-responsive table-hover">
<thead>
<tr>
<th>Id</th>
<th>Date</th>
<th>Name</th>
<th>Description</th>
<th>Receipt</th>
<th>Payment</th>
<th>Balance</th>
<th>Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>Date</th>
<th>Name</th>
<th>Description</th>
<th>Receipt</th>
<th>Payment</th>
<th>Balance</th>
</tr>
</tfoot>
</table>
Now, here when click on button ajax method is called and through parameter two date are passing to the asmx.cs file.
$("#btnSubmit").click(function (e) {
var dataToSend = {
param1: JSON.stringify(document.getElementById("<%=dtp_from.ClientID%>").value),
param2: JSON.stringify(document.getElementById("<%=dtp_to.ClientID%>").value)
};
$.ajax({
type: "POST",
url: "FillGridMethod.asmx/CashBookList",
data: dataToSend,
dataType: "json",
success: function (data) {
var datatableVariable = $('#gv_Datatable').DataTable({
dom: 'Bfrtip',
data: data,
columns: [
{ 'data': 'Id', visible: false },
{
'data': 'cashbookdate', 'render': function (date) {
var date = new Date(parseInt(date.substr(6)));
var month = date.getMonth() + 1;
return date.getDate() + "/" + month + "/" + date.getFullYear();
}
},
{ 'data': 'cashbookaccname' },
{ 'data': 'cashbookdescription' },
{ 'data': 'cashbookreceipt' },
{ 'data': 'cashbookpayment' },
{ 'data': 'Balance' },
{
"render": function (data, type, row) { return "<a href='#' class='btn btn-success' onclick=DeleteCustomer('" + row.Id + "');>View</>"; }
},
{
"render": function (data, row) { return "<a href='#' class='btn btn-danger'>Delete</a>"; }
}]
});
}
});
});
Here is the asmx.cs file code
[WebMethod(enableSession: true)]
public void CashBookList(string param1, string param2)
{
DateTime fromDate = DateTime.ParseExact(param1, "dd/MM/yyyy", CultureInfo.InvariantCulture);
var cashBook = new List<CashBookModel>();
string constr = cn.ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
qryFillGrid = " select cashbookid, cashbookdate, cashbookaccname, cashbookdescription, cashbookreceipt, cashbookpayment, Balance from tbl_cashbook " + System.Environment.NewLine;
qryFillGrid += " where BranchID = " + HttpContext.Current.Session["BranchID"] + " " + System.Environment.NewLine;
if (HttpContext.Current.Session["AccountMode"].ToString() != "ALL")
{
qryFillGrid += " and AccountMode = '" + HttpContext.Current.Session["AccountMode"].ToString() + "' " + System.Environment.NewLine;
}
if(param1 != "\"\"")
{
qryFillGrid += " and convert(varchar(10), convert(datetime, cashbookdate,105),112) >= '" + Convert.ToDateTime(service.IfNullThen(fromDate, DateTime.Now.Date)).Date.ToString("yyyyMMdd") + "' " + System.Environment.NewLine;
if (param2 != "\"\"")
{
qryFillGrid += " and convert(varchar(10), convert(datetime, cashbookdate,105),112) <= '" + Convert.ToDateTime(service.IfNullThen(param2, DateTime.Now.Date)).Date.ToString("yyyyMMdd") + "' " + System.Environment.NewLine;
}
}
else
{
qryFillGrid += " and convert(varchar(10), convert(datetime, cashbookdate,105),112) = '" + System.DateTime.Now.Date + "' " + System.Environment.NewLine;
}
qryFillGrid += " order by cashbookdate, cashbookid desc " + System.Environment.NewLine;
var cmd = new SqlCommand(qryFillGrid, con);
con.Open();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
var cashBookModel = new CashBookModel
{
Id = Convert.ToInt32(dr[0]),
cashbookdate = Convert.ToDateTime(dr[1]),
cashbookaccname = dr[2].ToString(),
cashbookdescription = dr[3].ToString(),
cashbookreceipt = Convert.ToDecimal(service.IfNullThen(dr[4], 0)),
cashbookpayment = Convert.ToDecimal(service.IfNullThen(dr[5], 0)),
Balance = Convert.ToDecimal(service.IfNullThen(dr[6], 0)),
};
cashBook.Add(cashBookModel);
}
}
var js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(cashBook));
}
DateTime fromDate = DateTime.ParseExact(param1, "dd/MM/yyyy", CultureInfo.InvariantCulture);
In this below line of asmx.as file getting error i.e.: 'String was not recognized as a valid DateTime.'
Upvotes: 0
Views: 40