没想到在。NET里调用COOKIES这么麻烦,花了我大半天的时间还没弄好,只好来请教大家了我的问题是想用COOKIES来存放客户登录后的信息,如用户名和密码等,然后在首页加以判断是否登录了,现在我是用以下方法来读写COOKIE的:写入COOKIE:
HttpCookie cookie1=new HttpCookie("mcookies1");
cookie1.Values.Add("userid",uid);
cookie1.Expires=DateTime.Now.AddMinutes(10);
Response.AppendCookie(cookie1);读出并判断COOKIE是否有值,如果有即通过登录:
if(Request.Cookies["mcookies1"].Values["userid"]!=)
{  //通过后的代码  }现在我的问题是,如果第一次调用此页面,那么运行上面的读出语句就会出以下错误: “未将对象引用设置到对象的实例”应该就是说这个COOKIE没有值或者没定义好所以不能直接调用吧,不过只要登录过网站,COOKIE里有值的话,就不会出这个错,但如果新客户进站当然不可能去先登录啦,请问这个问题要如何解决?谢啦!

解决方案 »

  1.   

    if not Request.Cookies["mcookies1"].isnothing
      

  2.   

    if not isnothing(Request.Cookies("mcookies1")) then
    end if先要判断,如果不判断,当cookie是空的时候,操作它就出错了
      

  3.   

    protected const String COOKIE_ORDERINFO  = "OrderInfo";
            protected const String COOKIE_ORDERID    = "OrderID";
            protected const String COOKIE_STATUS     = "OrderStatus";        protected Int32 OrderID 
            {
                get 
                {
                    HttpCookie cookieOrder = Request.Cookies[COOKIE_ORDERINFO];
                    if( cookieOrder == null )
                        HasError();                return Int32.Parse( cookieOrder.Values[COOKIE_ORDERID]); 
                }
            }        public void SetOrderCookie( Int32 orderid, StatusCode orderstatus )
            {
                HttpCookie cookieOrder = Request.Cookies[COOKIE_ORDERINFO];
                if( Request[COOKIE_ORDERINFO] == null )
                    cookieOrder = new HttpCookie(COOKIE_ORDERINFO);
                cookieOrder.Values.Clear();
                cookieOrder.Values.Add( COOKIE_ORDERID, orderid.ToString() );
                cookieOrder.Values.Add( COOKIE_STATUS, orderstatus.ToString() );
                Response.AppendCookie( cookieOrder);
            }        protected StatusCode OrderStatus
            {
                get 
                {
                    HttpCookie cookieOrder = Request.Cookies[COOKIE_ORDERINFO];
                    if( cookieOrder == null )
                        HasError();                return HandleStatusCode.Parse( cookieOrder.Values[COOKIE_STATUS]); 
                }   
            }
      

  4.   

    谢谢两位啦:)不过 xyunsh(#烟雨平生#) 你能再说明一个这个函数要如何使用吗?
      

  5.   

    还有 bizbuy(也曾风流)说的,我由于是在C#下使用,所以这个 isnothing好像也用不了哦,C#下要如何判断呢?
      

  6.   

    HttpCookieCollection MyCookieCollection = Request.Cookies;
    int cookieCount = MyCookieCollection.Count;
    int i;
    for(i =  0; i < cookieCount; i++)
    {
    if (MyCookieCollection[i].Name == "...")
    {有你的设置...}
             else
             {没有你要的设置...}
    }