2016-05-07 11 views
0

私のプロジェクトはオンラインの適性テストシステムです。私は試験の開始ページを実行しているときに質問が表示されていますが、回答は提出されません。 object reference not set to an instance of an objectエラーを取得しています: - オブジェクト参照がオブジェクトのインスタンスに設定されていません

私は間違いを犯していた場所を把握することができます - :私はif(i==ds.Tables[0].Rows.Count-1 || i !=0)

ERRORの「previous'button取得中にエラーをクリックしたときに私がif(ds1.Tables[0].Rows.Count>0)上のエラーを取得しています送信ボタンをクリックしてください 。コードを見て、どこで私が間違っているのか、解決策を教えてください。

.aspx.cs: -

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 

using System.Collections; 
using System.Configuration; 
using System.Web.Security; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 

public partial class StartExam : System.Web.UI.Page 
{ 
int totalsec = 0, sec = 0, min = 0; 
String time = ""; 
static int j = 0; 
static int i,total; 
SqlConnection con; 
static SqlDataAdapter da,da1; 
static DataSet ds,ds1; 
SqlCommand cmd; 
String date; 
String str,str1; 

protected void Page_Load(object sender, EventArgs e) 
{ 

    date = DateTime.Now.ToShortDateString(); 
    con = new SqlConnection("Data Source=.;Initial Catalog=OnlineExam;Integrated Security=True"); 
    con.Open(); 

    if (!Page.IsPostBack) 
    { 
     Session["time"] = 900; 
     i = 0; 
     total = 0; 

     string str = "Select top 15 * from Question_Master where Que_Topic='" + Session["Topic"].ToString() + "' order by newid()"; 

     SqlDataAdapter da = new SqlDataAdapter(str, con); 
     DataSet ds = new DataSet(); 

     da.Fill(ds, "Question_Master"); 

     if (ds.Tables[0].Rows.Count > 0) 
     { 
      DataRow dr; 
      while (i < ds.Tables[0].Rows.Count) 
      { 
       dr = ds.Tables[0].Rows[i]; 
       // Session["Answer"] = Convert.ToString(Convert.ToInt32(dr["Que_Answer"].ToString()) - 1); 

       QueLabel.Text = dr["Que_Question"].ToString(); 
       OptionRadioButtonList.ClearSelection(); 
       OptionRadioButtonList.Items.Clear(); 
         OptionRadioButtonList.Items.Add(dr["Que_Option1"].ToString()); 
       OptionRadioButtonList.Items.Add(dr["Que_Option2"].ToString()); 
       OptionRadioButtonList.Items.Add(dr["Que_Option3"].ToString()); 
        OptionRadioButtonList.Items.Add(dr["Que_Option4"].ToString()); 
       i++; 



       //cmd = new SqlCommand("Insert into Temp_Result(TR_Question,TR_Option1,TR_Option2,TR_Option3,TR_Option4,TR_Answer,TR_Id) values('" + ds.Tables[0].Rows[j][2].ToString().Trim() + "','" + ds.Tables[0].Rows[j][3].ToString().Trim() + "','" + ds.Tables[0].Rows[j][4].ToString().Trim() + "','" + ds.Tables[0].Rows[j][5].ToString().Trim() + "','" + ds.Tables[0].Rows[j][6].ToString().Trim() + "','" + ds.Tables[0].Rows[j][7].ToString().Trim() + "'," + Convert.ToInt32(Session["Id"].ToString()) + ")", con); 
       //cmd.ExecuteNonQuery(); 





       // String str1 = "Select top 15 * from Temp_Result where TR_Id=" + Session["Id"].ToString(); 
       // SqlDataAdapter da1 = new SqlDataAdapter(str, con); 
       // DataSet ds1 = new DataSet(); 

       // da1.Fill(ds1, "Temp_Result"); 

       // if (ds1.Tables[0].Rows.Count > 0) 
       // { 
       // DataRow dr1; 
       // while (i < ds.Tables[0].Rows.Count) 
       // { 
       //dr1 = ds1.Tables[0].Rows[i]; 


        con.Close(); 

       // } 

       // } 
      } 
     } 
    } 
} 






protected void EndExamButton_Click(object sender, EventArgs e) 
{ 
    Response.Redirect("~\\Result.aspx"); 
} 

protected void Timer1_Tick(object sender, EventArgs e) 
{ 
    Session["time"] = Convert.ToInt16(Session["time"]) - 1; 

    if (Convert.ToInt16(Session["time"].ToString()) <= 0) 
    { 
     Response.Redirect("~\\Result.aspx"); 
    } 
    else 
    { 
     totalsec = Convert.ToInt16(Session["time"]); 
     sec = totalsec % 60; 
     min = totalsec/60; 
     time = min + ":" + sec; 
     TimerLabel.Text = time; 
    } 
} 
protected void SESubmitButton_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     if (OptionRadioButtonList.SelectedItem.Text == ds1.Tables[0].Rows[j][6].ToString()) 
     { 
      total++; 

     } 
     else 
     { 

      total += 0; 

     } 
    cmd = new SqlCommand("update Temp_Result set TR_AttemptedAns='" + OptionRadioButtonList.SelectedItem.Text + "' where TR_Question='" + QueLabel.Text + "' and TR_Id=" + Convert.ToInt32(Session["Id"].ToString()) + "", con); 
     cmd.ExecuteNonQuery(); 
    } 
    catch (Exception ex) 
    { } 
    finally 
    { 
     if (i < ds1.Tables[0].Rows.Count - 1) 
     { 
      i++; 
      OptionRadioButtonList.SelectedIndex = -1; 
     } 
     else 
     { 
      Response.Redirect("~\\Result.aspx?total=" + total); 
     } 
    } 

    } 

