如何避免对象引用的错误未设置为对象的实例

时间:2013-08-06 06:55:45

标签: asp.net

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim score As Integer

    If RadioButtonList1.SelectedValue = "Be careful indexing attributes that have null values." Then
        score = score + 1
        Session("correct") = "Be careful indexing attributes that have null values."
        Session("question") = "1.A rule of thumb for choosing indexes for a relational database includes which of the following?"
        Session("yourans") = "Be careful indexing attributes that have null values."
        Session("score1") = 1
    ElseIf RadioButtonList1.SelectedValue = "Indexes are more useful on smaller tables." Then
        score = score + 0
        Session("correct") = "Be careful indexing attributes that have null values."
        Session("question") = "1.A rule of thumb for choosing indexes for a relational database includes which of the following?"
        Session("yourans") = "Indexes are more useful on smaller tables."
        Session("score1") = 0
    ElseIf RadioButtonList1.SelectedValue = "Indexes are more useful for columns that do not appear frequently in the WHERE clause in queries." Then
        score = score + 0
        Session("correct") = "Be careful indexing attributes that have null values."
        Session("question") = "1.A rule of thumb for choosing indexes for a relational database includes which of the following?"
        Session("yourans") = "Indexes are more useful for columns that do not appear frequently in the WHERE clause in queries."
        Session("score1") = 0
    ElseIf RadioButtonList1.SelectedValue = "Do not specify a unique index for the primary key of each table." Then
        score = score + 0
        Session("correct") = "Be careful indexing attributes that have null values."
        Session("question") = "1.A rule of thumb for choosing indexes for a relational database includes which of the following?"
        Session("yourans") = "Do not specify a unique index for the primary key of each table."
        Session("score1") = 0
    ElseIf RadioButtonList1.SelectedValue = "" Then
        score = score + 0
        Session("correct") = "Be careful indexing attributes that have null values."
        Session("question") = "1.A rule of thumb for choosing indexes for a relational database includes which of the following?"
        Session("yourans") = "No Answer"
        Session("score1") = 0
    End If


    Session("per") = Math.Round(score / 1 * 100)
    If Session("per") <= 50 Then
        Session("state") = "Poor Your Aptitude skills need to be improved!"
    ElseIf Session("per") <= 60 Then
        Session("state") = "Poor spend more time!"
    ElseIf Session("per") <= 70 Then
        Session("state") = "Not bad You need to pratice!"
    ElseIf Session("per") <= 80 Then
        Session("state") = "good with a little more work You could be really proud!"
    ElseIf Session("per") <= 90 Then
        Session("state") = "Very good you still missed one or more question pratice!"
    ElseIf Session("per") <= 99 Then
        Session("state") = " Excellent Which question(s) did You Miss!"
    ElseIf Session("per") <= 100 Then
        Session("state") = "Perfect You Should be really Product!"
    End If
    Session("a13") = "You score is " & score _
   & " Out of 10," & Session("per") & "%," & Session("state")
    Session("f") = score
    con.Open()
    qur = "insert into res values('" + Session("rn").ToString() + "','" + Session("na").ToString() + "','" + Session("cl").ToString() + "','" + Session("de").ToString() + "','" + Session("da").ToString() + "','" + Session("mn").ToString() + "','" + Session("em").ToString() + "','" + Session("f").ToString() + "','" + Session("per").ToString() + "','" + Session("state").ToString() + "')"
    cmd = New SqlCommand(qur, con)
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    con.Close()
    Response.Redirect("Aptituderesult.aspx")
End Sub

结果页面

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Label1.Text = Session("us11").ToString()
    Label2.Text = Session("question").ToString()
    Label3.Text = Session("correct").ToString()
    Label4.Text = Session("yourans").ToString()
    Label5.Text = Session("score1").ToString()
    Label6.Text = Session("a13").ToString()
End Sub

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

当出现这种类型的错误时,很明显你没有初始化控件的值等,并试图使用相同的错误。 在这种情况下,无法从会话中重新尝试该值,因为会话可能为null,同时没有值。 更好的方法是检查例如:

if(Session["us11"]!=null)
{
..//the assign the value
}