C#.Net ASP.Net 数据控件

发布于 2024-09-15 18:55:13 字数 5945 浏览 3 评论 0原文

使用此代码:我试图用数据填充页面,并且我想在显示数据时编辑数据,但它说找不到表“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

Using This code: I am tring to fill the page with the data and I would like to edit the data when it is displayed, but it says could not find table '0'.

Dataset returns null value.

Could any one help me pls.

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);
        }
    }

}:

Stack trace:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-09-22 18:55:13

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

ds = ServiceProvider.GetQuestionView(i);

GetQuestionView 不返回任何内容。您将需要进入该方法以确定为什么没有发回数据。

以下是我推荐的一些建议。

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

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

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

I believe your problem is in the following line.

ds = ServiceProvider.GetQuestionView(i);

The GetQuestionView is not returning anything. You will need to step into that method to determine why no data is sent back.

Here are a few pointers I would recommend.

1) Validate the questionId from the QueryString to ensure it is not null and that it contains a valid integer.

2) Check to see if tables exists in the DataSet first before checking for rows in a DataTable.

if (ds.Tables.Count > 0)
{
   ...
}
做个ˇ局外人 2024-09-22 18:55:13

由于当您尝试保存时发生异常,因此该行似乎

ServiceProvider.EditQuestion(...

没有返回其数据集中的任何表。

As the exception happens when you try to save, it looks like this line

ServiceProvider.EditQuestion(...

isn't returning any tables in it's DataSet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文