Introduction
In this article I am going to explain, how we can insert data using DAO class in MVC. In this example I am using ADO.net later on I will use Entity Framework. In this article I will use Request object to fetch data from view to controller. If you are not aware with Request object kindly first click here.
I have created the following table in Database for this example:-
For this example I am taking controller name Home and method name is Index and create view for this index method.
create table stu_record(id int primary key identity(1,1) , fname varchar(100),
lname varchar(100), course varchar(100), fees int, doj datetime)
Now go to the visual studio->File->New->project
And select MVC application.
And select MVC application.
Step 1
First of all go to the model folder->Right click->Add->class
Give proper name to the class and define properties for the each column which we define in table.
Give proper name to the class and define properties for the each column which we define in table.
Class Code
public class ViaStudy
{
public int ID { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string course { get; set; }
public int fees { get; set; }
public DateTime DOJ { get; set; }
}
Step 2
Now right click on the project and add the class and give it to name DAO. In this class I have created a method for insertion and in this method I am passing string type of query which will be generated in model which we created above.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
namespace MvcApplication1
{
public class DAO
{
//method which return connection string defined in configuration Manager
string con_string()
{
return ConfigurationManager.ConnectionStrings["con_string"].ToString();
}
//method which insert data in database
public int Insert(string query)
{
SqlConnection con = new SqlConnection(con_string());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = query;
int res = cmd.ExecuteNonQuery();
con.Close();
return res;
}
}
}
Step 3
Now again go to the Model which I added above and create method which generate the queries and calling the DAO class using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class ViaStudy
{
public int ID { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string course { get; set; }
public int fees { get; set; }
public DateTime DOJ { get; set; }
//insert method in which we pass the object type of Tech Altum
public int Insert(ViaStudy data)
{
string query = "insert into stu_record values('" + data.fName + "','" + data.lName + "','" + data.course + "'," + data.fees + ",'" + data.DOJ + "')";
DAO d=new DAO();
int res = d.Insert(query);
return res;
}
//insert method which using the current object properties
public int Insert()
{
string query = "insert into stu_record values('" + fName + "','" + lName + "','" + course + "'," + fees + ",'" + DOJ + "')";
DAO d = new DAO();
int res = d.Insert(query);
return res;
}
}
}
Step 4
Now it’s time to create controller and view. I have created the controller name Home and action name Index.
Now add the view for index method and make it strongly type view using ViaStudy model which we generated. And create form for this view which is as follows:-
Code of Index View
@model MvcApplication1.Models.ViaStudy
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("InsertResult","Home"))
{
<table>
<tr>
<td>Enter First Name</td>
<td>@Html.TextBox("fname")</td>
</tr>
<tr>
<td>Enter Larst Name</td>
<td>@Html.TextBox("lName")</td>
</tr>
<tr>
<td>Enter Course</td>
<td>@Html.TextBox("course")</td>
</tr>
<tr>
<td>Enter Fees</td>
<td>@Html.TextBox("fees")</td>
</tr>
<tr>
<td>Date of Joining</td>
<td>@Html.TextBox("doj")</td>
</tr>
<tr>
<td></td>
<td>@Html.TextBox("btn_submit", "Insert Data", new { type = "submit" })</td>
</tr>
</table>
}
</body>
</html>
Step 5
Now create another action where we redirect after successful completion of insertion. Add code to fetch data from view using request object. Pass this data to model and call the insert method.
Code of Home Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult InsertResult()
{
//create object of model
MvcApplication1.Models.ViaStudy data = new Models.TechAltum();
//pass data to the model using request object
data.fName = Request["fname"];
data.lName = Request["lname"];
data.fees = Convert.ToInt32 (Request["fees"]);
data.course=Request["course"];
data.DOJ=Convert.ToDateTime(Request["doj"]);
//call insertion method
int res = data.Insert();
if (res > 0)
{
Response.Write("Insert Successfully");
}
else
{
return RedirectToAction("index", "home");
}
return View();
}
}
}
No comments:
Post a Comment