| ■39701 / inTopicNo.7) | 
               Re[1]: ASP.NET:Cookie機能の使い方 | 
             
          
         
        
        □投稿者/ Lisa (7回)-(2009/08/12(Wed) 15:07:37)  
        
          
            
              
                 | 
                ASP.NET勉強の例題としてアクセスカウンタを作ってます。
ですが、Cookie情報がなかった場合の処理のcok.Values("cokcount") = countの行で
「オブジェクト参照がオブジェクト インスタンスに設定されていません。」のエラー
が発生し動きません。
どこが問題なんでしょうか?よろしくお願いします。
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        'ポストバック表示でない場合
        If Not Page.IsPostBack Then
            'アクセスカウンタ
            Dim sr As String = Server.MapPath("count.txt")
            Dim cokcount As String
            If System.IO.File.Exists(sr) Then
                'count.txtファイルが既に存在する場合
                'countデータの読込み
                Dim count As String = System.IO.File.ReadAllText(sr)
                'Cookieハンドルの定義
                Dim cok As HttpCookie = Request.Cookies("CokCheck")
                
                If Not (cok Is Nothing) Then
                    'Cookie情報がある場合
                    cokcount = cok.Values("cokcount")
                    If cokcount <> count Then
                        'ページが再表示でなかった場合
                        count += 1
                        Label1.Text = count
                        System.IO.File.WriteAllText(sr, count)
                        cok.Values("cokcount") = count
                        cok.Expires = DateTime.Now.AddSeconds(60)
                        Response.Cookies.Add(cok)
                    End If
                Else
                    'Cookie情報がなかった場合
                    count += 1
                    Label1.Text = count
                    System.IO.File.WriteAllText(sr, count)
                    cok.Values("cokcount") = count '←ここで「オブジェクト参照がオブジェクト インスタンスに設定されていません。」のエラー
                    cok.Expires = DateTime.Now.AddSeconds(60)
                    Response.Cookies.Add(cok)
                End If
            Else
                'conut.txtファイルが存在しない場合
                'Cookieハンドルの定義
                Dim cok As HttpCookie = Request.Cookies("CokCheck")
                Dim count As String
                count = 1
                Label1.Text = count
                System.IO.File.WriteAllText(sr, count)
                cok.Values("cokcount") = count
                cok.Expires = DateTime.Now.AddSeconds(60)
                Response.Cookies.Add(cok)
            End If
        End If
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無題のページ</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        アクセスカウンタ 09/08/12<br />
        <br />
        閲覧数:
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>
  | 
               
            
           
         
        
       |