日期:2014-05-17  浏览次数:20408 次

asp.net网站计数器的问题。
是这样的,我做了一个网站计数器,要将访问人数保存在本地文件counter.txt里。
这是代码,可是运行的时候一直报错。
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "使用Session对象设计网站计数器";
        string FilePath = Server.MapPath("counter.txt");
        StreamReader mystr = new StreamReader(FilePath);
        int count = int.Parse(mystr.ReadLine());
        mystr.Close();

        if (Session["counter"] == null)
        {
            Session["counter"] = "";
            count = count + 1;
            StreamWriter s = new StreamWriter(FilePath);
            s.WriteLine(count);
            s.Close();
        }
        Response.Write("当前SessionID值为:" + Session.SessionID + "<br>");
        Response.Write("您是本站第" + count.ToString() + "位访问者");
    }





这是报错信息。:


------解决方案--------------------
int count = int.Parse(mystr.ReadLine());
修改如下,当不能正常转换或者读取值为空时默认为0
int count=0;
                int.TryParse(mystr.ReadLine(),out count);

------解决方案--------------------
直接用asp的吧,.net也是可以直接用的

http://download.csdn.net/detail/liuchaolin/4664127


------解决方案--------------------
引用:
int count = int.Parse(mystr.ReadLine());
修改如下,当不能正常转换或者读取值为空时默认为0
int count=0;
                int.TryParse(mystr.ReadLine(),out count);

正解 在编程中 我们要时刻注意你操作的对象是否为空的时候 平时都注意点 这样可以减少不少bug
------解决方案--------------------
很明显你没有读到数据啊,没读到就返回null,然后你对null 进行操作就报错了。
做个判断,当读不到时就给个值就完了。这个程序的必要的判断啊。