tags:

views:

97

answers:

2

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

+1  A: 

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.

DavidGouge
Thanks Mate.. Actually its not returning any data for the dataset..Many thanks
Hari Gillala
+2  A: 

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)
{
   ...
}
dretzlaff17