Click here to Skip to main content
13,198,727 members (41,930 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi My application is for Leave module. When it will count days & trying to submit the leave it is not able to save record. It will give exception:
Message = "There is no row at position 0."

And my IsValidLeave function part is:
 public bool IsValidLeave(string leaveTypeID)
    {
        try
        {
            if (drdwnLeaveType.SelectedIndex == 0)
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "key1", "alert('Please select Leave Type');", true);
                DateOfJoin();
                ApplyLeave();
                return false;
            }
 
            if (countdays())
            {
                double NOD = Convert.ToDouble(ViewState["NoOfDays"].ToString());
                if (chkHalfLeave.Checked)
                {
                    NOD = NOD * 2;
                }
                bool go = false;
                if (leaveTypeID == "2" && NOD > 3)
                {
                    if (ViewState["Medical"].ToString() == "")
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "AlertCode", "alert('Medical Certificate is required to avail Sick Leave more than 3 Days')", true);
                        return false;
                    }
                    else
                    {
                        go = true;
                    }
                }
                else
                {
                    go = true;
                }
                if (go == true)
                {
 
                    dt.OpenCon();
 
                    string qry = "select MA_LeaveTypeName from master_LeaveType where MA_LeaveType= '" + leaveTypeID + "'";
 
                    string LeaveType = dt.ExecuteScalar(qry);
 
                    string[] LeaveTypeName = LeaveType.Split(' ');
 
                    dt.Dispose();
 
                    if (ShowYear.Visible == true) /*Check Leave Balance From Current Year*/
                    {
                        string CrYr = DateTime.Now.Year.ToString();
                        ArrayList arrchkleave = LeaveManagement.GetAppliedLeaveBalance(leaveTypeID, Session["U_Emp_Code"].ToString(), CrYr);
                        leavebal = Convert.ToDouble(arrchkleave[0]);
                    }
                    else if (ShowYear.Visible == false) /*Check Leave Balance From Previous Year*/
                    {
                        int CrYr = DateTime.Now.Year;
                        string PVYr = Convert.ToString(CrYr - 1);
                        ArrayList arrchkleave = LeaveManagement.GetAppliedLeaveBalance(leaveTypeID, Session["U_Emp_Code"].ToString(), PVYr);
                        leavebal = Convert.ToDouble(arrchkleave[0]);
                    }
 
                    if (leavebal > 0)
                    {
 
                        double bal = leavebal - Convert.ToDouble(ViewState["NoOfDays"].ToString());
 
                        if (bal < 0 || bal == 0)//(bal > 0 || bal == 0//Original old) again replace from new to old(bal < 0 || bal >= 0)//changes made by Seema bal < 0 instead of bal > 0
                        {
 
                            return true;
 
                        }
 
                        else
                        {
 
                           //ScriptManager.RegisterStartupScript(this, GetType(), "AlertCode", "alert('You have only " + leavebal + " " + LeaveTypeName[0] + " Leaves remaining.')", true);
                            ScriptManager.RegisterStartupScript(this, GetType(), "AlertCode", "alert('You have Cross The Limits')", true);
                            return false;
 
                        }
 
                    }
 
                    else if (leavebal == 0 || leavebal < 0)
                    {
 
                        return true;
 
                    }
 
                    else
                    {
 
                        ScriptManager.RegisterStartupScript(this, GetType(), "AlertCode", "alert('You have availed all " + LeaveTypeName[0] + " Leaves.')", true);
 
                        return false;
 
                    }
 
                }
 
                else
                {
                    return false;
                }
            }
 
            else
            {
                DateOfJoin();
                ApplyLeave();
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }


so how can i remove this error.
Posted 25-May-15 19:09pm
Comments
Abhipal Singh 26-May-15 1:34am
   
What does this entire code do? On which line you are getting this error?
Member 11221185 26-May-15 1:52am
   
This code help us for validate the condition for save the leave record. Error comes on bold area: If(ShowYear.Visible == true)
{
string CrYr = DateTime.Now.Year.ToString();
ArrayList arrchkleave = LeaveManagement.GetAppliedLeaveBalance(leaveTypeID, Session["U_Emp_Code"].ToString(), CrYr);
leavebal = Convert.ToDouble(arrchkleave[0]);
}
Debugger cant go front to Arraylist arrchkleave line.
Abhipal Singh 26-May-15 1:56am
   
Did you tried to debug GetAppliedLeaveBalance() in LeaveManagement class?
Member 11221185 26-May-15 2:08am
   
Yes sir i tried. Actually the same code is working in another condition like if employee completed Six month or one year that time it is working properly but it is not working in LWP(Leave Without Pay)/employee joining date is < 6 month. When i used debug then it stop to this line ArrayList arrchkleave = LeaveManagement.GetAppliedLeaveBalance(leaveTypeID, Session["U_Emp_Code"].ToString(), CrYr); and give the exception.
Abhipal Singh 26-May-15 2:15am
   
You need to debug the GetAppliedLeaveBalance() because you are passing different parameters this time in case of LWP.

Hint: Use immidiate window to check the parameter values you are passing to this method.
Sinisa Hajnal 26-May-15 2:33am
   
I would think the error explains it. You're accessing rows somewhere where there are no rows. Before accessing datatables, arrays and other lists, always check for existence of said rows.
Sinisa Hajnal 26-May-15 2:35am
   
You don't have any rows with that particular condition and you're running your math without checking if you get back something. It works when your database returns one or more rows.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Please check whether the function GetAppliedLeaveBalance() returns any data back for LWP as suggested in the comments.

Also it is a better practice to check arrchkleave is not null and has some elements before using any value from it.

The below code makes sure arrchkleave has at-least one element and avoids the exception you are facing.

if(arrchkleave != null && arrchkleave.Count > 0)
{
  leavebal = Convert.ToDouble(arrchkleave[0]);
}
  Permalink  
Comments
Member 11221185 26-May-15 3:30am
   
Sorry it also not working..when i tried debug to GetAppliedLeaveBalance() this class it show same exception. Query is that:
public static ArrayList GetAppliedLeaveBalance(string leaveTypeID, string App_Code, string Year)
{
string qry = "Select APL_LeaveBalance,SL_LeaveBalance from Applicant_LeaveBalance where App_Code='" + App_Code + "'and Year(ModifiedDate)=Year('" + Year + "')";
DataTable dtLeaveBal = Adapter.ExecuteDataset(qry).Tables[0];
double BAPL = Convert.ToDouble(dtLeaveBal.Rows[0]["APL_LeaveBalance"].ToString());
double BSL = Convert.ToDouble(dtLeaveBal.Rows[0]["SL_LeaveBalance"].ToString());
But employee dont have any leave bal thats y we are applying for LWP.
Member 11221185 26-May-15 7:49am
   
I solved this..Thanks for all.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171020.1 | Last Updated 26 May 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100