protected void SENextButton_Click(object sender, EventArgs e) 
{ 
    //int i=0; 
    //if (i < ds.Tables[0].Rows.Count - 1) 
    // { 
     Response.Redirect(Request.RawUrl); 
     // i++; 
     /* QueLabel.Text = ds.Tables[0].Rows[i]["Que_Question"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option1"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option2"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option3"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option4"].ToString(); */   

    // } 


} 

protected void SEPreviousButton_Click(object sender, EventArgs e) 
{ 

    if (i == ds.Tables[0].Rows.Count - 1 || i != 0) 
    { 
     i--; 

     /*QueLabel.Text = ds.Tables[0].Rows[i]["Que_Question"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option1"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option2"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option3"].ToString(); 
     OptionRadioButtonList.Text = ds.Tables[0].Rows[i]["Que_Option4"].ToString();*/ 
    } 

} 

protected void OptionRadioButtonList_SelectedIndexChanged(object sender, EventArgs e) 
{ 

    if (ds1.Tables[0].Rows.Count > 0) 
    { 
     OptionRadioButtonList.Items[2].Enabled = false; 
     OptionRadioButtonList.Items[3].Enabled = false; 
    } 
    else 
    { 
     OptionRadioButtonList.Items[2].Enabled = true; 

     OptionRadioButtonList.Items[3].Enabled = true; 
    } 


     if (OptionRadioButtonList.SelectedItem.Text == "") 
      SESubmitButton.Enabled = true; 
     else 
      SESubmitButton.Enabled = false; 

    } 

protected void EndExamButton_Click1(object sender, EventArgs e) 
{ 
    Response.Redirect("~\\Result.aspx?total=" + total); 
} 


} 
+0

私はそのリンクを通過しましたが、私の問題の解決策を得ていませんでした –

答えて

0

オブジェクトがnullの場合は、オブジェクトのプロパティにアクセスすることはできません。例えば:

string Foo = null; 
Console.WriteLine(Foo.Length); // null reference exception! 

これはDataTableを除いて、あなたのコード内で何が起こっているかです。私はあなたが行うとき、クエリは非常に結果を返しません疑う:

if (ds.Tables[0].Rows.Count > 0) 

... Rowsプロパティはnullです。 nullの場合Countにアクセスすることはできません。

if(ds.Tables != null) 
{ 
    if(ds.Tables[0].Rows != null) 
    { 
     //now you can be sure you can access `Count` 
    { 
} 

その他の慣例:

あなたはより多くの勤勉ヌルチェックを行う必要がありヘンクが言及したよう

  1. を、Timerは、あなたが考える方法を動作しません。これはデスクトップアプリケーションではなく、ステートレスなクライアント/サーバーアプリです。応答がクライアントに送信されると、セッションと対話する能力が失われてしまいます。リダイレクトする

  2. 適切な方法は次のとおりです。

    Response.Redirect(url, false); 
    Context.ApplicationInstance.CompleteRequest(); 
    
  3. あなたはSQLインジェクションから保護するために、構文エラーを防ぐために、あなたのクエリをパラメータ化する必要があります。私はもうこの例をあきらかにするつもりはありませんが、ADO.Netパラメータは非常にGoogleが可能なトピックです。
  4. ラジオボタンリストをバインドするデータは、ループ処理や追加処理に比べて優れています。
関連する問題