2014-12-07 59 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;