当前位置: 代码迷 >> VC >> 未将对象引用设置到对象的实例解决办法
  详细解决方案

未将对象引用设置到对象的实例解决办法

热度:8494   发布时间:2013-02-25 00:00:00.0
未将对象引用设置到对象的实例
这是个经常遇到的问题,我在winform中做了个用户登录的界面,输入用户名和密码后登录会提示“未将对象引用设置到对象的实例”,详细如下:

未处理 System.NullReferenceException
  Message="未将对象引用设置到对象的实例。"
  Source="CRM_Login"
  StackTrace:
  在 lengku48.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\冷库48\Form1.cs:行号 112
  在 System.Windows.Forms.Control.OnClick(EventArgs e)
  在 System.Windows.Forms.Button.OnClick(EventArgs e)
  在 System.Windows.Forms.Button.PerformClick()
  在 System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
  在 System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
  在 System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
  在 System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
  在 System.Windows.Forms.Control.PreProcessMessage(Message& msg)
  在 System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
  在 System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
  在 System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
  在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
  在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  在 System.Windows.Forms.Application.Run(Form mainForm)
  在 lengku48.Program.Main() 位置 E:\冷库48\Program.cs:行号 17
  在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
  在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:
出错位置:
 
C# code
private void button1_Click(object sender, EventArgs e)        {            SqlDataReader sdr = null;            if (String.IsNullOrEmpty(txt_LoginName.Text.Trim()))            {                MessageBox.Show("登录用户不许为空!", "软件提示");                txt_LoginName.Focus();                return;            }            if (String.IsNullOrEmpty(txt_LoginPwd.Text))            {                MessageBox.Show("登录密码不许为空!", "软件提示");                txt_LoginPwd.Focus();                return;            }            //用户编码不重复            string strSql = "select * from Operator where OperatorCode = '" + txt_LoginName.Text.Trim() + "'";            try            {                sdr = dal.GetDataReader(strSql);                if (!sdr.HasRows)  //若该用户编码无数据记录                {                    MessageBox.Show("登录用户不正确!", "软件提示");                    txt_LoginName.Focus();                }                else                {                    sdr.Read(); //读取唯一的一行记录                    if (!(txt_LoginPwd.Text == sdr["Password"].ToString()))  //若密码不相同                    {                                               MessageBox.Show("登录密码不正确!", "软件提示");                        txt_LoginPwd.Focus();                    }                    else                    {                        GlobalProperty.OperatorCode = sdr["OperatorCode"].ToString();                        GlobalProperty.OperatorName = sdr["OperatorName"].ToString();                        GlobalProperty.Password = sdr["Password"].ToString();                        GlobalProperty.IsFlag = sdr["IsFlag"].ToString();                        //this.Hide();                        timer1.Start();                    }                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "软件提示");            }            finally            {                [color=#FF0000]sdr.Close();[/color]//出错代码            }        }
  相关解决方案