2016-04-17 16 views
1

私はテキストボックスに保持されているIDに従ってデータの行を取得する簡単なクエリを作成しています。しかし、情報を取得していないし、エラーでもない。QueryStringからテーブルを取得するASP.NET

私はテキストボックスにURLで渡されるクエリ文字列のパラメータが入っています。これは動作しており、ページ上に正確なIDを表示しています。

これを使用して、残りの情報を関連するフィールドに取り込みます。

C#

protected void Page_Load(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True"); 
      con.Open(); 
      try 
      { 

       SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con); 
       sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text; 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 

       if (dt.Rows.Count > 0) 
        nameTxt.Text = dt.Rows[0][0].ToString(); 
       descriptionTxt.Text = dt.Rows[0][1].ToString(); 
       instructionsTxt.Text = dt.Rows[0][2].ToString(); 

       dt.Clear(); 

      } 
      catch (Exception ex) 
      { 

      } 

      con.Close(); 
     } 

ASP.NET

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> 
    <hgroup class="title"> 
     <h1><asp:Label ID="RecipeID" runat="server" ><%=Request.QueryString["id"] %></asp:Label></h1> 

     <asp:Label ID="nameTxt" runat="server" Text="Name"></asp:Label> 
    </hgroup> 

      <table style="width:926px"> 
       <tr> 
       <td class="auto-style2" > IMAGE </td> 
       <td > 
        <asp:Panel ID="descriptionPnl" runat="server" BackColor="White" Height="160px" Width="472px"> 
         <asp:Label ID="descriptionTxt" runat="server" Text="Label"></asp:Label> 
        </asp:Panel> 
        </td> 
       </tr>  
      </table> 

    <h6> Step by Step Guide</h6> 

      <table style="width:900px"> 
       <tr> 
       <td > 
        <asp:Panel ID="guidePnl" runat="server" BackColor="White" Height="200px" Width="900px"> 
         <asp:Label ID="instructionsTxt" runat="server" Text="Label"></asp:Label> 
        </asp:Panel> 
        </td> 
       </tr> 
      </table>   

    </asp:Content> 

誰もが問題で私を助けることができますか?どこが間違っていて、何を追加したり変更したりする必要がありますか?ありがとうございました。

+2

私はあなたがよく知っていると確信しています、それはあなたが文字列を連結する '+ '記号です。あなたの場合、恐怖です: 'Recipe_ID = '" + RecipeID.Text + "'" 'ブラウザの' RecipeID'フィールドに次のテキストを入力してみましょう: '; DROP TABLE Recipe; --'.Ooopsy。あなたがGoogleよりも十分な情報を得ることができると確信しています。つまり、クエリ文字列について話し、それらからいくつかの値を取得する前に、まず対処するより深刻な問題があります。 –

+1

@DarinDimitrov私は単純にパラメータ化されたクエリを追加し、それでも 'oopsy'はまだ動作していないので、これは実際に建設的な会話ではありません –

答えて

1

例外をすべてキャッチして何もしないので、間違いはありません。

また、そのコードを使用してSQLインジェクションに脆弱です(コメントに正しく記載されています)。

データベースファイル(配置時に破損する)を見つけるために相対パスを使用し、そのような設定情報をWeb.configファイルに入れる必要があります。

+0

相対パスを追加するにはどうすればいいですか? –

+1

Server.MapPath( "〜/ [Webアプリケーションのルートフォルダに相対的なdbへのパス] ");設定、パラメータのエンコーディング、および他の多くの概要については、Webアプリケーション開発のチュートリアルを読む必要があるようです。誰かがそれを確認する解放する前にスタックコードの見直しをすると、上のコードの問題は数多くあります。 – Nathan

0
protected void Page_Load(object sender, EventArgs e) 
     { 
      string ID = Request.QueryString["id"]; 
      RecipeID.Text = ID; 

      SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True"); 
      con.Open(); 
      try 
      { 

       SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con); 
       sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text; 
       DataTable dt = new DataTable(); 
       sda.Fill(dt); 

       if (dt.Rows.Count > 0) 
        nameTxt.Text = dt.Rows[0][0].ToString(); 
       descriptionTxt.Text = dt.Rows[0][1].ToString(); 
       instructionsTxt.Text = dt.Rows[0][2].ToString(); 

       dt.Clear(); 

      } 
      catch (Exception ex) 
      { 

      } 

      con.Close(); 
     } 
0

sda.SelectCommand.Parameters.Add( "@のrecipeid"、SqlDbType.Int).Valueの場合、Request.QueryString = [ "ID"]。

+0

あなたは自分のコードをコピーしましたか? –

関連する問題