简体   繁体   中英

ASP.NET MVC 4 An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

I'm using ASP.NET MVC4 When I'm trying to run my program that problem always pop up.

在此处输入图片说明

public class UsersService
{
    // Db Connection string
    string DBCon = ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString;

    //View users list
    public List<UsersModel> All()
    {
        List<UsersModel> _UsersModel = new List<UsersModel>();

        using (SqlConnection conn = new SqlConnection(DBCon))
        {
            //call stored procedure
            using (SqlCommand cmd = new SqlCommand("GetUsers", conn))  
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                conn.Open();

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    UsersModel _UserModel = new UsersModel();
                    _UserModel.UserID = int.Parse(reader["UserID"].ToString());
                    _UserModel.Firstname = reader["Firstname"].ToString();
                    _UserModel.Middlename = reader["Middlename"].ToString();
                    _UserModel.Surname = reader["Surname"].ToString();
                    _UserModel.Email = reader["Email"].ToString();

                    _UsersModel.Add(_UserModel);
                }
            }
        }

        return _UsersModel;
    }
}

there is a problem with the connection to the database. The main reason for this... sorry, it could be a network issue, firewall, rejected port, wrong details, etc... What I would do: try try and catch to manage the exception and see the error that you are getting, For more info: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM