private void button1_Click(object sender, EventArgs e)
        {
            if (names.Text.Trim() == "")
            {
                MessageBox.Show("用户名不能为空!", "提示信息");
                names.Focus();
                return;
            }
            if (pwd.Text.Trim() == "")
            {
                MessageBox.Show("密码不能为空!", "提示信息");
                pwd.Focus();
                return;
            }
            int i = 0;
            string xmlfilename, userid, password, DataSource;
            xmlfilename = System.Web.HttpContext.Current.Server.MapPath(".\\dataconfig.xml"); //这行提示"未将对象引用设置到对象的实例"
            DataSet myDs = new DataSet();
            myDs.ReadXml(xmlfilename);
            DataRow myDataRow = myDs.Tables[0].Rows[0];
            userid = myDataRow["userid"].ToString();
            password = myDataRow["password"].ToString();
            DataSource = myDataRow["DataSource"].ToString();
            string sqlString = "userid=" + userid + ";password=" + password + ";DataSource=" + DataSource + "'";
            OracleConnection conn = new OracleConnection(sqlString);
            //dataConn.ConnectionString = sqlString;
            conn.Open();
             //MessageBox.Show("数据库连接成功");
            {                string sql = "select count(*) from PACSUSER where LOGINID='" + names.Text.Trim() + "' and PWD='" + pwd.Text.Trim() + "'";
                MessageBox.Show(sql);
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    //i = (int)cmd.ExecuteScalar();
                    i = (int)Convert.ToInt32(cmd.ExecuteScalar());  //这行提示"未将对象引用设置到对象的实例"
                }
            }
            if (i > 0)
            {
                bl = true;                        
                this.Close();
                MainForm MF = new MainForm();
                MF.Show();               
            }
            else
            {
                MessageBox.Show("用户名或密码错误!请重新登陆!", "提示信息");
            }麻烦各位大虾帮忙分析下我上面贴出的红色部分报错?非常感谢

解决方案 »

  1.   

    cmd.ExecuteScalar()
    看看是不是等于null
      

  2.   

    System.Web.HttpContext.Current.Server.MapPath(".\\dataconfig.xml"); 
    你用断点监视一下,我估计肯定有空值,
    一般这种错误 ,大部分都是空值引起的
      

  3.   

    大哥,你这样写sql漏洞太大了,还是用参数吧
    你的错误是某个对象是null,你自己debug一下就知道了
      

  4.   

    System.Web.HttpContext.Current
    你一个CS项目哪来的HTTPCONTEXT
      

  5.   

           xmlfilename = System.Web.HttpContext.Current.Server.MapPath(".\\dataconfig.xml"); //这行提示"未将对象引用设置到对象的实例" 
    这在web上用了,winform上不是这种方式.
     i = (int)Convert.ToInt32(cmd.ExecuteScalar());  //这行提示"未将对象引用设置到对象的实例" 
    这行看是不是执行结果为null呢.
      

  6.   


    MapPath("dataconfig.xml"); Debug下直接这么写
    你得下断点跟踪
    你这个写的的确太乱了,建议不要这么写
      

  7.   

    WINFORM上是使用哪种方式了?麻烦前辈赐教!
    后面那句反馈的为空值,不知道怎么处理?
    因为生成没有报错,运行时报的错误
      

  8.   

    System.Web.HttpContext.Current:是一个HTTP请求
    你这里没有
      

  9.   

    如果dataconfig.xml这个文件在本地,则直接用硬盘路径
    如果是服务器
    那么需要使用HttpWebRequest,HttpWebResponse类下载此文件
      

  10.   

    是不是缺少类呀
    using....一下
      

  11.   

    按照刚才各位说的我改了该程序目前又出现些新的问题 string  userID, password, DataSource;
                XmlTextReader reader = new XmlTextReader(".\\dataconfig.xml");
                DataSet myDs = new DataSet();
                myDs.ReadXml(reader);
                DataRow myDataRow = myDs.Tables[0].Rows[0];
                userID = myDataRow["userid"].ToString();
                password = myDataRow["password"].ToString();
                DataSource = myDataRow["DataSource"].ToString();
                string sqlString = "userid=" + userID + ";password=" + password + ";DataSource=" + DataSource + "'";
                MessageBox.Show(sqlString);
                OracleConnection conn = new OracleConnection(sqlString); //提示不支持关键字userid
                //conn.ConnectionString = sqlString;
                conn.Open();
      

  12.   

    非常感谢现在又出现一下问题
    OracleConnection conn = new OracleConnection(sqlString); //提示从索引 31 处开始,初始化字符串的格式不符合规范
      

  13.   

    LZ 把sqlString 设试时的值COPY出来看看,然后使用SQLCONNECTION控件的配置向导配出一个来对照一下
      

  14.   

    int count = (int) cmd.ExecuteScalar();设个断点,看执行的结果
      

  15.   

    断点跟踪的值为:sqlString="Data Source=test;user id=test;password =test"说明值不为空但还是提示从索引 34 处开始,初始化字符串的格式不符合规范