How to insert record in database using ado in MVC

In this article, we will learn how to value insert in database using ado.net in MVC. Here we have a table with record name in sql server and we will create a form and with the help of insert query we will save the values in database.

Database:-





Model:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCODESOLUTIONS.Models
{
   

    public class recordDetail
    {
        public int record_id { get; set; }
        public string Name { get; set; }
        public string Contact_no { get; set; }
        public string Email { get; set; }


       
    }
}


Controller:-
using MVCCODESOLUTIONS.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace MVCCODESOLUTIONS.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();

        }
        [HttpPost]
        public ActionResult Index(recordDetail recordtbl)
        {
            string constr = "Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678";
            SqlConnection con = new SqlConnection(constr);
            string query = "INSERT INTO record(Name, Contact_no,Email) VALUES(@Name, @Contact_no,@Email)";

            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            cmd.Parameters.AddWithValue("@Name", recordtbl.Name);
            cmd.Parameters.AddWithValue("@Contact_no", recordtbl.Contact_no);
            cmd.Parameters.AddWithValue("@Email", recordtbl.Email);
            cmd.ExecuteNonQuery();
            con.Close();
  

            return View(recordtbl);
        }
    }
}

View:-
@model MVCCODESOLUTIONS.Models.recordDetail

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />

    <title>How to insert record in database using ado in MVC </title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <fieldset style="width: 500px;">
            <legend>Insert record in database using ado in MVC </legend>
            <table border="0" cellpadding="0" cellspacing="0">
                <tr style="width:150px;">
                    <td>Name</td>
                    <td>@Html.TextBoxFor(m => m.Name)</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>Contact Number:</td>
                    <td>@Html.TextBoxFor(m => m.Contact_no)</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td style="width: 80px">Email:</td>
                    <td>@Html.TextBoxFor(m => m.Email)</td>
                </tr>


                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>

                    <td></td>
                    <td><input type="submit" value="Save" /></td>
                </tr>
            </table>
        </fieldset>
    }
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
   @if (Model != null)
   {
    <script type="text/javascript">
            $(function () {
                alert("Value Inserted");
            });
    </script>
   }
 
</body>
</html>
Out-PUT:-


How to insert record in database using ado in MVC How to insert record in database using ado in MVC Reviewed by NEERAJ SRIVASTAVA on 12:11:00 PM Rating: 5

No comments:

Powered by Blogger.