I am trying to give an alert message using "WebMethod",where my conditions are as follows
1.I am trying to restrict the user applying leave on "Monday" when he/She has already taken Leave on the previous friday. 2.I am geeting the details from my Database where the employees leave Details and trying to code this in WebMethod
My cs page code:
[System.Web.Services.WebMethod]
public string GetCurrentTime()
{
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cn = new SqlCommand();
DateTime date3 = System.DateTime.Now;
DateTime date4 = System.DateTime.Now;
DateTime date1 = System.DateTime.Now.AddDays(-6); ;
DateTime date2 = System.DateTime.Now.AddDays(-6);
DateTime.TryParse(txtFromDate.Text, out date1);
DateTime.TryParse(txtToDate.Text, out date2);
// string val;
// var EmpID = "SS212";
SqlDataAdapter da = new SqlDataAdapter(scmd);
DataTable dt=new DataTable();
da.Fill(dt);
sdr = scmd.ExecuteReader();
if (date1.DayOfWeek == DayOfWeek.Monday && date2.DayOfWeek == DayOfWeek.Monday)
{
string Leave = "Select EmpID ,LeaveType,LeaveFromDate,LeaveToDate,LeaveStatus from LeaveApplication Where LeaveFromDate = '" + date1 + "' and LeaveToDate = '" + date2 + "'";
scmd = new SqlCommand(Leave, scon);
}
for(int i = 0; i < dt.Rows.Count; i++)
{
String value ;
if ((dt.Rows[i]["LeaveStatus"].ToString() == "Accepted") || (dt.Rows[i]["LeaveStatus"].ToString() == "Pending"))
{
value="";
}
else
{
value = "";
}
}
return "";
}
My aspx:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function ShowCurrentDate() {
$.ajax({
type: "POST",
url: "LMSEmployee.aspx/GetCurrentTime",
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert("Please");
}
});
}
function OnSuccess(response) {
alert("Please");
}
</script>
Webmethod works only with static functions so try
public static string GetCurrentTime()
instead of
public string GetCurrentTime()
note that if you do so then event driven features which usually comes in event driven functions(IE server side click event) wont work
there is an awesome post by Mike at
example //just a car class
public class Cars
{
public string carName;
public string carRating;
public string carYear;
}
//Your Webmethod*
[WebMethod]
public List<Cars> getListOfCars(List<string> aData)
{
SqlDataReader dr;
List<Cars> carList = new List<Cars>();
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "spGetCars";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@makeYear", aData[0]);
con.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
while (dr.Read())
{
string carname = dr["carName"].ToString();
string carrating = dr["carRating"].ToString();
string makingyear = dr["carYear"].ToString();
carList.Add(new Cars
{
carName = carname,
carRating = carrating,
carYear = makingyear
});
}
}
}
}
return carList;
}
//*
//Your Client Side code
$("#myButton").on("click", function (e) {
e.preventDefault();
var aData= [];
aData[0] = $("#ddlSelectYear").val();
$("#contentHolder").empty();
var jsonData = JSON.stringify({ aData:aData});
$.ajax({
type: "POST",
//getListOfCars is my webmethod
url: "WebService.asmx/getListOfCars",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json", // dataType is json format
success: OnSuccess,
error: OnErrorCall
});
function OnSuccess(response) {
console.log(response.d)
}
function OnErrorCall(response) { console.log(error); }
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.