Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am using LINQ. I get an error. Following code:
C#
HRM_SamparkDataContext Obj1 = new HRM_SamparkDataContext();
        try
        {
            string CompId = string.Empty;
            HRM_SamparkDataContext Obj=new HRM_SamparkDataContext();
            string Ip = Context.Request.ServerVariables["REMOTE_ADDR"];
            var Res = from Log in Obj.Sp_Check_Head_Hr_Login(Tbx_Head_Hr_ID.Text.Trim(), Tbx_Pass.Text.Trim()) select Log;
           

            if (Res.Count() > 0)
            {
               // Session["CompID"] = Tbx_Head_Hr_ID.Text.Trim();
                Res = from Log in Obj.Sp_Check_Head_Hr_Login(Tbx_Head_Hr_ID.Text.Trim(), Tbx_Pass.Text.Trim()) select Log;
              <big>  CompId = Res.ElementAt(2).CompID;</big>
                Obj1.Sp_Insert_HeadHr_Login_Log(Tbx_Head_Hr_ID.Text.Trim(), CompId, Tbx_Pass.Text.Trim(), "Success", Ip);

              
            }
            else
            {
                Obj1.Sp_Insert_HeadHr_Login_Log(Tbx_Head_Hr_ID.Text.Trim(),CompId, Tbx_Pass.Text.Trim(), "Fail", Ip);
                this.ClientScript.RegisterStartupScript(this.GetType(), "Welcome at HRM Intranet", "<script language=\"javaScript\">" + "alert('Authentication Fail Try Again !');" + "<" + "/script>");
            }


        }
        catch (Exception Ex)
        {

        }


The above code is giving Exception:
[System.ArgumentOutOfRangeException] = {"Specified argument was out of the range of valid values.\r\nParameter name: index"} from where the exception is thrown is shown in bold

Can any one tell me the solution?
Posted
Updated 12-Jul-10 20:09pm
v2

Check the Res variable, it seems it has less than 3 values, thats why its giving you this exception since you are indexing an element of index 2.
 
Share this answer
 
You are not checking null for that operation. Check if Res.ElementAt(2) exists or not.

Try this,
MIDL
CompId = Res.ElementAt(2) ?? 0;

if(CompId !=0)
{
  Obj1.Sp_Insert_HeadHr_Login_Log(Tbx_Head_Hr_ID.Text.Trim(), CompId,   Tbx_Pass.Text.Trim(), "Success", Ip);
}

This is not an accurate snippet. But you can test and modify accordingly.
 
Share this answer
 

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