部分代码:
                        string sql = string.Format("select * from S where (SNo = '{0}' and SN = '{1}' and SAge = '{2}' and SDept = '{3}' and SClass = '{4}' and SAddress = '{5}')",
                            username.Text, stname.Text, stage.Text, comboBox2.Text, comboBox3.Text, stadd.Text);
                        SqlCommand command1 = new SqlCommand(sql, dbhelp.hong);
                        try
                        {
                            dbhelp.hong.Open();
                            int i = (int) command1.ExecuteScalar();
                            if (i >= 1)
                            {
                                if (username.Text != "")
                                {
                                    string sql1 = string.Format("select * from S where SNo ='{0}'", username.Text);
                                    sda = new SqlDataAdapter(sql1, dbhelp.hong);
                                    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                                    sda.Fill(ds,"S");
                                    dataGridView1.DataSource = ds.Tables["S"];
                                    SqlCommand command = new SqlCommand(sql1, dbhelp.hong);
                                    dbhelp.hong.Open();
                                    SqlDataReader datareader = command.ExecuteReader();
                                    if (!datareader.HasRows)
                                    {
                                        MessageBox.Show("数据库中不包含此数据,请核对后重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                    dbhelp.hong.Close();}
                                else if (stname.Text != "")
                                {
                                    string sql2 = string.Format("select * from S where SN like '%{0}%'", stname.Text);
                                    sda = new SqlDataAdapter(sql2, dbhelp.hong);
                                    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                                    sda.Fill(ds, "S");
                                    dataGridView1.DataSource = ds.Tables["S"];
                                    SqlCommand command = new SqlCommand(sql2, dbhelp.hong);
                                    dbhelp.hong.Open();
                                    SqlDataReader datareader = command.ExecuteReader();
                                    if (!datareader.HasRows)
                                    {
                                        MessageBox.Show("数据库中不包含此数据,请核对后重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                    dbhelp.hong.Close();
                                }
}
............
                            else
                            {
                                MessageBox.Show("您查找的用户数据与数据库不相符,请核对后重新输入!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                                username.Text = "";
                                stname.Text = "";
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("数据库出错!");
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            dbhelp.hong.Close();
                        }
运行之后跳出未将对象引用设置到对象的实例 
是哪里出了问题吗?

解决方案 »

  1.   

    我单步跟踪了,
    在 int i = (int) command1.ExecuteScalar();
    这句结束后直接跳到
      catch (Exception ex)
      {
      MessageBox.Show("数据库出错!");
      MessageBox.Show(ex.Message);
      }
      

  2.   

    ex.Message的提示信息是什么?
      

  3.   

    command1.ExecuteScalar返回第一行第一列数据
      

  4.   

    未将对象实例化,也就是说你的对象可能为空,查询下你的SQL语句
      

  5.   

    回复3楼:ex.Message提示数据库错误
      

  6.   

    查询分析中执行sql,F11单步判断NULL
      

  7.   


    command1.ExecuteScalar()这个返回的是一个object,估计应该是返回null了,然后被你强制(int)
    你可以改成:
    int i = (int)(command1.ExecuteScalar()?? 0);
    或者用
    if(int.TryParse(Convert.ToString(command1.ExecuteScalar(), out i))
    {
        if(i > 1)
            ...
    }
    else
    ....
      

  8.   

    支持楼上,应该是NULL,但是却被强奸了