前台界面代码:
<%@ 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>-&gt;系部介绍
            </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>
            &nbsp; &nbsp; &nbsp; &nbsp;
            <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 

解决方案 »

  1.   

    Request.QueryString["id"]是通过地址栏的参数获取值,如果你的地址栏里没有参数,就会取不到值,就会报未将对象引用设置到对象的实例错误。改成
    if(Request.QueryString["id"]!=null)
    {
    int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());
    }
      

  2.   


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

  3.   

    C# code是啥?应该在哪改?
      

  4.   

    把您的代码int newsid = Convert.ToInt32(Request.QueryString["id"].ToString());用
    int newsid = Convert.ToInt32(Request.QueryString["Id"] == null ? 1 : Request.QueryString["id"]);
    替换掉顺便百度一下 三元运算符
      

  5.   

    这样也不行。
      上面提示
    编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: 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 
      

  6.   

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