0
protected void Page_Load(object sender, EventArgs e)
    {
        int nCount = 0;
        nCount = GetCounterValue();

        txtDate.Text = DateTime.Now.ToShortDateString() + "---" + DateTime.Now.ToShortTimeString();
        txtAgentName.Text = User.Identity.Name;
    }




    private int GetCounterValue()>>>HERE IS THE ERROR not all path code return value
    {
        StreamReader ctrFile;
        FileStream ctrFileW;
        StreamWriter sw;

        string Path = Server.MapPath("Counter.txt");
        string CounterContents;
        int nCounter;
        if (File.Exists(Path))
        {
            ctrFile = File.OpenText(Path);
            CounterContents = ctrFile.ReadLine().ToString();
            ctrFile.Close();
            nCounter = Convert.ToInt32(CounterContents);
        }
        else
        {
            nCounter = 0;
            nCounter++;
            ctrFileW = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
            sw = new StreamWriter(ctrFileW);
            sw.WriteLine(Convert.ToString(nCounter));
            sw.Close();
            ctrFileW.Close();

            return nCounter;


        }


    }
   
}

Edited by jamshed ahmed: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Sals
0

Put the return statement out of else block in GetCounterValue() method.

private int GetCounterValue()
    {
        StreamReader ctrFile;
        FileStream ctrFileW;
        StreamWriter sw;

        string Path = Server.MapPath("Counter.txt");
        string CounterContents;
        int nCounter;
        if (File.Exists(Path))
        {
            ctrFile = File.OpenText(Path);
            CounterContents = ctrFile.ReadLine().ToString();
            ctrFile.Close();
            nCounter = Convert.ToInt32(CounterContents);
        }
        else
        {
            nCounter = 0;
            nCounter++;
            ctrFileW = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
            sw = new StreamWriter(ctrFileW);
            sw.WriteLine(Convert.ToString(nCounter));
            sw.Close();
            ctrFileW.Close();
        }
   return nCounter;
    }
0

use return statement in both if and else
or write it after else only.

If you don't want to return anything use return 0 wherever required.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.