用label显示新闻内容,怎么写。我刚开始学,实在是基础差。。。

zqc2010 2012-05-14 12:16:54
看了里面的好多帖子,研究了半天,自己也改了好多次,就是还不行。
只好再麻烦大家给我讲讲了。
我在之前的页中已经写好了按news_no跳转连接到shownewsPAGE的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="news_no" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="news_no" HeaderText="news_no" ReadOnly="True"
SortExpression="news_no" />
<asp:BoundField DataField="news_name" HeaderText="news_name"
SortExpression="news_name" />
<asp:HyperLinkField DataNavigateUrlFields="news_no"
DataNavigateUrlFormatString="USER\shownews.aspx?news_no={0}"
DataTextField="news_no" DataTextFormatString="查看{0}的详细信息" HeaderText="选择" />
</Columns>
</asp:GridView>
这个可以运行,点击查看后直接就到了shownews
可是到了要具体显示news的页面后,
我写了个后台代码如下:(我用了两个label)
public partial class USER_shownews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string news_no = Request.QueryString["news_no"];

string strConnection = "server=pc0113kzq\\Sqlexpress;database=2012;uid=2012;pwd=000000;";
SqlConnection Connection = new SqlConnection(strConnection);
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandText = "select * from news where news_no='"+news_no+"'";
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{

this.Label1.Text = sdr["news_name"].ToString();
this.Label2.Text = sdr["news_infor"].ToString();

}
sdr.Close();
}


}
}
数据库学校没有好好学,现在很乱。。。可是又到了交作业的时候。。
...全文
148 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqc2010 2012-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
谢谢你,我又试了,又有新问题了能帮我看看么

引用楼主 的回复:
看了里面的好多帖子,研究了半天,自己也改了好多次,就是还不行。
只好再麻烦大家给我讲讲了。
我在之前的页中已经写好了按news_no跳转连接到shownewsPAGE的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
……
[/Quote]
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class USER_shownews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string news_no = Request.QueryString["news_no"];


string strConnection = "server=pc0313kzq\\Sqlexpress;database=ZQC2012;uid=ZQC2012;pwd=000000;";
SqlConnection Connection = new SqlConnection(strConnection);

SqlCommand command = new SqlCommand(strSql, Connection);


SqlCommand cmd = new SqlCommand();
//cmd.Connection = con;
Connection.Open();
cmd.CommandText = "select * from news where news_no=@news_no";
cmd.Parameters.AddWithValue("@news_no", news_no);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{

this.Label1.Text = sdr["news_name"].ToString();
this.Label2.Text = sdr["news_infor"].ToString();

}
sdr.Close();
}


}
}
错误提示说上下文不存在名称strSql,我该怎么改呢
zqc2010 2012-05-14
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class USER_shownews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string news_no = Request.QueryString["news_no"];


string strConnection = "server=pc0313kzq\\Sqlexpress;database=ZQC2012;uid=ZQC2012;pwd=000000;";
SqlConnection Connection = new SqlConnection(strConnection);

SqlCommand command = new SqlCommand(strSql, Connection);


SqlCommand cmd = new SqlCommand();
//cmd.Connection = con;
Connection.Open();
cmd.CommandText = "select * from news where news_no=@news_no";
cmd.Parameters.AddWithValue("@news_no", news_no);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{

this.Label1.Text = sdr["news_name"].ToString();
this.Label2.Text = sdr["news_infor"].ToString();

}
sdr.Close();
}


}
}
调试说当前上下文不存在名称strSql........我不会改了。。。。
zqc2010 2012-05-14
  • 打赏
  • 举报
回复
谢谢你,我又试了,又有新问题了能帮我看看么[Quote=引用楼主 的回复:]
看了里面的好多帖子,研究了半天,自己也改了好多次,就是还不行。
只好再麻烦大家给我讲讲了。
我在之前的页中已经写好了按news_no跳转连接到shownewsPAGE的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="news_no" DataSource……
[/Quote]
孟子E章 2012-05-14
  • 打赏
  • 举报
回复
cmd.CommandText = "select * from news where news_no=@news_no";
cmd.Parameters.AddWithValue("@news_no",news_no);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.Read())
{

this.Label1.Text = sdr["news_name"].ToString();
this.Label2.Text = sdr["news_infor"].ToString();

}
sdr.Close();
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