在下面的代码中,当我按下 button2 时,它说:
你调用的对象是空的
这是怎么回事?
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);
}
}
}