当插入数据库时​​,Asp.net没有发生任何数据插入

时间:2017-03-02 14:35:14

标签: c# sql asp.net

这是我的UserRegister.cs模型

public class UserRegister
    {
        public int Id { get; set; }
        [Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
        public string Username { get; set; }
        [Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
        [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
        [StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
        public string Password { get; set; }
        [Compare("Password", ErrorMessage = "Confirm password dose not match.")]
        [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
        public string ConfirmPassword { get; set; }
        [Required(ErrorMessage = "Please provide full name", AllowEmptyStrings = false)]
        public string FullName { get; set; }

        [RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
            ErrorMessage = "Please provide valid email id")]
        public string Email { get; set; }
    }

这是我的Customercontroller,我在其中执行将数据库插入其中的函数。

public ActionResult Customer()
        {
          return View(new UserRegister());
        }

        [HttpPost]
        public ActionResult Customer(UserRegister model1)
        {
            if (ModelState.IsValid)
            {
                SqlConnection cnn;
                string connectionString = ConfigurationManager.ConnectionStrings
                                            ["DefaultConnection"].ConnectionString;
                cnn = new SqlConnection(connectionString);
               try
            {
                cnn.Open();
                Response.Write("Connection Made");




                string query = "INSERT INTO Customer(Username,Password,Email) VALUES('" + model1.Username + "','" + model1.Password + "','" + model1.Email + "')";
                SqlCommand command = new SqlCommand(query, cnn);
                command.ExecuteNonQuery();

            }

            catch
            {
                Response.Write("error");

            }

            finally
            {
                cnn.Close();

            }

                Response.Write("Insert succesful");

                ViewBag.Message = "Customer Page";
            }
                return View(model1);

        }

当我插入数据库时​​没有任何事情发生,没有插入数据。我想问一下,有什么东西需要添加到我的useregister.cs模型中,因为我读了一些指导实体框架的信息,需要添加一些数据类型的东西。因此我更加困惑,因为我是asp.net的新手。真的希望你们能帮助我。

HTML CODE

@model DDAC_Assignment_jason.Models.UserRegister

@{
    ViewBag.Title = "Customer";
}

<h2>Customer</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User</legend>
        @Html.AntiForgeryToken()
        @if (ViewBag.Message != null)
        {
            <div style="border:solid 1px green">
                @ViewBag.Message
            </div>
        }


        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConfirmPassword)
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
} 

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

The register part of my website

1 个答案:

答案 0 :(得分:0)

你可以编写try catch block并在catch中设置一个断点。

try
{
   cnn.Open();
   Response.Write("Connection Made");

   string query = "INSERT INTO Customer(Username,Password,Email) VALUES('" + model1.Username + "','" + model1.Password + "','" + model1.Email + "')";
   SqlCommand command = new SqlCommand(query, cnn);
   command.ExecuteNonQuery();
}
catch
{  //break point
   //log
}
finally
{
    cnn.Close();
}

你的HTML将是,

@using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post))