2013-03-13 13 views
-1

我創建一個網站,但只要我運行此代碼,將引發一個異常:爲了解決在創建網站的異常細節

#Exception Details: System.IndexOutOfRangeException: There is no row at position 0. 

我該如何解決這個問題?

public partial class Employee_frmSearchClaims : System.Web.UI.Page 
{ 
    int empno; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["empno"] != null) 
      empno = int.Parse(Session["empno"].ToString()); 

     if (!IsPostBack) 
     { 
     DataRow r = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno).Tables[0].Rows[0]; 
       Label7.Text = r["policyname"].ToString(); 
       Label8.Text = r["policyid"].ToString(); 
       Label9.Text = r["policyamount"].ToString(); 
       Label10.Text = r["TotalAmount"].ToString(); 
       Label11.Text = r["pstartdate"].ToString(); 
      } 
     } 

    protected void btnapplicy_Click(object sender, EventArgs e) 
    { 
     if (ddlReason.SelectedItem.ToString() != "--Select--") 
     { 
      if (ddlReason.SelectedItem.Text == "Death") 
      { 
       Session["Reason"] = ddlReason.SelectedItem.Text; 
       Response.Redirect("frmDeathCliam.aspx"); 
      } 
      else if (ddlReason.SelectedItem.Text == "Accident") 
      { 
       Session["Reason"] = ddlReason.SelectedItem.Text; 
       Response.Redirect("frmAccidentClaim.aspx"); 
      } 
      else 
      { 
       Session["Reason"] = ddlReason.SelectedItem.Text; 
       Response.Redirect("frmCompleteClaim.aspx"); 
      } 

     } 
     else 
      lblmsg.Text = "You have to select the reason"; 
    } 
} 
+0

你能告訴我們在這行發生錯誤? – 2013-03-13 16:54:46

+0

我認爲它是這個地方:第22行:if(!IsPostBack) 23行:DataRow r = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno).Tables [0] .Rows [0]; 第25行:Label7.Text = r [「policyname」]。ToString(); 第26行:Label8.Text = r [「policyid」]。ToString(); – 2013-03-13 16:54:58

+0

我剛剛將其重新命名爲C#,但我不確定這是否正確。如果不是,請重新標記。 – Hasturkun 2013-03-13 16:58:50

回答

0

你需要確保你的表中包含行第一:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Session["empno"] != null) 
     empno = int.Parse(Session["empno"].ToString()); 

    if (!IsPostBack) 
    { 
     var ds = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      DataRow r = ds.Tables[0].Rows[0]; 
      Label7.Text = r["policyname"].ToString(); 
      Label8.Text = r["policyid"].ToString(); 
      Label9.Text = r["policyamount"].ToString(); 
      Label10.Text = r["TotalAmount"].ToString(); 
      Label11.Text = r["pstartdate"].ToString(); 
     } 
    } 
} 
+0

**我真的很感謝你的貢獻。那部分現在工作得很好,謝謝** – 2013-03-15 10:11:29