繁体   English   中英

C#.Net ASP.Net数据控件

[英]C#.Net ASP.Net Data Controls

使用此代码:我想用数据填充页面,并且我想在显示数据时对其进行编辑,但是它说找不到表'0'。

数据集返回空值。

谁能帮助我。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillLists();
                int i = Convert.ToInt32(Request.QueryString["QID"]);
                // Response.Write(i);
                eTutorService ServiceProvider = new eTutorService();
                DataSet ds = new DataSet();
                ds = ServiceProvider.GetQuestionView(i);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    txtQuestion.Text = ds.Tables[0].Rows[0]["Question"].ToString();
                    txtOption1.Text = ds.Tables[0].Rows[0]["Option1"].ToString();
                    txtOption2.Text = ds.Tables[0].Rows[0]["Option2"].ToString();
                    txtOption3.Text = ds.Tables[0].Rows[0]["Option3"].ToString();
                    txtOption4.Text = ds.Tables[0].Rows[0]["Option4"].ToString();
                    txtCorrectAnswer.Text = ds.Tables[0].Rows[0]["CorrectAnswer"].ToString();
                    //Response.Write("ds.Tables[0].Rows[0][QuizId].ToString()" + ds.Tables[0].Rows[0]["QuizId"].ToString() +"</br>");
                    //Response.Write("ds.Tables[0].Rows[0][DifficultyLevel].ToString()" + ds.Tables[0].Rows[0]["DifficultyLevel"].ToString() + "</br>");
                    //Response.Write("ds.Tables[0].Rows[0][QuestionOrder].ToString()" + ds.Tables[0].Rows[0]["QuestionOrder"].ToString() + "</br>");
                    ddlPaper.Items.FindByValue(ds.Tables[0].Rows[0]["QuizId"].ToString()).Selected = true;
                    ddlDifficultyLevel.Items.FindByValue(ds.Tables[0].Rows[0]["DifficultyLevel"].ToString()).Selected = true;
                    ddlQuestionOrder.Items.FindByValue(ds.Tables[0].Rows[0]["QuestionOrder"].ToString()).Selected = true;
                }
            }
        }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int QuestionId =0;
        if (Request.QueryString["QID"] != null)
        {
            QuestionId= Convert.ToInt32(Request.QueryString["QID"]);
        }
        string Question=txtQuestion.Text  ; 
        int DifficultyLevel=Convert.ToInt32 (ddlDifficultyLevel.SelectedItem.Value);
        int QuestionOrder = Convert.ToInt32(ddlQuestionOrder.SelectedItem.Value);
        int QuizId = Convert.ToInt32(ddlPaper.SelectedItem.Value);
        string CorrectAnswer=txtCorrectAnswer.Text;
        string Option1=txtOption1.Text;
        string Option2 = txtOption2.Text;
        string  Option3=txtOption3.Text ;
        string Option4=txtOption4.Text;
        eTutorService ServiceProvider = new eTutorService();
        DataSet ds = new DataSet();
        ds=ServiceProvider.EditQuestion(QuestionId, Question, DifficultyLevel, QuestionOrder,
            QuizId, CorrectAnswer, Option1, Option2, Option3, Option4);
        if (ds.Tables[0].Rows[0][0] != null)
        {
            pnlMain.Visible = false;
            pnlConfirm.Visible = true;
        }


    }

    protected void FillLists()
    {
        eTutorService ServiceProvider = new eTutorService();
        DataTable dtDiffLevelsdtDiffLevels = ServiceProvider.GetDiffLevels().Tables[0];
        DataTable dtQuizPapers = ServiceProvider.GetQuizPapers().Tables[0];

        ddlPaper.DataSource = dtQuizPapers;
        ddlPaper.DataValueField = dtQuizPapers.Columns["QuizID"].ToString();
        ddlPaper.DataTextField = dtQuizPapers.Columns["Subject"].ToString();
        ddlPaper.DataBind();

        ddlDifficultyLevel.DataSource = dtDiffLevelsdtDiffLevels;
        ddlDifficultyLevel.DataValueField = dtDiffLevelsdtDiffLevels.Columns["LEVEL_ID"].ToString();
        ddlDifficultyLevel.DataTextField = dtDiffLevelsdtDiffLevels.Columns["LEVEL_NAME"].ToString();
        ddlDifficultyLevel.DataBind();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuestionList.aspx", false);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["QID"] != null)
        {
            Response.Redirect("QuestionView.aspx?QID=" + Request.QueryString["QID"].ToString(), false);
        }
    }

}:

堆栈跟踪:

Cannot find table 0.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Cannot find table 0.

Source Error:

Line 60:             ds=ServiceProvider.EditQuestion(QuestionId, Question, DifficultyLevel, QuestionOrder,
Line 61:                 QuizId, CorrectAnswer, Option1, Option2, Option3, Option4);
Line 62:             if (ds.Tables[0].Rows[0][0] != null)
Line 63:             {
Line 64:                 pnlMain.Visible = false;


Source File: C:\Users\Star\Desktop\ETutor-Aug11\ETutor-Aug11\ETutor\EtutorServiceWebApp\QuestionEdit.aspx.cs    Line: 62

Stack Trace:

[IndexOutOfRangeException: Cannot find table 0.]
   System.Data.DataTableCollection.get_Item(Int32 index) +92
   EtutorServiceWebApp.QuestionEdit.btnSave_Click(Object sender, EventArgs e) in C:\Users\Star\Desktop\ETutor-Aug11\ETutor-Aug11\ETutor\EtutorServiceWebApp\QuestionEdit.aspx.cs:62
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1

我相信您的问题出在以下几行。

ds = ServiceProvider.GetQuestionView(i);

GetQuestionView不返回任何东西。 您将需要进入该方法以确定为什么不发送回数据。

这里有一些我会建议的建议。

1)验证QueryString中的questionId,以确保它不为空并且包含有效的整数。

2)在检查DataTable中的行之前,请先检查表是否存在于DataSet中。

if (ds.Tables.Count > 0)
{
   ...
}

当您尝试保存时发生了异常,它看起来像这行

ServiceProvider.EditQuestion(...

没有在其DataSet中返回任何表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM