public string generateInvoiceNo(string PRODUCTNAME)
            {
                try
                {
                    if (con.State != ConnectionState.Open)
                    {

                        con.Open();
                        string number;
                        OleDbDataAdapter DA1 = new OleDbDataAdapter("SELECT MAX(SUBSTR(INVOICE_NO,9,5)+1) AS MAXINVOICE FROM PRODUCT_DETAILS", con);
                        DataTable DT1 = new DataTable();
                        DA1.Fill(DT1);
                        if (DT1.Rows.Count > 0)
                        {
                            number = DT1.Rows[0]["MAXINVOICE"].ToString();
                        }
                        else
                        {
                            number = "00001";
                        }
                        OleDbCommand cmd2 = new OleDbCommand("SELECT SUBSTR(D_NO,0,2) AS D_NO FROM PRODUCT_MASTER WHERE PRODUCT_NAME='" + PRODUCTNAME + "'", con);
                        OleDbDataAdapter DA2 = new OleDbDataAdapter(cmd2);
                        DataTable DT2 = new DataTable();
                        DA2.Fill(DT2);
                        string dno = DT2.Rows[0]["D_NO"].ToString() + System.DateTime.Today.ToString() + number;
                        return dno;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }
                finally
                {
                    con.Close();
                }
            }

It's possible to exit your method without returning a string, you need to reutnr one in the finally clause and you shouldn't return one in the catch clause (since the finally clause will being doing that).

If i return in finally clause, the it it will not be knowing about the string named in my case 'dno' will not be known to finally clause as i have done already. It is showing "The name dno doesn't exist in current context".
And if delete the return from catch, It shows same error as mentioned above "Not all code paths return value".

Then you need to declare dno outside the try block.

your if block inside try returns a string.. But there's no else block, if code execution doesn't enter inside if block then your method exits without returning any value !!. And thus compiler is giving the error.. so you have to include an else block as well, or do some workaround

Thank You ckchaudhary for the solution.

On every step of the way through the method the code must return some value, if it cannot continue from there.
So every if, else if and else must return it.

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.