Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 在何处应用控制器中的try-catch';MVC4中的角色是什么?_Asp.net Mvc 4_Try Catch_Sqlconnection - Fatal编程技术网

Asp.net mvc 4 在何处应用控制器中的try-catch';MVC4中的角色是什么?

Asp.net mvc 4 在何处应用控制器中的try-catch';MVC4中的角色是什么?,asp.net-mvc-4,try-catch,sqlconnection,Asp.net Mvc 4,Try Catch,Sqlconnection,下面是我的代码,其中用户被插入到数据库中。 请告诉我在哪里应用try-catch块,以防止在此场景中发生任何类型的异常 if (ModelState.IsValid) { user.Type = 'J'; user.DateOfJoining = DateTime.Now; using (SqlConnection connection = new SqlConnection(WebConfiguration

下面是我的代码,其中用户被插入到数据库中。 请告诉我在哪里应用try-catch块,以防止在此场景中发生任何类型的异常

if (ModelState.IsValid)
        {
            user.Type = 'J';
            user.DateOfJoining = DateTime.Now;
            using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnectionString"].ToString()))
            {
                using (SqlCommand command = new SqlCommand("SignupUser", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@FirstName",user.FirstName);
                    command.Parameters.AddWithValue("@LastName",user.LastName);
                    command.Parameters.AddWithValue("@Email",user.Email);
                    command.Parameters.AddWithValue("@Password",user.Password);
                    command.Parameters.AddWithValue("@Mobile",user.Mobile);
                    command.Parameters.AddWithValue("@City",user.City);
                    command.Parameters.AddWithValue("@StateProvince",user.StateProvince);
                    command.Parameters.AddWithValue("@Country",user.Country);
                    command.Parameters.AddWithValue("@Type",user.Type);
                    command.Parameters.AddWithValue("@DateOfJoining",user.DateOfJoining);
                    connection.Open();
                    int result = command.ExecuteNonQuery();
                    connection.Close();
                    return RedirectToAction("account");
                }
            }
        }

if块中的所有代码都应该在try-catch块中,以便在第一个using块中捕获连接到数据库时的异常