Click here to Skip to main content
15,174,547 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 22: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
Richard Deeming 14-Jul-17 10:03am
   
Also, it looks like you're storing dates as strings. That will only lead to problems. Use one of the more appropriate data types instead:
Date and Time Types[^]

1 solution

refer this
Calling ASP.Net WebMethod using jQuery AJAX[^]
try with static method
   
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
Karthik_Mahalingam 14-Jul-17 10:27am
   
any error in console?
Member 12605293 17-Jul-17 1:30am
   
The function name is not showing in console.Between I want one more thing.
Karthik_Mahalingam 17-Jul-17 1:35am
   
what
Member 12605293 17-Jul-17 1:45am
   
I Have implemeted the below code to meet my requirement,

In a complany ,if an employee is taking leave on friday and the immediate monday he is not allowed to apply leave,on every monday the previous friday is monitored.
Frm date and Todate are used on Calendar Control and datas are fetched from DB.
[System.Web.Services.WebMethod]
    public static string GetDetails(DateTime date3, DateTime date4)
    {
        string conString = System.Configuration.ConfigurationManager.ConnectionStrings["TimeSheetConnectionString2"].ConnectionString;
        SqlConnection scon;
        SqlCommand scmd;
        SqlDataAdapter sda;
        SqlDataReader sdr;
        DataSet ds;
        DataTable dtab;
        DataTable table;
        if (HttpContext.Current != null)
        {
            date3 = System.DateTime.Now;
            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 + "'  ";
        
       
        
        scmd = new SqlCommand(Leave);
       
        sda = new SqlDataAdapter(scmd);
        ds = new DataSet();
        ds.Clear();
        sda.Fill(ds, "LeaveApplication");
        table = ds.Tables[0];
        string message = string.Empty;
        for (int i = 0; i < table.Rows.Count; i++)
        {

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

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

        }
        


        return message;
    }
Member 12605293 19-Jul-17 6:43am
   
Hi Karthik
Karthik_Mahalingam 19-Jul-17 6:49am
   
Hi
Member 12605293 19-Jul-17 6:52am
   
I have a doubt in my C# logic,dont know how to check,Can you guide me.
Karthik_Mahalingam 19-Jul-17 6:53am
   
What logic
Member 12605293 19-Jul-17 6:57am
   
I have to Restrict candidates from Applying leave for the days comming next and before weekdays
Like fri,sat,sun,mon.
I have to show alert message on two cases like
when a person takes leave on monday,it should the previous friday whether he has applied leave on friday and not allow the candiate to apply on monday.
Member 12605293 19-Jul-17 7:02am
   
I have my code for it
Karthik_Mahalingam 19-Jul-17 10:02am
   
team viewer
Member 12605293 20-Jul-17 1:02am
   
Hi Karthik,
I am not allowed to use Team Viewer at Office :(
Karthik_Mahalingam 20-Jul-17 1:48am
   
oh, now i am in office
ping me after 6pm
Member 12605293 20-Jul-17 1:53am
   
ok
Member 12605293 20-Jul-17 9:47am
   
Hi.
Karthik_Mahalingam 20-Jul-17 10:12am
   
hi
Member 12605293 21-Jul-17 1:10am
   
Hi Karthik,
GOod Mor
Karthik_Mahalingam 21-Jul-17 1:29am
   
hi
Member 12605293 21-Jul-17 1:31am
   
Shall I post my Code here,I need a clarification karthik,Now I am in office and unable to use Team Viewer
Karthik_Mahalingam 21-Jul-17 1:50am
   
ok what clarification you needed
Member 12605293 21-Jul-17 2:12am
   
When I fill both the textboxes.the blur event fires .After the error message,when i tried to change value in first textbox the event fires before changing the second texbox.
Member 12605293 21-Jul-17 2:12am
   



$(document).ready(function () {
$("[id*=txtFromDate],[id*=txtToDate]").blur(function () {

var obj = {};
obj.date3 = $.trim($("[id*=txtFromDate]").val());
obj.date4 = $.trim($("[id*=txtToDate]").val());
if (obj.date3 != '' && obj.date4 != '') {
var temp = $.ajax({
type: "POST",
url: 'LMSEmployee.aspx/GetCurrentTime',
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
});
temp.success(function (response) {
if (response.d == "Apply") {
$("[id *= btnApply]").attr('disabled', false);
}
else {
$("[id *= btnApply]").attr('disabled', true);
alert(response.d);
}
})

}
return false;
});
});


Karthik_Mahalingam 21-Jul-17 3:10am
   
for that you need to store the prev value in a variable and try
compare if both are not same
Member 12605293 21-Jul-17 3:23am
   
Can you give sample using my code
Karthik_Mahalingam 21-Jul-17 5:29am
   
this is how it works,
what you can do is, after the error message clear the textboxes
the logic of your requiremetn is wrong. replace the blur with a button..
Karthik_Mahalingam 21-Jul-17 5:29am
   
think logically.
Member 12605293 27-Jul-17 1:33am
   
Hi Karthik
Karthik_Mahalingam 27-Jul-17 2:19am
   
hi
Member 12605293 28-Jul-17 1:53am
   
Hi Good Morning
Karthik_Mahalingam 28-Jul-17 1:54am
   
gm
Member 12605293 28-Jul-17 1:58am
   
Hi Karthik
It Shows operator == cannot be applied to operands of type string and bool

table.Rows[0]["ShiftDetails"].ToString() == "false"
Karthik_Mahalingam 28-Jul-17 2:00am
   
how it can be
its a legal syntax
it should work.

bool flag = table.Rows[0]["ShiftDetails"].ToString() == "false";
Member 12605293 28-Jul-17 2:02am
   
This is in if condition Karthik,
if ((table.Rows[0]["LeaveStatus"].ToString() == "Accepted" || table.Rows[0]["LeaveStatus"].ToString() == "Pending" || table.Rows[0]["Status"].ToString() == "Absent") && (bool flag = table.Rows[0]["ShiftDetails"].ToString() == "false" ))
Member 12605293 28-Jul-17 2:04am
   
Already the Column datatype is BIT
Karthik_Mahalingam 28-Jul-17 2:14am
   
 DataRow row = table.Rows[0];
            if ((row["LeaveStatus"].ToString() == "Accepted" || row["LeaveStatus"].ToString() == "Pending" || row["Status"].ToString() == "Absent") && (row["ShiftDetails"].ToString() == "false")) { }

Karthik_Mahalingam 28-Jul-17 2:14am
   
table.Rows[0]["ShiftDetails"].ToString().ToLower() =="false"
Member 12605293 30-Jul-17 10:52am
   
Thanks Karthik :)
Karthik_Mahalingam 31-Jul-17 0:45am
   
welcome
Member 12605293 31-Jul-17 1:21am
   
Karthik,
How to check ie(eiher using ""/null) a column when there is no entry in that column?
Karthik_Mahalingam 1-Aug-17 3:51am
   
bool val = String.IsNullOrEmpty( table.Rows[0]["ColumnName"]) ;
Member 12605293 1-Aug-17 9:54am
   
Thanks karthik
Karthik_Mahalingam 1-Aug-17 10:23am
   
welcome
Member 12605293 1-Aug-17 10:29am
   
Again I have a doubt in Checkbox calling of webmethod
Karthik_Mahalingam 1-Aug-17 11:04am
   
Am outside
Member 12605293 2-Aug-17 2:07am
   
Hi good Mor
Karthik_Mahalingam 2-Aug-17 2:39am
   
gm
Member 12605293 2-Aug-17 3:22am
   
Hi Karthik,Shall I ask my doubt?
Member 12605293 2-Aug-17 3:33am
   
I have two calendar controls and when both texts are filled and onBLUR,the web method hits..On the other Hand I am having a Checkbox and it is Unchecked Intitially,but When it is checked I dnt want the Webmethod to hit.
Karthik_Mahalingam 2-Aug-17 4:56am
   
then check the condition for it, what is the issue
Karthik_Mahalingam 2-Aug-17 4:55am
   
ok
Member 12605293 2-Aug-17 5:11am
   
$(document).ready(function () {
            $("[id*=txtFromDate],[id*=txtToDate],[id*=chkHalfFrom]").blur(function () {

                var obj = {};
                obj.date3 = $.trim($("[id*=txtFromDate]").val());
                obj.date4 = $.trim($("[id*=txtToDate]").val());
                obj.chek1 = chek1 = $('[id*=chkHalfFrom]').is(":checked");

                if (obj.date3 != '' && obj.date4 != '' && !obj.chek1) {
                    var temp = $.ajax({
                        type: "POST",
                        url: 'LMSEmployee.aspx/GetCurrentTime',
                        data: JSON.stringify(obj),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                    });
                    temp.success(function (response) {

                        if (response.d == "Apply") {
                            $("[id *= btnApply]").attr('disabled', false);
                        }
                        else {

                            $("[id *= btnApply]").attr('disabled', true);
                            $("[id *= txtToDate]").val('');
                            alert(response.d);
                        }
                        if (obj.chek1) {
                            $("[id *= btnApply]").attr('disabled', false);
                        }

                    })

                }

                
                return false;
            });
        });
 
Karthik_Mahalingam 2-Aug-17 5:31am
   
run this in console and see what you are getting
$('[id*=chkHalfFrom]').is(":checked");
Member 12605293 2-Aug-17 6:16am
   
Karthik,Two calendar Controls ,selecting a value in Cal Caontrol will show a checkboxes under each Cal control.I want When two cc has value and checkbox is unchecked the webmethod sholud call,else it should not
Member 12605293 3-Aug-17 1:08am
   
Hi Good Morning :)
Member 12605293 4-Aug-17 1:05am
   
