2016-03-29 21 views
-1

SQL Server Management Studioにデータベースファイルを保存しています。私は自分のデータベースに格納されているPDFファイルを表示するgridviewコントロールを持っています。私はまた、対応するファイルを表示すると思われるリンクボタン "View"を持っています。私が持っている問題は、内容が私のリテラルコントロールに表示されていないということです。編集:私はまだ私のリテラルコントロールで表示する内容を取得する問題があります。エラーが表示されます。データが存在しないときに読み込みが無効です。私が得ているエラーは次の行です: bytes =(byte [])sdr ["Attachment"]; これ以外に何を試していいのか分かりませんが、これまでのコードはここにあります。私のリテラルコントロールにPDFコンテンツが表示されないのはなぜですか?

これは、QueryStringのIDを使用するハンドラです。 Fileデータをフェッチすることを想定しています。ここに私のハンドラのコードは次のとおりです。

public class FileCS : IHttpHandler 
{ 
public void ProcessRequest(HttpContext context) 
{ 
    int id = int.Parse(context.Request.QueryString["id"]); 
    byte[] bytes; 
    string fileName, contentType; 
    string constr = ConfigurationManager.ConnectionStrings["PALM3ConnectionString2"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "SELECT Attachment FROM Support_Doc WHERE [email protected]"; 
      cmd.Parameters.AddWithValue("@SupportDocID", id); 
      cmd.Connection = con; 
      con.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       sdr.Read(); 
       bytes = (byte[])sdr["Attachment"]; 
       contentType = sdr["Name"].ToString(); 
       fileName = sdr["Type"].ToString(); 

      } 
      con.Close(); 
     } 
    } 
    context.Response.Buffer = true; 
    context.Response.Charset = ""; 
    if (context.Request.QueryString["download"] == "1") 
    { 
     context.Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName); 
    } 
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    context.Response.ContentType = "application/pdf"; 
    context.Response.BinaryWrite(bytes); 
    context.Response.Flush(); 
    context.Response.End(); 
} 
public bool IsReusable 
{ 
    get 
    { 
     return false; 
    } 
} 
} 

これは、それは私のGridView内でクリックされたときに、私のLinkBut​​tonコントロールを処理するための私のコードです:

protected void Button2_Click(object sender, EventArgs e) 
    { 

     //int id = int.Parse((sender as LinkButton).CommandArgument); 
     int id = Convert.ToInt32(Request.QueryString["SupportDocID"]); 
     string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"500%\" height=\"600px\">"; 
     embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>"; 
     embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file."; 
     embed += "</object>"; 

     ltEmbed.Text = string.Format(embed, ResolveUrl("~/FileCS.ashx?Id="), id); 

    } 

このコードは、私のデータベースにファイルのアップロード

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); 
     string contentType = FileUpload1.PostedFile.ContentType; 
     using (Stream fs = FileUpload1.PostedFile.InputStream) 
     { 
      using (BinaryReader br = new BinaryReader(fs)) 
      { 
       byte[] bytes = br.ReadBytes((Int32)fs.Length); 
       using (SqlConnection con = Connection.GetConnection()) 
       { 
        //using (SqlCommand com = new SqlCommand()) 
        //{ 

         // com.CommandText = "UploadDoc"; 
         //com.CommandType = CommandType.StoredProcedure; 
        string query = "insert into Support_Doc values (@Type, @Name, @Attachment)"; 
        using (SqlCommand com = new SqlCommand(query)){ 
         com.Connection = con; 
         com.Parameters.AddWithValue("@Type", filename); 
         com.Parameters.AddWithValue("@Name", contentType); 
         com.Parameters.AddWithValue("@Attachment", bytes); 
         con.Open(); 
         com.ExecuteNonQuery(); 
         Label1.ForeColor = System.Drawing.Color.Green; 
         Label1.Text = "File Uploaded Successfully!"; 
         con.Close(); 
        } 

       } 
      } 
     } 
     Response.Redirect(Request.Url.AbsoluteUri); 
    } 

私は絶対に運がないとこれをどこでも見てきました。私はsooooに感謝するどんな助けを!!!再度、感謝します!!!!!

+0

どのようなリテラルコントロールですか?どの行がエラーをスローしますか? – David

+0

この行でエラーが発生します。int id = int.Parse(context.Request.QueryString ["id"]); – user3474069

+0

そして、 'context.Request.QueryString [" id "]'に何がありますか?このエラーは、それが「ヌル」であることを示唆しているようです。 – David

答えて

0

この行(およびそれのような他のもの):

bytes = (byte[])sdr["Attachment"]; 

データは、データリーダ内に存在することを仮定しています。しかし、この回線が返す場合はfalse

sdr.Read(); 

この場合、レコードは見つかりませんでした。だから、エラーはそこにないレコードを読もうとしていることを伝えています。これは、空の配列(emptyArray[0])の最初の要素を読み取ろうとするのと同じです。

戻り値boolsdr.Read()から戻り値を使用して、そのレコードを読み取ろうとする前にレコードが存在するかどうかを判断できます。あなたは(コードが意味する)、正確に一つのレコードを期待している場合、このようなものが動作するはずです:

if (sdr.Read()) 
{ 
    bytes = (byte[])sdr["Attachment"]; 
    contentType = sdr["Name"].ToString(); 
    fileName = sdr["Type"].ToString(); 
} 
else 
{ 
    // no record was found, how do you want to proceed? 
} 

elseブロックに何をするあなた次第です。たぶんユーザーにエラーメッセージを返しますか?たぶん、別のクエリを実行して、「デフォルト」のPDFをユーザーに返しますか?他に何か?しかし、エラー状態を処理したいのは、システムをどのように動作させるかによって異なります。要点は、潜在的なエラー状態が存在し、それをチェックする必要があるということです。

最終的には存在しないレコードが要求されています。したがって、コードはそれを考慮に入れる必要があります。

+0

問題が見つかりました。問題はButton2_Clickイベントにありました。私は使用していたはずです: int id = int.Parse((LinkBut​​tonとしての送信者).CommandArgument); の代わりに を使用します。int id = Convert.ToInt32(Request.QueryString ["SupportDocID"]); 私は元々それを使用していましたが、私のハンドラで使用していたクエリ文字IDは、idではなくSupportDocIDに設定されていました。私は読者のためにif else文を追加しました。 Davidに感謝します! – user3474069

関連する問題