2012-12-14 43 views
0

在母版页我的C#代码如下的Visual Studio 2012的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有助于解决这个问题,将运行该网站wonderfuly 感谢

+0

爆炸代码:crikey –

+1

当'SelectMethod(string)'函数的结果为'null'时,什么是'odb'? – Jodrell

+1

下面是从SQL数据库读取的一个很好的示例:http://www.dotnetperls.com/sqlconnection。它可能有帮助。 – FishBasketGordo

回答

2

我不知道什么是你的代码odb,但问题是,SelectMethod被返回null。

您的编辑之后,它看起来像罪魁祸首是你SelectMethod的try/catch块:可能正在引发异常,因为你不需要做任何事情有了它,你可以不知道发生了什么错误:

try { 
    // do DB stuff... 
} 
catch (Exception sm) { // What does this exception contain??? 
    _SqlDataReader = null; 
} 

尝试用调试程序加入代码,查看异常情况。如果你不能这样做,只要删除catch块:你可以看到,它只是隐藏了原因的失败,但它并没有使代码神奇地工作:)

+0

如果有人有我的时间我可以邮寄他们的项目检查出来,如果可能的话?需要一些帮助不好,我很困惑在这一点 –

+0

@LeachimHca:看到我更新的答案。顺便说一句,你可以(你知道很热)调试你的代码吗? –

+0

*如果你不能这样做,只需删除catch块:*否如果只是删除catch块。 –

0

我看到你的代码中至少有4个问题 你沉默地吞下你的代码中的例外,没有任何意义。

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.你在关闭它之前已经处理了你的连接,这个异常将被抛到最后。

finally 
    { 
     _SqlConnection.Dispose();    
     _SqlConnection.Close(); 
    } 

3.吞下异常并且什么也不做,你失去了你的堆栈跟踪。

4设置_sqlconnection和那些非托管对象是全局变量而不实现IDisposable是不好的。

终于在代码行中设置了一个断点,其中抛出异常并尝试自己调试您的代码。