Asp.net mvc 4 在何处应用控制器中的try-catch';MVC4中的角色是什么?
下面是我的代码,其中用户被插入到数据库中。 请告诉我在哪里应用try-catch块,以防止在此场景中发生任何类型的异常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
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块中捕获连接到数据库时的异常