private void Page_Load(object sender, System.EventArgs e)
{
string newsid = Request.QueryString["uname"].ToString().Trim();
if(!this.IsPostBack)
{
if (Session["com_uname"]==null||Session["com_uname"].ToString()=="")
{
HyperLink1.Visible=false;
HyperLink2.Visible=false;
}
else
{
HyperLink1.Visible=true;
HyperLink2.Visible=true;
HyperLink1.NavigateUrl="company1/addfav.aspx?id="+newsid;
HyperLink2.NavigateUrl="company1/MAIL_SEND.aspx?reid="+newsid;
} SqlConnection MyConnection =new SqlConnection(ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
string strSel1 = "UPDATE person SET click=click+1 WHERE uname='"+newsid+"'";
SqlCommand myCommand= new SqlCommand("proc_per_list",MyConnection);
myCommand.CommandType=CommandType.StoredProcedure;
SqlParameter uid=new SqlParameter("@uname",SqlDbType.VarChar,10);
uid.Value=newsid;
myCommand.Parameters.Add(uid);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); 
SqlCommand myCommand1= new SqlCommand(strSel1,MyConnection);
MyConnection.Open();
myCommand1.ExecuteNonQuery();
myCommand.ExecuteNonQuery();
DataSet ds = new DataSet(); 
myAdapter.Fill( ds,"Table1" ); 
Repeater1.DataSource = ds.Tables["Table1"].DefaultView; 
Repeater1.DataBind(); 
MyConnection.Close(); }
---------------
将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误: 
行 33:  if (Session["com_uname"]==null||Session["com_uname"].ToString()=="")
行 34:  {
行 35:  HyperLink1.Visible=false;
行 36:  HyperLink2.Visible=false;
行 37:  }
 
去掉 判断SESSION那段IF ELSE 就正常显示 但是HyperLink1.Visible效果没有??????????????????

解决方案 »

  1.   

    没有定义变量吧!
    protected HyperLink Hyperlink1;
    protected HyperLink Hyperlink2;
      

  2.   

    protected System.Web.UI.WebControls.HyperLink HyperLink1;
    protected System.Web.UI.WebControls.HyperLink HyperLink2;
    protected System.Web.UI.WebControls.Repeater Repeater1;
    定义了!
      

  3.   

    Session["com_uname"].ToString()==""
    这一行错了,因为当Session为空时,是Tostring()方法是错误的!!!!
      

  4.   

    呵呵,Session 就是有时会不是null,但是其对象已被清除。所以上面的代码要改成:
    try {
      if(Session["com_uname"].ToString()=="") {
        HyperLink1.Visible=false;
      }
    }
    catch(Exception) {
      // Session["com_uname"] is null or is a invalid object
      HyperLink1.Visible=false;
    }
      

  5.   

    if (Session["com_uname"]==null||Session["com_uname"].ToString()=="")
    这句是错的,必须分开
    if(Session["com_uname"]==null)
    {
    }
    else if (Session["com_uname"].ToString()=="")
    {
    }
    else
    {
    }
      

  6.   

    c#语法中if (Session["com_uname"]==null||Session["com_uname"].ToString()=="")可以这样写的吧
    如果Session["com_uname"]==null成立,就直接返回true,而不会再去判断Session["com_uname"].ToString()==""了,也就不存在null调用tostring()方法的错误了
      

  7.   

    你在session_state里面设置一下Session["com_uname"]="";就好了。
      

  8.   

    你在Session_Start里面设置一下Session["com_uname"]="";就好了。写错了。。不好意思。
      

  9.   

    session为空就会这样报错了if (Session["com_uname"]==null||Session["com_uname"].ToString()=="")这句只保留if (Session["com_uname"]==null)就对了,当然前提是你在保存SESSION之前要判断用户名不能为空
      

  10.   

    将对象引用设置到对象的实例
    这句话就知道问题了,跟踪下,肯定哪个地方有NULL值出现
      

  11.   

    即使LOGIN后 SESSION有值 浏览 问题同样全部代码
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Configuration;namespace job
    {
    /// <summary>
    /// person 的摘要说明。
    /// </summary>
    public class person : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.HyperLink HyperLink1;
    protected System.Web.UI.WebControls.HyperLink HyperLink2;



    protected System.Web.UI.WebControls.Repeater Repeater1;

    private void Page_Load(object sender, System.EventArgs e)
    {

    string newsid = Request.QueryString["uname"].ToString().Trim();

    if (Session["com_uname"]==null||Session["com_uname"].ToString()=="") {
    HyperLink1.Visible=false;
    HyperLink2.Visible=false;
    }

    else
    {

    HyperLink1.Visible=true;
    HyperLink2.Visible=true;
    HyperLink1.NavigateUrl="company1/addfav.aspx?id="+newsid;
    HyperLink2.NavigateUrl="company1/MAIL_SEND.aspx?reid="+newsid;

    }


    SqlConnection MyConnection =new SqlConnection(ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
    string strSel1 = "UPDATE person SET click=click+1 WHERE uname='"+newsid+"'";
    SqlCommand myCommand= new SqlCommand("proc_per_list",MyConnection);
    myCommand.CommandType=CommandType.StoredProcedure;
    SqlParameter uid=new SqlParameter("@uname",SqlDbType.VarChar,10);
    uid.Value=newsid;
    myCommand.Parameters.Add(uid); SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); 
    SqlCommand myCommand1= new SqlCommand(strSel1,MyConnection);
    MyConnection.Open();
    myCommand1.ExecuteNonQuery();
    myCommand.ExecuteNonQuery();
    DataSet ds = new DataSet(); 
    myAdapter.Fill( ds,"Table1" ); 
    Repeater1.DataSource = ds.Tables["Table1"].DefaultView; 
    Repeater1.DataBind(); 
    MyConnection.Close();




    }
    }
    }-------------
    <%@ Page language="c#" Codebehind="person.aspx.cs" AutoEventWireup="false" Inherits="job.person" %>
    <HTML>
    <HEAD>
    <title></title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <LINK href="images/[email protected]" type="text/css" rel="stylesheet">
    </HEAD>
    <BODY text="#000000" bgColor="#003366" leftMargin="0" topMargin="0">
    <asp:repeater id="Repeater1" runat="server">
    <ItemTemplate>

    <table id="AutoNumber62" borderColor="#111111" cellSpacing="0" cellPadding="0" width="770"
    align="center" bgColor="#ffffff" border="0">
    <tr>
    <td vAlign="top" width="768">
    <table id="AutoNumber64" borderColor="#111111" cellSpacing="0" cellPadding="0" width="710"
    align="center" border="0">
    <tr>
    <td width="100%"><IMG src="IMAGES/persons.gif" border="0"></td>
    </tr>
    </table>
    <table id="AutoNumber65" borderColor="#111111" cellSpacing="0" cellPadding="0" width="710"
    align="center" bgColor="#1168a9" border="0">
    <t<td width="81" bgColor="#ffffff"></td>
    <td width="629">
    <table id="AutoNumber66" borderColor="#000000" cellSpacing="0" cellPadding="0" width="100%"
    border="0">
    <tr>
    <td vAlign="top" width="100%" bgColor="#ffffff">
    <table id="AutoNumber68" borderColor="#111111" cellSpacing="0" cellPadding="6" width="100%"border="0">
    <tr>
    <td width="50%" bgColor="#2c8ed3"><font color="#ffffff">个人基本资料:</font></td>
    <td align="right" width="50%" bgColor="#2c8ed3"><font color="#ffffff">以被查看【
    <%# DataBinder.Eval(Container.DataItem,"click").ToString() %>
    】次</font></td>
    </tr>
    </table>
    <table id="AutoNumber69" borderColor="#2c8ed3" cellSpacing="0" cellPadding="6" width="100%"bgColor="#d2eafb" border="1">
    <tr>
    <td align="right" width="16%">姓名:</td>
    <td width="21%">
    <%# DataBinder.Eval(Container.DataItem,"iname").ToString() %>
    </td>
    <td align="right" width="18%">性别:</td>
    <td width="21%">
    <%# DataBinder.Eval(Container.DataItem,"sex").ToString() %>
    </td>
    <td vAlign="middle" align="center" width="24%" rowSpan="6"><img height=149 
    src='<%# DataBinder.Eval(Container.DataItem,"pic").ToString() %>' 
     width=128 border=0>
    </td>
    </tr>
    <tr>
    <td align="right" width="16%">年龄:</td>
    <td width="21%">
    <%# DataBinder.Eval(Container.DataItem,"nianling").ToString() %>
    </td>
    <td align="right" width="18%">民族:</td>
    <%# DataBinder.Eval(Container.DataItem,"mzhu").ToString() %>
    </td>
    </tr>
    <tr>
    <td align="right" width="16%">出生日期:</td>
    <td width="21%">
    <%# DataBinder.Eval(Container.DataItem,"bday").ToString() %>
    </td>
    <td align="right" width="25%">相关工作经验:</td>
    <td width="25%">
    <%# DataBinder.Eval(Container.DataItem,"gznum").ToString() %>
    年</td>
    </tr>
    <tr>
    <td align="right" width="25%">外语语种:</td>
    <td width="25%">
    <%# DataBinder.Eval(Container.DataItem,"language").ToString() %>
    </td>
    <td align="right" width="25%">外语水平:</td>
    <td width="25%">
    <%# DataBinder.Eval(Container.DataItem,"lanlevel").ToString() %>
    </td>
    </tr>
    <tr>
    <td align="right" width="25%">普通话程度:</td>
    <td width="25%">
    <%# DataBinder.Eval(Container.DataItem,"pthua").ToString() %>
    </td>
    <td align="right" width="25%">计算机能力:</td>
    <td width="25%">
    <%# DataBinder.Eval(Container.DataItem,"computer").ToString() %>
    </td>
    </tr>
    <tr>
    <td align="center" width="100%" colSpan="4">个人详细工作经历:</td>
    </tr>
    <tr>
    <td width="100%" colSpan="4">
    <%# DataBinder.Eval(Container.DataItem,"gzjl").ToString() %>
    </td>
    </tr>
    </table>
    <table id="AutoNumber71" borderColor="#2c8ed3" cellSpacing="0" cellPadding="6" width="100%"
    bgColor="#d2eafb" border="1">
    <tr>
    <td width="100%" bgColor="#2c8ed3" colSpan="4"><font color="#ffffff">求职意向:</font></td>
    </tr>
    <tr>
    <td align="right" width="20%">求职类型:</td>
    <td width="34%">
    <%# DataBinder.Eval(Container.DataItem,"jobtype").ToString() %>
    </td>
    <td align="right" width="17%">月薪要求:</td>
    <td width="29%">
    <%# DataBinder.Eval(Container.DataItem,"yuex").ToString() %>
    </td>
    </tr>
    <tr>
    <td align="right" width="20%">应聘职位名称1:</td>
    <td width="34%">
    <%# DataBinder.Eval(Container.DataItem,"jobs").ToString() %>
    </td>
    <td align="right" width="17%">应聘职位名称2:</td>
    <td width="29%">
    <%# DataBinder.Eval(Container.DataItem,"job1").ToString() %>
    </td>
    </tr>
    </tr>
    </table>
    <table id="AutoNumber72" borderColor="#111111" cellSpacing="0" cellPadding="12" width="100%"border="0">
    <tr>
    <td align="center">
    <asp:HyperLink id="HyperLink1" runat="server" ImageUrl="IMAGES/jl1.gif">HyperLink</asp:HyperLink></td>
    <td align="center">
    <asp:HyperLink id="HyperLink2" runat="server" ImageUrl="IMAGES/jl3.gif">HyperLink</asp:HyperLink></td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td width="81" bgColor="#ffffff"></td>
    <td width="629" bgColor="#ffffff"></td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:repeater>
    </BODY>
    </HTML>
    ????????????????????????