using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class testing : System.Web.UI.Page
{
    int num = 0;
    static int j=0;
    static string[] answer = new string[10];
    static int[] s = new int[10];
    static Random a = new Random();
    protected void Page_Load(object sender, EventArgs e)
    {
        Random a = new Random();
        string connectionString = "Provider = Microsoft.Jet.OleDb.4.0; Data Source=" + MapPath("~/Database1.mdb");
        string b = Session["item"].ToString();
        string sql = "select *from que_table";
        OleDbConnection con = new OleDbConnection(connectionString);        con.Open();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        OleDbDataReader dbReader = cmd.ExecuteReader();
        int count = 1;
        while (dbReader.Read()) count++;
        int i = a.Next(count);//想要取十个完全不同的随机数为什么会这么难???
        int c = 0;
        while (c < j - 1)
        {
            int bc = 0;
            for (bc = 0; bc <= c; bc++)
                if (i == s[bc])
                {
                    i = a.Next(count);
                    break;
                }
            if(bc==c)c++;
        }
        s[num++] = i;
        sql = "Select content From que_table where queNo ='" + i.ToString() +
                    "'and subject='" + b + "'";
        cmd = new OleDbCommand(sql, con);        string question = Convert.ToString(cmd.ExecuteScalar());
        con.Close();
        quetxtbox.Text = question;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        answer[j++] = antxtbox.Text;
        antxtbox.Text = "";
        if (j == 9)
        {
            Button2.Enabled = false;
            j = 0;
        }
    }    protected void Button1_Click(object sender, EventArgs e)
    {
        string connectionString = "Provider = Microsoft.Jet.OleDb.4.0; Data Source=" + MapPath("~/Database1.mdb");
        string user = Session["user"].ToString();
        for (int count = 0; count < 10; count++)
        {
            string snum = s[count].ToString();
            OleDbConnection con = new OleDbConnection(connectionString);
            con.Open();
            string sql = "Select key From que_table where queNo ='"+snum+"'";//读取参考答案            OleDbCommand cmd = new OleDbCommand(sql, con);
            object obj = cmd.ExecuteScalar();
            string key = Convert.ToString(obj);
            sql = "Select queScore From que_table where queNo ='"+snum+"'";//读取分值
            cmd = new OleDbCommand(sql, con);
            int score=Convert.ToInt32(cmd.ExecuteScalar());
            int y_score;
            if (answer[count].Equals(key)) y_score = score;//这句到底错哪了?实在看不出来……
            else y_score = 0;
            sql = "insert into score_table values('" + user + "','" 
                + snum + "','" + answer[count] + "'," + y_score.ToString() + ")";
            cmd = new OleDbCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

解决方案 »

  1.   

    int score=Convert.ToInt32(cmd.ExecuteScalar());
    int y_score;
    if (answer[count].Equals(key)) y_score = score;//这句到底错哪了?实在看不出来……--------------------------------------------------------------answer[count] 没取到值?
      

  2.   

    在这句设断点,监视一下这个answer[count] 
      

  3.   

    vs的编译器对“引用类型的变量为null”的引用都会报"未将对象引用设置到对象的实例"错误。为了提高开发效率,建议你Google一下此类错误并详细了解一下。
    换言之:引用类型的变量在使用前必须实例化。
      

  4.   

    if (answer[count].Equals(key)) y_score = score;这句当中的answer[count]是null,因为在页面初始化和载入过程中没有为数组answer赋值
      

  5.   

     
    protected void Button2_Click(object sender, EventArgs e)
        {
            answer[j++] = antxtbox.Text;        antxtbox.Text = "";
            if (j == 9)
            {
                Button2.Enabled = false;
                j = 0;
            }
        }

    ///你循环的时候确定answer数组中有值?
      
     for (int count = 0; count < 10; count++)
            {
                if (answer[count].Equals(key)) y_score = score;//这句到底错哪了?实在看不出来……
            }
      

  6.   

    现在发现的是answer数组里有值,不过不是我输入的值……