Paul
Paul

Reputation: 15

Not all paths return a value in Public ActionResult , I used Exception

Basically when i do this

public ActionResult ValidateLogin()
{
    return View();
}

This works fine now When i use say a try catch block in this case and i do something like return RedirectToAction("Dashboard"); in try catch blocks, i am also expecting that it should check the designation, before it redirects the user to the main page, it fires and Error like not all paths return a value.

My source code looks like this

public ActionResult ValidateLogin(UserAuthClass auth)
{
    string constring = @"Data Source=DESKTOP-9CM4N5S\SQLEXPRESS;Initial Catalog=MVCLogintestDB;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();

        string query = "select * from [MVCLogintestDB].[dbo].[users_table] where username = @username and password= @password and designation = @designation";
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@username",auth.username);
            cmd.Parameters.AddWithValue("@password", auth.password);
            cmd.Parameters.AddWithValue("designation", auth.designation);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            try
            {
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    if (auth.designation == "Admin")
                    {
                        return RedirectToAction("AdminDashboard");
                    }
                    else if (auth.designation == "Security")
                    {
                        return RedirectToAction("SecurityDashboard");
                    }
                    else if (auth.designation == "Visitor")
                    {
                        return RedirectToAction("VisitorDashboard");
                    }
                }
                else
                {
                    return RedirectToAction("WrongPasswordArea");
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}

I am trying to understand what is the wrong thing I appear to be missing here?

Upvotes: 0

Views: 172

Answers (3)

LucasM
LucasM

Reputation: 308

Set a return in your catch. I think this would be better for you to use rather than returning outside of your using block because from the return in your using you can start to show errors from this return instead of a general return outside that looks like nothing happened.

Upvotes: 0

Pedro Rodrigues
Pedro Rodrigues

Reputation: 2658

Do a refactor on your if-else-if logic. I implemented a simpler version and the result is your error is gone.

You will have to adapt, as I did to test this in a quick environment. But just go to the guts, the if-else-if, and make it more logical. Again no error about missing returns in the code bellow.

public string ValidateLogin(string auth)
{
    using (new NoDispose())
    {
        using (new NoDispose())
        {
            try
            {
                int dtRowsCount = 10;
                string authDesignation = "";
                if (dtRowsCount <= 0)
                    return "WrongPasswordArea";
                switch (authDesignation)
                {
                    case "Admin":
                        return "AdminDashboard";
                    case "Security":
                        return "SecurityDashboard";
                    case "SecurityDashboard":
                        return "SecurityDashboard";
                    case "Visitor":
                    default:
                        return "VisitorDashboard";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Disclaimer. I do not claim that the logic implemented above does exactly what the previous did, nor that it conforms to your requirements.

Using chains

And btw, you can chain using statements in the following manner.

using (var a = new NoDispose())
using (var b = new NoDispose())
{
    // both a and b are available in here
}

Upvotes: 1

Jerry Phillips
Jerry Phillips

Reputation: 31

You need to have a default return outside of your using block. Something like this

return RedirectToAction("Login");

When all scenarios fail, return the user to the Login page or whatever your Login page is called.

Upvotes: 0

Related Questions