这是个经常遇到的问题,我在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:
出错位置:
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
{
sdr.Close();//出错代码
} }
希望大家能帮我分析一下,是try...catch...finally有问题还是数据库的事,先谢过了。
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:
出错位置:
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
{
sdr.Close();//出错代码
} }
希望大家能帮我分析一下,是try...catch...finally有问题还是数据库的事,先谢过了。
解决方案 »
- Professional C# 4 amd .NET4的一个例子RoomReservation 怎么弄都不行
- 急!hashtable
- 求asp.net好的书籍
- agsxmpp急,能不能在广域网中使用。
- winfrom中datagrid时间列的判断事件
- “/AttributeQuery”应用程序中的服务器错误。
- 请问高手们是否可以用正则表达式判断一个算术表达式的合法性?(比如括号配对) 请高手指点并提供一个该正则表达式串。
- 请教一个关于在listbox中双击某个文件后(如A.TXT),用当前电脑中相应的打开方式打开的方法。
- 请VB高手+C#高手帮我把VB写的这个函数翻译成C#的(在线...)
- CSDN的兄弟帮个忙啊,当打开一个EXCEL的时候就会弹出一个对话框
- 帮助
- 解决问题马上给分
if(sdr != null)
{
sdr.Close();
}
应该就可以了。
但是最好看看dal.GetDataReader(strSql)为什么会返回null。
private void timer1_Tick(object sender, EventArgs e)
{
this.Opacity += dou;
if (this.Opacity == 1)
{
timer1.Stop();
dou = -0.05;
timer2.Start();
}else if (this.Opacity == 0)
{
timer1.Stop();
Form2 form = new Form2();
show.Get_Show(form);
}
}
处循环不出来了,不知道是什么原因
catch (Exception e)
{
throw e;
}
捕捉到SqlException:
在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
这会是什么问题引起的啊?不知道您能不能指点一下迷津呀?
我昨天也搜过那个异常,不过没什么收获,我再查查看吧,谢谢你啦!
应该是这样吧
string strConn = "Server = " + strServer + ";Database=lengku;User id=" + strUserID + ";PWD=" + strPwd;
[DataBase]
Server=CY-63FBE3FF6AE5\SQLEXPRESS
UserID=sa
Pwd=sa
这样调用就好用了