マスターページでの私の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;
}
}
Webサイトを実行すると、次の時点で爆発します。if(dr.Read())[エラーメッセージ:オブジェクト参照がオブジェクトのインスタンスに設定されていません。]
私は何が欠けていますか?私は一種の初心者です:この問題を解決するためのPヘルプはサイトを素晴らしい運営になりますありがとう