第一,你第二个页面中 newsid=Request.Params["id"];取得 的 newsid的值 是多少?第二, string strCom="select * from news where id="+newsid;news表中的字段是什么类型的? int or char or .....???
news 表中的字段为 int 我第二个页面中 newsid=Request.Params["id"];取得 的 newsid的值 是我要点选择的内容~你可以看一下我的第一个页面~我把他也发上了~~你看一看~
ID 应该为整型 另外此处代码有问题reader.Read();//是否返回为true 应该判断 int i=reader.GetInt32(0); i++; reader.Close(); strCon="update news set counter="+i.ToString()+"where (id="+newsid+")";count为自动编号类型, 不需要你给它赋值,当插入一条记录时它会自动增加,人为的去加1,简直就是多事,而且还会出现不可预料的错误
如果要这句话的话,strCon="update news set counter="+i.ToString()+"where (id="+newsid+")";中间缺少空格应该为strCon="update news set counter="+i.ToString()+" where (id="+newsid+")";
不行吗?
<%@ Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>网站新闻发布系统</title>
<Script Language="C#" runat="server">DataSet ds;
DataRow dr;
string newsid;
void Page_Load(Object sender,EventArgs e)
{
SqlConnection myConnection;
string myConnString=@"server=localhost;uid=sa;pwd= ;database=test ";
string connStr,strCon;
newsid=Request.Params["id"];
myConnection=new SqlConnection(myConnString);
myConnection.Open();
string strCom="select * from news where id="+newsid;
ds=new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(strCom,myConnection);
myCommand.Fill(ds,"news");
dr=ds.Tables["news"].Rows[0];
strCon="select counter from news where id="+newsid;
SqlCommand myCommand2=new SqlCommand(strCon,myConnection);
SqlDataReader reader=myCommand2.ExecuteReader();
reader.Read();
int i=reader.GetInt32(0);
i++;
reader.Close();
strCon="update news set counter="+i.ToString()+"where (id="+newsid+")";
myCommand2.CommandText=strCon;
myCommand2.ExecuteNonQuery();
myConnection.Close();
}
</Script>
</head>
<body>
<div align="center">新闻发布系统</div>
<%=dr["biaoti"]%>
<p>
<%=dr["shijian"]%>
<p>
<%=dr["neirong"]%>
<p>
摘自:<%=dr["zhaizi"]%>
<p>
本条消息被浏览了[<%=dr["Counter"]%>回
<p align="right"><a href="javascript:window.close()">关闭窗口</a></p>
</body>
</html>修改后的错误:'`' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '`' 附近有语法错误。
我第二个页面中 newsid=Request.Params["id"];取得 的 newsid的值 是我要点选择的内容~你可以看一下我的第一个页面~我把他也发上了~~你看一看~
另外此处代码有问题reader.Read();//是否返回为true 应该判断
int i=reader.GetInt32(0);
i++;
reader.Close();
strCon="update news set counter="+i.ToString()+"where (id="+newsid+")";count为自动编号类型, 不需要你给它赋值,当插入一条记录时它会自动增加,人为的去加1,简直就是多事,而且还会出现不可预料的错误
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '`' 附近有语法错误。
我呢一直没有找出来~如果说我不用传递值 ,直接给id 加一个号能显示信息~怎么回事~
看看 newsid 到底是多少?
<asp:HyperLinkColumn
HeaderText="标题"
DataNavigateUrlFormatString="show2.aspx?id={0}"
DataNavigateUrlField="biaoti"
DataTextField="biaoti"
Target="_new"
/>
biaoti ?????????????? 不是整型的,当然会出错了
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '~' 附近有语法错误。源错误: 不行啊~
biaoti是整型的
AspNetPager 免费分页控件4.2版发布,同时发布最新源代码,欢迎下载:http://www.webdiyer.com
关键这句select语句:
改成:
string strCom="select * from news where id=@newsid";
SqlParameter parm = new SqlParameter("@newsid",SqlDbType.NChar ,10);
//这里的SqlDbTyp根据你库中newsid字段类型,10为字段长度~~~
parm.Value = newsid ;
myCommand2.Parameters.Add(parm);
就是通过biaoti获取id 的值~
<%@ Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>网站新闻发布系统</title>
<Script Language="C#" runat="server">DataSet ds;
DataRow dr;
string newsid;
void Page_Load(Object sender,EventArgs e)
{
SqlConnection myConnection;
string myConnString=@"server=localhost;uid=sa;pwd= ;database=test ";
string connStr,strCon;
//newsid=Request.Params["id"];
newsid=Request.QueryString["id"];
//Response.Write(newsid);myConnection=new SqlConnection(myConnString);
myConnection.Open();
string strCom="select * from news where id="+newsid;
ds=new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(strCom,myConnection);
myCommand.Fill(ds,"news");
dr=ds.Tables["news"].Rows[0];
strCon="select counter from news where id="+newsid;
SqlCommand myCommand2=new SqlCommand(strCon,myConnection);
SqlDataReader reader=myCommand2.ExecuteReader();
reader.Read();
int i=reader.GetInt32(0);
i++;
reader.Close();
strCon="update news set counter="+i.ToString()+" where (id="+newsid+")";
myCommand2.CommandText=strCon;
myCommand2.ExecuteNonQuery();
myConnection.Close();
}
</Script>
</head>
<body>
<div align="center">新闻发布系统</div>
<%=dr["biaoti"]%>
<p>
<%=dr["shijian"]%>
<p>
<%=dr["neirong"]%>
<p>
摘自:<%=dr["zhaizi"]%>
<p>
本条消息被浏览了[<%=dr["Counter"]%>回
<p align="right"><a href="javascript:window.close()">关闭窗口</a></p>
</body>
</html>看一看~~
这是要接受的id
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<html>
<head>
<title>网站新闻发布系统</title>
<Script Language="C#" runat="server">
int startIndex;
void Page_Load(Object sender,EventArgs e)
{
if(!IsPostBack)
{
startIndex=0;
}
Binding();
}
void Binding()
{
string mySelect;
string myConnString=@"server=localhost;uid=sa;pwd= ;database=test ";
SqlConnection myConnection = new SqlConnection(myConnString);
myConnection.Open();
mySelect ="SELECT id,biaoti,zhaizi,counter,shijian FROM news ORDER BY id DESC";
DataSet myDataSet= new DataSet();
SqlDataAdapter myCommand=new SqlDataAdapter(mySelect,myConnection);
myCommand.Fill(myDataSet,"news");
DataGrid1.DataSource=myDataSet.Tables["news"].DefaultView;
DataGrid1.DataBind();
}
void ChangePage(Object sender,DataGridPageChangedEventArgs e)
{
startIndex=e.NewPageIndex*DataGrid1.PageSize;
DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid1.DataBind();
}
</script>
</head>
<body>
<form runat="server">
<p align="center"><font size="+3">新闻浏览</font>
<asp:DataGrid id="DataGrid1"
runat="server"
ForeColor="Black"
PagerStyle_Mode="NumericPages"
OnPageindexChanged="ChangePage"
PageSize="10"
Width="80%"
AllowPaging="True"
Font-Names="Verdana"
Font-Size="8pt"
autogeneratecolumns="False">
<Columns>
<asp:TemplateColumn>
<a href='show2.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>'><%#DataBinder.Eval(Container.DataItem,"biaoti")%></a>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText=“摘自" itemstyle-width=10% DataField=zhaizi/>
<asp:BoundColumn HeaderText=“点击次数" itemstyle-width=20% DataField=counter/>
<asp:BoundColumn HeaderText=“发表日期" itemstyle-width=20% DataField=shijian/>
</Columns>
</asp:DataGrid>
</form>
</body>
</Html>