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;


        }


    }
   
}

Recommended Answers

All 2 Replies

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;
    }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.