Asp.net-Object引用未设置为对象的实例 [英] Asp.net-Object reference not set to an instance of an object
本文介绍了Asp.net-Object引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以告诉我有关错误对象引用未设置为对象实例的信息.
在我的应用程序中,将记录插入数据库时遇到此错误.
这是我的数据层类.
在数据层中,它在Throw块中引发错误.
Hi,
Can anyone tell me about the error Object reference not set to an instance of an object.
In my application I am getting this error while inserting records into the database.
Here is my Data layer class.
In data layer it is throwing the error in Throw block.
public int Insert(string _Type, string _Name, string _Address, string Postal_Address, string _Phone, string _Fax)
{
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("usp_proc", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
string Docket_ID;
cmd.Parameters.AddWithValue("@_Type", _Type);
cmd.Parameters.AddWithValue("@_Name", _Name);
cmd.Parameters.AddWithValue("@_Address", _Address);
cmd.Parameters.AddWithValue("@Postal_Address", Postal_Address);
cmd.Parameters.AddWithValue("@_Phone",_Phone);
cmd.Parameters.AddWithValue("@_Fax", _Fax);
;
SqlParameter p = new SqlParameter("@Emp_ID", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p);
Emp_ID = p.Value.ToString();
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
如果我的问题不清楚,请告诉我.
谢谢您,
Please Let me know if my question asking is not clear to solve.
Thank you,
推荐答案
对象引用未设置为对象的实例
当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [
Object reference not set to an instance of an object
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]
A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
这篇关于Asp.net-Object引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文