2011-04-05 87 views
2
public partial class newsarticle : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      int article_id = Convert.ToInt32(Request.QueryString["id"]); 

      string select = "SELECT [ID],[NEWS],[CONTENTS] FROM [NEWS]"; 
      string strCon = System.Web 
         .Configuration 
         .WebConfigurationManager 
         .ConnectionStrings["ConnectionString"] 
         .ConnectionString; 

      SqlConnection conn = new SqlConnection(strCon); 
      conn.ConnectionString = strCon; 
      conn.Open(); 
      SqlDataReader myReader = null; 
      SqlCommand myCommand = new SqlCommand(select, conn); 
      myReader = myCommand.ExecuteReader(); 

      while (myReader.Read()) 
      { 
       news_title.Text = myReader["NEWS"].ToString(); 
       news_content.Text = myReader["CONTENTS"].ToString(); 

      } 
      conn.Close(); 
     } 
    } 

你好,我有這個方法的問題。我將這些來自DB的「新聞」和「內容」與news_title和news_content連接起來,並且在newsarticle.aspx站點中我有兩個文字控件,它們寫新聞和內容。我有從DB獲取消息

<a href="/newsarticle.aspx?id=<%#Eval("ID") %>"> 
    <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("News") %>'> 
    </asp:Label> 
</a> 

, 這個我得到消息稱鏈接,當我點擊一個鏈接,它總是返回我的姓氏和contentfrom表。 exmp。如果我有4希望,當我點擊2我從4鏈接得到結果。不知道什麼可以解決?

回答

2

您還沒有在您的select語句的where子句中添加article_id

string select = "SELECT [ID],[NEWS],[CONTENTS] FROM [NEWS] where ID = " + article_id.ToString(); 
+0

我再次得到相同的resutl。 – mikrimouse 2011-04-05 10:49:22

+0

其他人嗎? – mikrimouse 2011-04-05 12:16:13

+0

你可以調試,並檢查你是否在這裏得到id int article_id = Convert.ToInt32(Request.QueryString [「id」]);您需要在調試模式下檢查此變量article_id – 2011-04-05 12:23:57