Hi Gm :)
Karthik_Mahalingam 6-Aug-17 23:43pm
   
hi gm
i was on vacation for last 4 days
Member 12605293 7-Aug-17 1:20am
   
Its okay Karthik. Good Morg :)
Karthik_Mahalingam 7-Aug-17 1:22am
   
gm
Member 12605293 8-Aug-17 1:41am
   
Hi Good Morni :)
Karthik_Mahalingam 8-Aug-17 3:13am
   
gm
Member 12605293 8-Aug-17 3:27am
   
I want to know how to show differnt color on the row values in GRidview.
Karthik_Mahalingam 8-Aug-17 4:04am
   
https://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-Color-based-on-condition-in-ASPNet-using-C-and-VBNet.aspx
Karthik_Mahalingam 8-Aug-17 4:04am
   
https://stackoverflow.com/a/2193018/1147428
Member 12605293 9-Aug-17 1:06am
   
Hi Karthik,I tried the above sample.But in my case I am using Stored Procedure.
Karthik_Mahalingam 9-Aug-17 1:31am
   
so what, it doesnt make a big diff when using stored procedure, it is just a way to fetch data
Member 12605293 9-Aug-17 1:36am
   
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {     
            string lnkdServerName = "";
            showErrorMsg.Visible = false;
            //if (chkOldDB.Checked == true)
            //{
            //    lnkdServerName = ConfigurationManager.AppSettings["LnkdSeverOld"];
            //    conn = connnew;
            //}
            //else
            //    lnkdServerName = ConfigurationManager.AppSettings["LnkdSever"];

            if (txtStartDate.Text == "")
            {
                txtStartDate.Focus();
                return;
            }
            if (txtEndDate.Text == "")
            {
                txtEndDate.Focus();
                return;
            }
            DateTime sd = Convert.ToDateTime(txtStartDate.Text);
            DateTime ed = Convert.ToDateTime(txtEndDate.Text);


            int countDate = Convert.ToInt32((ed - sd).TotalDays);
            showErrorMsg.Text = "";
            if (countDate >= 31)
            {
                showErrorMsg.Style.Add("margin-left", "0px");
                showErrorMsg.Visible = true;
                showErrorMsg.Text = "Date Range should not greater than 45 days";
                showErrorMsg.ForeColor = Color.Red;
               
                return;
            }

            if (ed < sd)
            {
                Response.Write("<script>alert('From Date should be less than To Date')</script>");
                return;
            }
            DataTable dtwithDates = new DataTable();
            
                if (ddlStatus.SelectedIndex == 0)
                {
                    if (ddlDept.SelectedIndex == 0 && ddlEmp.SelectedIndex == 0)
                    {
                        if (txtStartDate.Text != string.Empty && txtEndDate.Text != string.Empty)
                        {
                            if (txtStartDate.Text.Length == 10 && txtStartDate.Text.ToString().Substring(2, 1) == "/" && txtStartDate.Text.ToString().Substring(5, 1) == "/" && txtEndDate.Text.Length == 10 && txtEndDate.Text.ToString().Substring(2, 1) == "/" && txtEndDate.Text.ToString().Substring(5, 1) == "/")
                            {
                                if (ed > sd)
                                {
                                    dtwithDates.Clear();
                                    if (conn.State == ConnectionState.Closed)
                                    {
                                        conn.Open();
                                    }

                                    
                                }
                            }
                        }
                    }
                    
                   
                }
                else if (ddlStatus.SelectedIndex == 1)
                {
                    if (ddlDept.SelectedIndex == 0 && ddlEmp.SelectedIndex == 0)
                    {
                        if (txtStartDate.Text != string.Empty && txtEndDate.Text != string.Empty)
                        {
                            if (txtStartDate.Text.Length == 10 && txtStartDate.Text.ToString().Substring(2, 1) == "/" && txtStartDate.Text.ToString().Substring(5, 1) == "/" && txtEndDate.Text.Length == 10 && txtEndDate.Text.ToString().Substring(2, 1) == "/" && txtEndDate.Text.ToString().Substring(5, 1) == "/")
                            {
                                if (ed > sd)
                                {
                                    dtwithDates.Clear();
                                    if (conn.State == ConnectionState.Closed)
                                    {
                                        conn.Open();
                                    }
                                    SqlCommand cmdwit
Karthik_Mahalingam 9-Aug-17 1:38am
   
the code has nothing to do with the query you have asked
Member 12605293 9-Aug-17 1:37am
   
What I hv tried is
float TotDur = int.Parse(e.Row.Cells[7].Text);

        foreach (TableCell cell in e.Row.Cells)
        {
            if (TotDur <= 9.0)
            {
                cell.BackColor = Color.Red;
            }

        }
Karthik_Mahalingam 9-Aug-17 2:48am
   
why dont u use the gridview events like onrowbound, or row created event like this..
Member 12605293 10-Aug-17 1:09am
   
Hi Karthik Gm. :)
Karthik_Mahalingam 10-Aug-17 3:20am
   
gm
Member 12605293 10-Aug-17 1:14am
   
As You said I have written my code in Bound event,I have to Show alert color when the Specified Column crosses Below "9:0".But The Input String Is not in correct format Error Araises.
protected void gridEmpList_RowDataBound(object sender, GridViewRowEventArgs e)
    {

      
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[10].Text = "9:5";
            float Record = int.Parse(e.Row.Cells[10].Text);
           
           // String Record = e.Row.Cells[10].Text;
            foreach (TableCell cell in e.Row.Cells)
            {
                if (Record > 9)
                {
                    cell.BackColor = Color.Orange;
                }
            }

        }
    }
Karthik_Mahalingam 10-Aug-17 3:21am
   
why do you parse int value and assign to an float value
float Record = int.Parse(e.Row.Cells[10].Text);

use TryParse
http://www.c-sharpcorner.com/blogs/how-to-use-inttryparse1

Member 12605293 17-Aug-17 1:20am
   
Hi Karthik.Good Mor :)
Karthik_Mahalingam 17-Aug-17 1:53am
   
gm
Member 12605293 17-Aug-17 3:41am
   
From Date ToDate No.of Days Status Reason
2017-06-28 2017-06-28 1 Accepted abc
2017-06-28 2017-06-28 1 Rejected abc
Member 12605293 17-Aug-17 3:42am
   
Hi Karthik.
I need to Restrict Duplicate Entry like the below one
Karthik_Mahalingam 17-Aug-17 3:52am
   
wat is your expected result
Member 12605293 17-Aug-17 3:52am
   
What I have Tried,

Select DISTINCT LeaveFromDate,LeaveToDate, EmpID ,LeaveType,LeaveStatus from IntranetRelease.dbo.LeaveApplication Where  LeaveFromDate >='2017-06-30'  and LeaveToDate  <= '2017-06-30'  and EmpID  = 'SS525'
Karthik_Mahalingam 17-Aug-17 3:53am
   
wat is the issue
Member 12605293 17-Aug-17 4:34am
   
In the Apply Button Click,I am Using an if condition on top
lblLeaveApplyErrorMsg.Visible = false;
        string strLAEmpid = lblEmpID.Text;
        string strLAEmpName = lblEmpName.Text;
        string strLAEmpOffMailID = Session["offId"].ToString();
        string strLALeaveType = ddlLeaveType.SelectedItem.Text;
        string strLALeaveFromDate = txtFromDate.Text;
        string strLALeaveHalfDayFrom;
        string strMP = txtmisspunchfrom.Text;
        string submit_cc = null;
        string submitTo = null;
        string CCmailid = string.Empty;
        ///////////Code To Restrict Duplicate Entry/////////////////
        if (ddlLeaveType.SelectedItem.Text == "Casual Leave" || ddlLeaveType.SelectedItem.Text == "Earned Leave" || ddlLeaveType.SelectedItem.Text == "Sick Leave" || ddlLeaveType.SelectedItem.Text == "Comp Off" || ddlLeaveType.SelectedItem.Text == "Comp Off"
            || ddlLeaveType.SelectedItem.Text == "Wedding Leave" || ddlLeaveType.SelectedItem.Text == "Maternity Leave" || ddlLeaveType.SelectedItem.Text == "Bereavement Leave" || ddlLeaveType.SelectedItem.Text == "Paternity Leave" || ddlLeaveType.SelectedItem.Text == "Floating Leave")
        {
            string chkalreadyapplied = "( Select DISTINCT LeaveFromDate,LeaveToDate, EmpID ,LeaveType,LeaveStatus from IntranetRelease.dbo.LeaveApplication Where  LeaveFromDate >= '" + strLALeaveFromDate + "'   and LeaveToDate  <= '" + strLALeaveFromDate + "'  and EmpID  = 'SS525' )";
            ////  MessageBox.Show(strLAEmpid + strLAEmpName + strLAEmpOffMailID + strLALeaveType + strLALeaveFromDate + strLALeaveHalfDayFrom + strLALeaveToDate + strLALeaveHalfDayTo + douLANoOfDays + strLAReason + strLALeaveStatus + strLALeaveAppliedOn + strLeaveAppliedTo);
            scon = new SqlConnection(conString);
            scmd = new SqlCommand(chkalreadyapplied, scon);
            scon.Open();
            scmd.ExecuteNonQuery();
            scon.Close();
if (ddlLeaveType.SelectedItem.Text == "Permission")
            {
                string permissionapply = "Insert into [Emp_Permission] ([Empid],[Empname],[Date of Permission],[Reason],[Fromtime],[Totime],[TimeDuration],[PerAppliedOn],[PerAppliedTo]) values ('" + strLAEmpid + "','" + strLAEmpName + "','" + strLALeaveFromDate + "','" + strLAReason + "','" + ViewState["Pertimefrom"] + "','" + ViewState["Pertotime"] + "','" + ViewState["tmper"] + "','" + strLALeaveAppliedOn + "', '" + strLeaveAppliedTo + "' )";
                SqlConnection percon = new SqlConnection(conString);
                SqlCommand percmd = new SqlCommand(permissionapply, percon);
                percon.Open();
                percmd.ExecuteNonQuery();
                percon.Close();
            }
            if (ddlLeaveType.SelectedItem.Text == "Miss Punch")        
            {
                string misspunchapply = "Insert into IntranetReleaseNew.dbo.Emp_MissPunch ([Empid],[Empname],[DateofMissPunch],[Reason],[TimeDurInHours],[MisspunchAppliedOn],[MisspunchAppliedTo]) values ('" + strLAEmpid + "','" + strLAEmpName + "','" + strLALeaveFromDate + "','" + strLAReason + "','" + strMP + "','" + strLALeaveAppliedOn + "', '" + strLeaveAppliedTo + "' )";
                SqlConnection misspuncon = new SqlConnection(conString);
                SqlCommand misspunchcmd = new SqlCommand(misspunchapply, misspuncon);
                misspuncon.Open();
                misspunchcmd.ExecuteNonQuery();
                misspuncon.Close();
            }

            if (ddlLeaveType.SelectedItem.Text == "Work from Home")
            {
                string WFHapply = "Insert into [WorkFromHome] ([Empid],[Empname],[Date of WFH],[Reason],[Fromtime],[Totime],[TimeDuration],[WFHAppliedOn],[WFHAppliedTo]) values ('" + strLAEmpid + "','" + strLAEmpName + "','" + strLALeaveFromDate + "','" + strLAReason + "','" + ViewState["Pertimefrom"] + "','" + ViewState["Pertotime"] + "','" + ViewState["tmper"] + "','" + strLA
Karthik_Mahalingam 17-Aug-17 5:04am
   
wat is this
Member 12605293 17-Aug-17 5:31am
   
Apply Button Code Bro
Karthik_Mahalingam 17-Aug-17 5:32am
   
very difficult to decode, pls post only relevant portion of the code.
Member 12605293 17-Aug-17 5:44am
   
string strLALeaveFromDate = txtFromDate.Text;
         string strLALeaveToDate = txtToDate.Text;
        string strLALeaveHalfDayFrom;
        string strMP = txtmisspunchfrom.Text;
        string submit_cc = null;
        string submitTo = null;
        string CCmailid = string.Empty;
        ///////////Code To Restrict Duplicate Entry/////////////////
        if (ddlLeaveType.SelectedItem.Text == "Casual Leave" || ddlLeaveType.SelectedItem.Text == "Earned Leave" || ddlLeaveType.SelectedItem.Text == "Sick Leave" || ddlLeaveType.SelectedItem.Text == "Comp Off" || ddlLeaveType.SelectedItem.Text == "Comp Off"
            || ddlLeaveType.SelectedItem.Text == "Wedding Leave" || ddlLeaveType.SelectedItem.Text == "Maternity Leave" || ddlLeaveType.SelectedItem.Text == "Bereavement Leave" || ddlLeaveType.SelectedItem.Text == "Paternity Leave" || ddlLeaveType.SelectedItem.Text == "Floating Leave")
        {
            string chkalreadyapplied = "( Select  LeaveFromDate,LeaveToDate, EmpID ,LeaveType,LeaveStatus from IntranetRelease.dbo.LeaveApplication Where  LeaveFromDate >= '" + strLALeaveFromDate + "'   and LeaveToDate  <= '" + strLALeaveToDate + "'  and EmpID  = 'SS525' )";
            ////  MessageBox.Show(strLAEmpid + strLAEmpName + strLAEmpOffMailID + strLALeaveType + strLALeaveFromDate + strLALeaveHalfDayFrom + strLALeaveToDate + strLALeaveHalfDayTo + douLANoOfDays + strLAReason + strLALeaveStatus + strLALeaveAppliedOn + strLeaveAppliedTo);
            scon = new SqlConnection(conString);
            scmd = new SqlCommand(chkalreadyapplied, scon);
            scon.Open();
            scmd.ExecuteNonQuery();
            scon.Close();

            if (txtFromDate.Text != strLALeaveFromDate)
            {
 }
        }
        else
        {
            MessageBox.Show("You Have Already Applied Leave on the Selected Date");
            btnApply.Visible = false;
        }
Karthik_Mahalingam 17-Aug-17 3:52am
   
status is different
Member 12605293 17-Aug-17 5:44am
   
This query will give the values which are Present
 Select DISTINCT LeaveFromDate,LeaveToDate, EmpID ,LeaveType,LeaveStatus from IntranetRelease.dbo.LeaveApplication Where  LeaveFromDate >='2017-06-30'  and LeaveToDate  <= '2017-06-30' 
Karthik_Mahalingam 17-Aug-17 5:46am
   
since the LeaveStatus is different, the distinct will not apply
Member 12605293 17-Aug-17 5:54am
   
Ok Karthik.But If an EmpId has an entry like 2017-17-08' and any Leave Status ,He is restricted to apply on same Date.
Karthik_Mahalingam 17-Aug-17 6:01am
   
not idea on how your page is designed or how u done the code.. its difficult to say the answer as comment.
post as a new question with detailed information, someone might reply for that.
Member 12605293 17-Aug-17 6:20am
   
Generally Speaking an EmpId has an entry like 2017-17-08' he s restricted to apply on same date again to avoid duplicate entry
Karthik_Mahalingam 17-Aug-17 6:50am
   
add an if condition to check whether there is any entry for that employee before adding a new record.
Member 12605293 17-Aug-17 10:53am
   
yes Done ,and it works bro :)
Karthik_Mahalingam 17-Aug-17 10:56am
   
Fine
Member 12605293 18-Aug-17 1:54am
   
Hi Karthik :)
Karthik_Mahalingam 18-Aug-17 3:38am
   
hi
Member 12605293 18-Aug-17 5:15am
   
DateTime d = DateTime.Now;
d = d.AddMonths(-1);
DateTime start = DateTime.Parse(d.Month + "/23/" + DateTime.Now.Year.ToString());
DateTime End = DateTime.Parse(System.DateTime.Now.Month.ToString() + "/25/" + DateTime.Now.Year.ToString());

string chkalreadyapplied = "(Select COUNT(*) as 'TotalPermisson' From IntranetRelease.dbo.LeaveApplication Where LeaveType='Permission' and LeaveStatus <> '' and LeaveFromDate >='" + start + "' and LeaveToDate <='" + End + "' and EmpID='" + strLAEmpid + "')";
SqlConnection scon = new SqlConnection(conString);
scon.Open();
SqlCommand percountcmd = new SqlCommand(chkalreadyapplied, scon);
TotalPermisson = (Int32)percountcmd.ExecuteScalar();
scon.Close();


How to pass the Start and End Date when applying on Future Date
Member 12605293 18-Aug-17 5:25am
   
When the LeaveFromDate and LeaveToDate is in this month,it works good..but not on future dates
Karthik_Mahalingam 18-Aug-17 5:45am
   
think where it is going wrong..
Karthik_Mahalingam 18-Aug-17 5:45am
   
take a pen and paper, draw flow chart and see, it will be so simple.
Member 12605293 18-Aug-17 5:50am
   
I have did That bro
Member 12605293 18-Aug-17 5:52am
   
Thats y asking help
Karthik_Mahalingam 18-Aug-17 6:06am
   
see i will not be there to help you all time.. then when wil you learn
Member 12605293 18-Aug-17 6:14am
   
ok Karthik
Karthik_Mahalingam 18-Aug-17 7:25am
   
start learning

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