Reputation: 1
My c# coding in Master page is the following
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>");
}
}
}
My Code In DBLayer.cs is the following
public class 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;
}
}
when I run the website it's exploding at the following point, if (dr.Read()) [Error Msg: Object reference not set to an instance of an object.]
what am I missing? i'm kind of a beginner :P help to solve this issue will run the site wonderfuly thanks
Upvotes: 0
Views: 460
Reputation: 3052
I saw at least 4 problems in your code 1. you silencely swallow your exception in your code, doesn't make any sence.
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;
}**
2. you disposed your connection before you close it, the exception will be throwed in the finally.
finally
{
_SqlConnection.Dispose();
_SqlConnection.Close();
}
3. it's not a best prectice to swallow the exception and do nothing, you lost your stacktrace.
4 set _sqlconnection and those unmanaged object to be globle variable without implement IDisposable is not good.
finally set a break point on the line of code where exception throwed and try to debug your code yourself.
Upvotes: 0
Reputation: 57252
I don't know what is odb
in your code, but the point is that SelectMethod
is returning null.
After your edit, it looks like the culprit is the try/catch block in your SelectMethod
: probably an exception is being raised, and since you just do nothing with it, you cannot understand what's going wrong:
try {
// do DB stuff...
}
catch (Exception sm) { // What does this exception contain???
_SqlDataReader = null;
}
Try stepping in your code with the debugger and see what the exception is. If you cannot do that, just remove the catch block: as you can see, it's only hiding the failure of the cause, but it's not making the code magically work :)
Upvotes: 2