Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
i got error on array out of bound but i count the rows and columns but still having some problem...I am sending my code please give me some solving suggestion...

XML
<div>
    <table border="0" cellpadding="0"  cellspacing="0"  width="100%">
        <tr>
            <td align="left" valign="top">
                <asp:label ID="lblName" Visible="false"  CssClass="lblBoldback" runat="server"></asp:label>
                &nbsp;&nbsp;&nbsp;
                <asp:label ID="lblRole" Visible="false" CssClass="lblBoldback" runat="server"></asp:label>
            </td>
        </tr>
        <tr>
            <td align="left" valign="top" >
                <asp:label ID="lblNoRecordFound" Visible="false" runat="server"></asp:label>
            </td>
        </tr>
        <tr>
            <td >
                 <asp:GridView ID="grdAdminInitiate" Visible="false"  BorderColor="AliceBlue" runat="server" CellPadding="4" Font-Names="verdana" Font-Size="Small"   ForeColor="#333333" GridLines="Horizontal"
                         Width="100%"  AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="grdAdminInitiate_PageIndexChanging">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#f2f9ff" ForeColor="#333333" />
                        <EditRowStyle BackColor="#999999" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#6699cc" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <Columns>
                            <asp:BoundField DataField="emp_code" ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="Emp Code"/>
                            <asp:BoundField DataField="emp_name" ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="Name"/>
                            <asp:BoundField DataField="emp_designation" ItemStyle-HorizontalAlign ="Left"  Visible="true" HeaderStyle-HorizontalAlign="Left"  HeaderText="Designation"/>
                            <asp:BoundField DataField="grade" ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="Grade"/>
                            <asp:BoundField DataField="mail_sendto" ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="eMail To" />
                            <asp:BoundField DataField="status" ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="Status"/>
                            <asp:BoundField DataField="appraiser_empcode" Visible="false"  ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="Appraiser Emp Code" />
                            <asp:BoundField DataField="appraiser_name"   Visible="false" ItemStyle-HorizontalAlign ="Left" HeaderStyle-HorizontalAlign="Left"  HeaderText="Appraiser Name" />
                            <asp:TemplateField HeaderText="Initiate" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign ="Left">

                                <ItemTemplate>

                                        <asp:CheckBox ID="chkInitiate" AutoPostBack="true"  runat="server" />

                                 </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Freeze" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign ="Left">
                                                <ItemTemplate>
                                                    <asp:CheckBox ID="chkFreeze" runat="server" />
                                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <PagerSettings PreviousPageText="Previous"  NextPageText="Next" FirstPageText="First" LastPageText="Last"/>
                 </asp:GridView>
            </td>
        </tr>
        <tr>
            <td align="right" valign="top">
                <asp:Button ID="btnInitiate" Visible="false"  runat="server" Text="Apply"  onclick="btnInitiate_Click"
                 BackColor="#47b5b5" BorderColor="#47b5b5" BorderStyle="None" ForeColor="White" Width="64px"/>
            </td>
        </tr>
    </table>
    </div>




C#
/////////////////////////////////////////
protected void Page_Load(object sender, EventArgs e)
   {
       HtmlLink cssLink = new HtmlLink();
       cssLink.Href = "MedicalApp.css";
       cssLink.Attributes.Add("rel", "stylesheet");
       cssLink.Attributes.Add("type", "text/css");
       Header.Controls.Add(cssLink);

       string sCompany  = "";
       string sLocation = "";
       string sEmpCode  = "";
       grdAdminInitiate.RowDataBound += new GridViewRowEventHandler(grdAdminInitiate_RowDataBound);
       Library lib = new Library();
       DataTable dtAdminInitiate = null;

       if (!IsPostBack)
       {
           if (Request.QueryString["comcode"] != null && Request.QueryString["comcode"].ToString() != "")
           {

               if (Request.QueryString["empcode"] != null && Request.QueryString["empcode"].ToString() != "")
               {
                   if (Request.QueryString["comcode"].ToString() == "HDP")
                   {
                       int iFlag = lib.SetCompanyCode("mehtascl", "rnv", "4", 1);
                       sCompany = "HDP";
                       sLocation = "RNF";
                   }


                   sEmpCode = Request.QueryString["empcode"].ToString();
                   DataTable dtMedicalDetail = lib.getMedicalUserDetail(sCompany, sLocation, sEmpCode);

                   if (dtMedicalDetail != null)
                   {
                       if (dtMedicalDetail.Rows.Count > 0)
                       {
                           if (dtMedicalDetail.Rows[0].ItemArray[18] != null && dtMedicalDetail.Rows[0].ItemArray[18].ToString() != "")
                           {
                               string sAppRole = Convert.ToString(dtMedicalDetail.Rows[0].ItemArray[18]);
                               if (sAppRole.CompareTo("Admin") == 1)
                               {
                                   string sEmpShortNameAdmin = "";
                                   DataTable dtName = lib.getShortUserName(sCompany, Convert.ToString(sEmpCode));
                                   if (dtName.Rows.Count > 0)
                                   {
                                       string fname = Convert.ToString(dtName.Rows[0].ItemArray[0]);
                                       string mname = Convert.ToString(dtName.Rows[0].ItemArray[1]);
                                       string lname = Convert.ToString(dtName.Rows[0].ItemArray[2]);

                                       sEmpShortNameAdmin = fname + " " + mname + " " + lname;
                                   }
                                   lblName.Visible = true;

                                   lblName.Text = "Welcome : " + sEmpShortNameAdmin +" ,";
                                   lblRole.Visible = true;
                                   lblRole.Text = "Role : " + sAppRole.Trim();
                                   grdAdminInitiate.Visible = true;
                                   btnInitiate.Visible = true;
                                   dtAdminInitiate = lib.AdminInitiateApplication(sCompany, sLocation);
                               }
                               else
                               {
                                   lblName.Visible = false;
                                   lblRole.Visible = false;
                                   grdAdminInitiate.Visible = false;
                                   btnInitiate.Visible = false;
                               }
                           }
                       }
                   }


               }
           }


           if (dtAdminInitiate != null)
           {
               if (dtAdminInitiate.Rows.Count > 0)
               {
                   grdAdminInitiate.DataSource = dtAdminInitiate;
                   grdAdminInitiate.DataBind();

               }
               else
               {
                   lblName.Visible = false;
                   lblRole.Visible = false;
                   grdAdminInitiate.Visible = false;
                   btnInitiate.Visible = false;
                   lblNoRecordFound.CssClass = "Fail";
                   lblNoRecordFound.Text = "No record found";
               }
           }
       }
}


PLEASE HELP ME OUT....
Posted
Updated 4-May-10 21:30pm
v2

This is a big code, can you please tell us exactly where and when you are getting error. :sigh:
 
Share this answer
 
Please mention the line of code where you get this error.
 
Share this answer
 
avijit1112010 wrote:
string mname = Convert.ToString(dtName.Rows[0].ItemArray[1]);
string lname = Convert.ToString(dtName.Rows[0].ItemArray[2]);

This looks like a possible place for the exception.

To code based on ItemArray indexed with numbers are not good, keeping that aside.. whenever you are using it, make sure you have that right and is not null.

Check for null before accessing the ItemArray's data.
 
Share this answer
 
    • Usually error messages are pretty informative, for instance, they show the offending line in the source code.
    • Have you ever heard about the debugger? It is a great tool...

:)
 
Share this answer
 
v2

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