各位大侠。帮我看看这程序出现了什么问题。。?

zheng_unhappy 2011-04-17 05:43:33
前台界面代码:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ReadNews.aspx.cs" Inherits="ReadNews" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript" type="text/javascript">
<!--

function TABLE1_onclick() {

}

// -->
</script>
<table border="0" cellpadding="0" cellspacing="0" style="border-right: #cccccc 1px solid;
border-top: #cccccc 1px solid; border-left: #cccccc 1px solid; width: 95%;
border-bottom: #cccccc 1px solid" id="TABLE1" language="javascript" onclick="return TABLE1_onclick()" align=center>
<tr>
<td style="width: 100%; border-bottom: #00ff33 1px solid; height: 14px; background-color: #cccccc;" align=left>
您的位置:<asp:Label ID="lbl_class" runat="server"></asp:Label>->系部介绍
</td>
</tr>
<tr>
<td style="width: 100%; height:15px">
<table border="0" cellpadding="0" cellspacing="0" style="width: 60%" align=center>
<tr>
<td style="border-bottom: #ffcc00 1px solid; height: 17px" align=center>
<asp:Label ID="lbl_title" runat="server"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="width: 100%; height:14px" align=center>
<asp:Label ID="lbl_info" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 100px; height: 16px;">
</td>
</tr>
<tr>
<td style="width: 100%; height:14px" align=left>
       
<asp:Label ID="lbl_content" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Content>




后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ReadNews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());

string setting = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServives"]);

SqlConnection conn = new SqlConnection(setting);

conn.Open();
string sql = "select A.Title,A.Content,A.Time,A.Author,A.Readcishu, B.pastemName from Body A ,Pastem B Where B.pastemID = A.pastemID And A.ID =" + newsid;

SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lbl_class.Text = dr["pastemName"].ToString();
lbl_content.Text = dr["Content"].ToString();
lbl_title.Text = dr["Title"].ToString();
lbl_info.Text = "作者:" + dr["Author"].ToString() + " 时间:" + dr["Time"].ToString() + "阅读:" + dr["Readcishu"].ToString() + "次";
}
int m = Convert.ToInt32(dr["Readcishu"].ToString()) + 1;
conn.Close();
UpdataPo(newsid, m);
}
public void UpdataPo(int id, int m)
{
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);

SqlConnection myconn = new SqlConnection(settings);

myconn.Open();
string sql = "Update Body Set ReadCishu=" + m + " Where ID = " + id;

SqlCommand mycmd = new SqlCommand(sql, myconn);
mycmd.ExecuteNonQuery();
}
}

错误信息:
“/WebSite1”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 15: protected void Page_Load(object sender, EventArgs e)
行 16: {
行 17: int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());
行 18:
行 19: string setting = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServives"]);


源文件: d:\Documents\Visual Studio 2005\WebSites\WebSite1\ReadNews.aspx.cs 行: 17

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
ReadNews.Page_Load(Object sender, EventArgs e) in d:\Documents\Visual Studio 2005\WebSites\WebSite1\ReadNews.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
System.Web.UI.Control.OnLoad(EventArgs e) +73
System.Web.UI.Control.LoadRecursive() +52
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2184



...全文
105 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zheng_unhappy 2011-04-18
  • 打赏
  • 举报
回复
没有..不会默认为0,因为在地址栏里我有参数.
Lost_c 2011-04-17
  • 打赏
  • 举报
回复
纠正,目前开发用的是4.0 4.0以前的没太在意。不知道会不会默认为0!
Lost_c 2011-04-17
  • 打赏
  • 举报
回复
从堆栈信息看,应该不是参数问题。 如果参数为空默认得到应该是0;所以不会报错。 应该是没得到连接字符串的原因。你打个断点看看呢?
Lost_c 2011-04-17
  • 打赏
  • 举报
回复
int newsid = Convert.ToInt32(Request.QueryString["Id"] == null ? "1" : Request.QueryString["id"]);
zheng_unhappy 2011-04-17
  • 打赏
  • 举报
回复
这样也不行。
上面提示
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。

编译器错误消息: CS0173: 无法确定条件表达式的类型,因为“int”和“string”之间没有隐式转换

源错误:



行 15: protected void Page_Load(object sender, EventArgs e)
行 16: {
行 17: int newsid = Convert.ToInt32(Request.QueryString["id"] == null ? 1 : Request.QueryString["id"]);
行 18:
行 19: string setting = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServives"]);


源文件: d:\Documents\Visual Studio 2005\WebSites\WebSite1\ReadNews.aspx.cs 行: 17

子夜__ 2011-04-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zheng_unhappy 的回复:]

C# code是啥?应该在哪改?
[/Quote]
把您的代码int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());


int newsid = Convert.ToInt32(Request.QueryString["Id"] == null ? 1 : Request.QueryString["id"]);
替换掉

顺便百度一下 三元运算符
zheng_unhappy 2011-04-17
  • 打赏
  • 举报
回复
C# code是啥?应该在哪改?
zheng_unhappy 2011-04-17
  • 打赏
  • 举报
回复
因为我是新手。所以麻烦各位大侠说清楚点!
子夜__ 2011-04-17
  • 打赏
  • 举报
回复

int newsid = Convert.ToInt32(Request.QueryString["Id"] == null ? 1 : Request.QueryString["id"]);
huiyuan659 2011-04-17
  • 打赏
  • 举报
回复
Request.QueryString["id"]是通过地址栏的参数获取值,如果你的地址栏里没有参数,就会取不到值,就会报未将对象引用设置到对象的实例错误。

改成
if(Request.QueryString["id"]!=null)
{
int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());
}
Teng_s2000 2011-04-17
  • 打赏
  • 举报
回复
*****.aspx?newsid=100

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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