2014-12-07 1 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 내가 내용에 <p>이름 DS</p>가 종료되지 않습니다 위대한 작업을 잡을 시도없이 동일한 코드를 사용할 때마다 내용에 종료되지이 오류가 난 위의 내 코드를 작성 캐치에게

답변

2

당신은 차단} {은 try 외부 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; 
관련 문제