我在母版页中的 c# 编码如下
DBLayer odb = new DBLayer();
SqlDataReader dr;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.ToString() == "Commercial")
{
bind1();
}
else if (DropDownList1.SelectedItem.ToString() == "Residential")
{
bind2();
}
else
{
bind3();
}
}
public void bind1()
{
string query = "select * from commercialproperty";
dr = odb.SelectMethod(query);
if (dr.Read())
{
Label11.Text = dr[0].ToString();
Label1.Text = dr[1].ToString();
Label2.Text = dr[2].ToString();
Label3.Text = dr[3].ToString();
Label4.Text = dr[4].ToString();
Label5.Text = dr[5].ToString();
Label6.Text = dr[6].ToString();
Label7.Text = dr[7].ToString();
Label8.Text = dr[8].ToString();
Label9.Text = dr[9].ToString();
Label10.Text = dr[10].ToString();
}
else
{
Response.Write("<script>alert('Record Not Found')</script>");
}
}
}
我在 DBLayer.cs 中的代码如下
公共类 DBLayer
{
public SqlConnection _SqlConnection;
public SqlCommand _SqlCommand;
public SqlDataAdapter _SqlDataAdapter;
public SqlDataReader _SqlDataReader;
public DataSet _DataSet;
public DBLayer()
{
}
public int InsertEditDelete(string query)
{
int i;
try
{
_SqlConnection = new SqlConnection("server=.;user id=sa;password=sa;initial catalog=RealEstate");
_SqlConnection.Open();
_SqlCommand = new SqlCommand(query, _SqlConnection);
i = _SqlCommand.ExecuteNonQuery();
}
catch (Exception ied)
{
i = -1;
}
finally
{
_SqlConnection.Dispose();
_SqlCommand.Dispose();
_SqlConnection.Close();
}
return i;
}
public DataSet DataAdapter(string query, string tname)
{
try
{
_SqlConnection = new SqlConnection("server=.;user id=sa;password=sa;initial catalog=RealEstate");
_SqlConnection.Open();
_SqlDataAdapter = new SqlDataAdapter(query, _SqlConnection);
_DataSet = new DataSet();
_SqlDataAdapter.Fill(_DataSet,tname);
}
catch (Exception ds)
{
_DataSet = null;
}
finally
{
_SqlConnection.Dispose();
_SqlConnection.Close();
}
return _DataSet;
}
public SqlDataReader SelectMethod(string query)
{
try
{
_SqlConnection = new SqlConnection("server=.;user id=sa;password=sa;initial catalog=RealEstate");
_SqlConnection.Open();
_SqlCommand = new SqlCommand(query, _SqlConnection);
_SqlDataReader = _SqlCommand.ExecuteReader();
}
catch (Exception sm)
{
_SqlDataReader = null;
}
return _SqlDataReader;
}
}
当我运行网站时,它在以下点爆炸,如果 (dr.Read()) [错误消息:对象引用未设置为对象的实例。]
我错过了什么?我是一个初学者:P 帮助解决这个问题将运行该网站非常感谢