Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
    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.


What I have tried:

C#
<pre>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>
Posted
Updated 13-Jul-17 21:28pm
Comments
Karthik_Mahalingam 14-Jul-17 2:26am    
what is the issue
Member 12605293 14-Jul-17 2:47am    
Hi Karthik,
Nice to see you back,Dont know how to use WebMethod
Karthik_Mahalingam 14-Jul-17 3:27am    
what is params

1 solution

refer this
Calling ASP.Net WebMethod using jQuery AJAX[^]
try with static method
 
Share this answer
 
Comments
Member 12605293 14-Jul-17 3:49am    
Yes Karthik,this works Fine.But in my case then function is not been called
Karthik_Mahalingam 14-Jul-17 4:04am    
does it hit the breakpoint.
Member 12605293 14-Jul-17 3:57am    
Hi Karthik
[System.Web.Services.WebMethod]
    public string GetDetails()
    {
        DateTime date3 = System.DateTime.Now;
        DateTime date4 = System.DateTime.Now;
       // DateTime date1 = System.DateTime.Now.AddDays(-7); ;
       // DateTime date2 = System.DateTime.Now.AddDays(-7);
        DateTime.TryParse(txtFromDate.Text, out date3);
        DateTime.TryParse(txtToDate.Text, out date4);
        string Id = "SS472";
        string Leave = "Select EmpID ,LeaveType,LeaveFromDate,LeaveToDate,LeaveStatus from LeaveApplication Where LeaveFromDate  >= '" + date3.AddDays(-7) + "' and LeaveToDate  <= '" + date4.AddDays(-7) + "' and EmpID  = '" + Id + "'  ";
        scon = new SqlConnection(conString);
        scmd = new SqlCommand(Leave, scon);
        scon.Open();
        sda = new SqlDataAdapter(scmd);
        ds = new DataSet();
        ds.Clear();
        sda.Fill(ds, "LeaveApplication");
        table = ds.Tables[0];
        String Value=""; 
        for (int i = 0; i < table.Rows.Count; i++)
        {

            if ((table.Rows[i]["LeaveStatus"].ToString() == "Accepted") || (table.Rows[i]["LeaveStatus"].ToString() == "Pending") || (table.Rows[i]["LeaveHalfDayFrom"].ToString() == "FN"))
            {
                Value = "Leave Not Allowed";
            }

            else if (table.Rows[i]["LeaveStatus"].ToString()=="Rejected")
            { 
            
            }

        }
        scon.Close();


        return Value;
    }


<input id="btnGetResponses" type="button" value="Show Current Time"  />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGetResponses").click(function () {
                $.ajax({
                    type: "POST",
                    url: "LMSEmployee.aspx/GetDetails",
                    data: '{}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        if (response.d == true) {
                            alert("Abc");
                            
                        }
                    },
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            });
        });
        </script>

Karthik_Mahalingam 14-Jul-17 4:04am    
try with static keyword,
check for any error in chrome console
Member 12605293 14-Jul-17 8:49am    
I Have tried but it s not been called

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900