2017-06-21 4 views
0

SQL ServerからPDF/JPGファイルを取得またはダウンロードできません。ブラウザのボタンをクリックすると、応答せず何も起こりません。親切に私を助けてください。コードに問題があることを確認してください。 iは整数としてIDを含むテーブルを持っていると背後C#を使用してデータベースからPDF/JPGファイルを取得またはダウンロードできません

enter image description here enter image description here

やコード:あなたはすべてのエラーをキャッチし、nullを返しているので、それは何も返さないです。..

protected void btnView_Click(object sender, EventArgs e) 
    { 
     string strQuery = "select Upload_Name, Content_Type, Uploads from Tab_CPD_Hours WHERE id [email protected]"; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1; 
     DataTable dt = GetData(cmd); 
     if (dt != null) 
     { 
      download(dt); 
     } 
    } 

    private void download(DataTable dt) 
    { 
     Byte[] bytes = (Byte[])dt.Rows[0]["Uploads"]; 
     Response.Buffer = true; 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = dt.Rows[0]["Content_Type"].ToString(); 
     Response.AddHeader("content-disposition", "attachment;filename=" 
     + dt.Rows[0]["Upload_Name"].ToString()); 
     Response.BinaryWrite(bytes); 
     Response.Flush(); 
     Response.End(); 
    } 

public static DataTable GetData(SqlCommand cmd) 
    { 
     DataTable dt = new DataTable(); 
     string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     try 
     { 
      con.Open(); 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
      return dt; 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      con.Close(); 
      sda.Dispose(); 
      con.Dispose(); 
     } 
    } 

答えて

0

catch 
{ 
    return null; 
} 

また、データを取得するメソッドは、nullが返されたときは何も行いませんエラーがない

+0

...あなたはユーザーにエラーメッセージを表示することによって、適切にエラーを処理する必要があります...

DataTable dt = GetData(cmd); if (dt != null) { download(dt); } 

を回し、私はそのはすべてのエラーをキャッチされていませんチェックして...それ以降はプログラムの終了までデバッグしていますが、何も起こっていません...私は助けが必要です:/ –

+1

あなたはaspxページでupdatepanelを使用していますか? –

+0

はい..これが私が昨日解決した理由です。でも、助けてくれてありがとう。 :) –

関連する問題