'SaveUser(string)':并非所有代码路径都返回一个值

时间:2017-08-13 23:48:56

标签: c# asp.net

请帮忙!这是我需要解决的最后一个错误。“SaveUser”收到错误'clsDataLayer.SaveUser(string,string,string,string)':并非所有代码路径都返回值

// This function saves the user login
    public static bool SaveUser(string Database, string UserName, string UserPassword, string SecurityLevel)
    {
        bool recordSaved;

        try
        {
            // Add your comments here
            OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + Database);

            conn.Open();
            OleDbCommand command = conn.CreateCommand();
            string strSQL;

            strSQL = "Insert into tblUserLogin (UserName, UserPassword, SecurityLevel) values ('" +
            UserName + "', '" + UserPassword + "', '" + SecurityLevel + "')";

            command.CommandType = CommandType.Text;
            command.CommandText = strSQL;

            command.ExecuteNonQuery();

            conn.Close();
            recordSaved = true;



        }
        catch (Exception ex) { }

}

以下是此过程的if else

protected void btnAddUser_Click(object sender, EventArgs e)
    {
        if (clsDataLayer.SaveUser(Server.MapPath("PayrollSystem_DB.accdb"),
UserName.Text, UserPassword.Text, ddlSecurityLevel.SelectedValue))
        {

            lblDisplay.Text = "The user was successfully added!";
            grdUsers.DataBind();
        }

        else
            lblDisplay.Text = "The user was not added!";


    }

1 个答案:

答案 0 :(得分:1)

你没有归还任何东西。而不是

recordSaved = true;

你可以这样做:

return true;

同样,当您捕获异常时,您还需要从catch块返回一些内容,例如

catch (Exception ex) { return false; }