开发者

getting news from DB

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();
        }
    }

hello, i have problem with this method. I connected these "news" and "contents" from DB with news_title and news_content, and in newsarticle.aspx site i have two literal controls which writes news and content. And i have

<a href="/newsarticle.aspx?id=<%#Eval("ID") %>"> 
   <asp:Label ID="lblTitle" runat="server" Text='<开发者_StackOverflow中文版;%#Eval("News") %>'>
   </asp:Label>
</a>

, with this I get links of news name and when I click on one the link it always returns me last name and contentfrom table. exmp. if i have 4 liks and when i click on 2 i get result from 4 link. Naybody know what could be solution ?


You have not added article_id in your select statement's where clause.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