2016-05-07 13 views
0

mein Projekt ist über Online-Eignungstestsystem. Wenn ich die Startprüfungsseite ausführe, werden die Fragen angezeigt, aber die Antwort wird nicht übermittelt. , wenn ich auf die Schaltfläche zum Senden klicken Ich erhalte Fehler auf if(ds1.Tables[0].Rows.Count>0) und wenn ich klicken Sie auf ‚previous'button Fehler immer auf if(i==ds.Tables[0].Rows.Count-1 || i !=0)Fehler bekommen: - Objektreferenz nicht auf eine Instanz eines Objekts gesetzt

ERROR: - object reference not set to an instance of an object

ich herausfinden kann, wo ich Fehler mache. Schau dir den Code an, wo ich Fehler mache und was die Lösung ist.

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

Ich hatte durch diesen Link gegangen, aber nicht die Lösung meines Problems bekommen –

Antwort

0

Sie nicht eine Eigenschaft eines Objekts, wenn das Objekt null ist zugreifen können. Zum Beispiel:

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

Dies ist, was in Ihrem Code geschieht, außer mit einem DataTable. Ich vermute, dass Ihre Abfrage keine Ergebnisse so, wenn Sie tun:

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

... die Rows Eigenschaft null ist. Sie können nicht auf Count auf Null zugreifen.

Sie müssen fleißiger null Prüfung tun:

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

Andere Gedenktage:

  1. Als Henk erwähnt, wird die Timer nicht so funktionieren, Sie denken. Dies ist keine Desktop-App, es ist eine zustandslose Client/Server-App. Sobald die Antwort an den Client gesendet wurde, haben Sie die Möglichkeit verloren, auf diese Weise mit der Sitzung zu interagieren.

  2. Der richtige Weg zur Umleitung ist:

    Response.Redirect(url, false); 
    Context.ApplicationInstance.CompleteRequest(); 
    
  3. Sie müssen Ihre Fragen parametrieren gegen SQL-Injection zu schützen und Syntaxfehler zu vermeiden. Ich werde diese Antwort nicht mehr mit einem Beispiel überladen, ADO.Net Parameter ist ein sehr Google-fähiges Thema.
  4. Datenbindung Ihre Radio Button-Liste wäre vorteilhaft gegenüber dem Schleifen und Hinzufügen.
Verwandte Themen