2014-12-07 56 views
0
try 
{ 
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString; 
    SqlConnection con = new SqlConnection(str); 
    SqlCommand cmd = new SqlCommand("GetProduct",con); 

    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@ProductId", productid); 

    SqlDataAdapter da = new SqlDataAdapter(); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 

} 
catch (Exception e) 
{ 
    //throw new Exception(message.ex); 

    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message); 

} 


return ds; 

我上面寫我的代碼,但顯示這個錯誤,每當我使用相同的代碼,而不試圖追趕其工作的高度名稱DS會在內容不會退出嘗試捕捉

名稱DS不會在內容出口

回答

2

必須定義DS的嘗試外{}塊

DataSet ds = new DataSet(); 
    try 
    { 
     //... 
    } 
    catch(Exception e) 
    { 
     //... 
    } 
1

您正在返回ds,這意味着您需要在嘗試開始之前對其進行定義。無法保證您的trycatch塊中的特定行將存在。

嘗試在try catch之前定義變量,看看它是否適合您。

DataSet ds = null; 
try 
{ 
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString; 
    SqlConnection con = new SqlConnection(str); 
    SqlCommand cmd = new SqlCommand("GetProduct",con); 

    cmd.CommandType = System.Data.CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@ProductId", productid); 

    SqlDataAdapter da = new SqlDataAdapter(); 
    ds = new DataSet(); 
    da.Fill(ds); 
} 
catch (Exception e) 
{ 
    //throw new Exception(message.ex); 
    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message); 
} 


return ds;