-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";
    }
}
4

1 回答 1

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();
        }
    }
}
于 2013-03-13T17:03:25.517 回答