C# 对象引用未设置为对象 asp.net 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19473089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
object reference not set to an instance of an object asp.net
提问by Shakir Nasser
In the below code, when I press button2 it says:
在下面的代码中,当我按下 button2 时,它说:
object reference not set to an instance of an object
你调用的对象是空的
What's going on?
这是怎么回事?
public partial class rec : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DB.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
con.Open();
cmd = new SqlCommand("SELECT SrviceType, Msg FROM OrderNum ", con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
Label1.Text = dr[0].ToString();
TextBox1.Text = dr[1].ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlDataReader dr = null;
try
{
dr.Read();
Label1.Text = dr[0].ToString();
TextBox1.Text = dr[1].ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
回答by Mudeyanse
SqlDataReader dr = null;
Then you try to read from null object from
然后你尝试从空对象中读取
dr.Read();
Make sure this is web page you can not keep the state if you want to get the Button_click1
data rearder
确保这是网页,如果您想获得Button_click1
数据,则不能保持状态
回答by Jerry
You need to assign the reader dr
to a command.
您需要将阅读器分配给dr
命令。
Take a look at the example here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
看看这里的例子:http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
回答by Sudhakar Tillapudi
SqlDataReader object is used to hold the one time result of executed/fetched data from the database.which can be used to iterate over each row to get the required columns. hence before Trying to read from SqlDataReader object it should have some data.
SqlDataReader 对象用于保存从数据库中执行/获取数据的一次性结果。可用于迭代每一行以获取所需的列。因此在尝试从 SqlDataReader 对象读取之前,它应该有一些数据。
which can be accomplished by following statement:
这可以通过以下语句来完成:
SqlDataReader sqldatareaderobject=sqlcommandobject.ExecuteReader();
SqlDataReader sqldatareaderobject=sqlcommandobject.ExecuteReader();
you are following this above principle in Button1_click function but you are missing the same principle in Button2_click function.
您在 Button1_click 函数中遵循上述原则,但在 Button2_click 函数中缺少相同的原则。
SqlDataReader object in your case "dr" contains null as you have missed to call the ExecuteReader() function, and it is throwing exception as you are calling Read() function on top of null object(dr).
在您的情况下,SqlDataReader 对象“dr”包含 null,因为您错过了调用 ExecuteReader() 函数,并且当您在 null 对象(dr)之上调用 Read() 函数时,它会抛出异常。
Thank you
谢谢