How we can use ASP.NET MVC Framework check this post
http://www.dotnetissues.com/2011/04/code-example-for-aspnet-mvc20.html
In the above Post example I have used ADO.NET to insert the data in SQLServer Db (Student Table)
Now In this post I 'll add one more view Where To Select the records from the above table I 'll
use ADO.NET Entity framework.
1.)
Creating the ADO.NET Entity Data Model
In order to use the Entity Framework, you need to create an Entity Data Model. You can take advantage of the Visual Studio Entity Data Model Wizard to generate an Entity Data Model from a database automatically.
Follow these steps:
1)Right-click the Models folder in the Solution Explorer window and select the menu option
Add, New Item.
2)In the
Add New Item dialog, select the Data category
3)Select the
ADO.NET Entity Data Model template, give the Entity Data Model the name Model1.edmx,
4)Click the
Add button. Clicking the
Add button launches the Data Model Wizard.
5)In the
Choose Model Contents step, choose the
Generate from a database option and click the
Next button
6)In the
Choose Your Data Connection step, select the MoviesDB.mdf database connection, enter the entities connection settings name TestMVEntities3, and click the
Next button
8)In the
Choose Your Database Objects step, select the TestMVC database table and click the
Finishbutton
After you complete these steps, the ADO.NET Entity Data Model Designer (Entity Designer) opens.
Model1.designer.cs will be automatically generated and It will Map the Database and the tables as the
entity classes and To Communicate with the DB now we can Communicate with The entity classes objects
We can use EntitySQl query syntax or LINQ to Sql Below is the code sample for both.
To Select Records from student table using entity sql I added below highlighted code in StudentController.cs
Controllers/StudentController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestMVCapplication.Models;
using System.Data;
using System.Data.SqlClient;
using System.Data .Objects ;
namespace TestMVCapplication.Controllers
{
public class StudentController : Controller
{
SqlConnection connObj = new SqlConnection();
DataSet ds;
SqlCommand objCommand;
SqlDataAdapter objAdapter;
// GET: /Student/
public ActionResult GetStudent()
{
return View();
}
public ActionResult Viewstudent()
{
TestMVCEntities3 DBobj = new TestMVCEntities3();
//ENTITY SQL QUERY SYNTAX
ObjectQuery query = DBobj.CreateQuery<Student>("Select value s from student as s");
var res = query.Execute(MergeOption.NoTracking) as IEnumerable<Student>;
int i=0;
foreach (Student c in res)
{
// Response .Write (c.Name);
ViewData["name"+i.ToString()] = c.Name;
ViewData["email" + i.ToString()] = c.Email;
ViewData["comment" + i.ToString()] = c.Comment;
i++;
}
//LINQ TO ENTITY QUERY
//var query1 = from Student c in DBobj.Student
// select c;
//foreach (Student c in query1)
// {
// Response .Write (c.Name);
// }
return View();
}
[HttpPost]
public ActionResult GetStudent(StudentModel model)
{
Response.Write(model.Name + model.Comment + model.Email);
//code to insert data in DB using ADO.NET
connObj.ConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=TestMVC;Integrated Security=True";
connObj.Open();
SqlCommand comm = new SqlCommand("insert into student(name,email,comment) values(@name,@email,@comment)", connObj);
comm.Parameters.Add("@name", SqlDbType.VarChar , 50).Value =model .Name ;
comm.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = model.Comment;
comm.Parameters.Add("@comment", SqlDbType.VarChar, 50).Value = model.Email;
int result = comm.ExecuteNonQuery();
if (result != 0)
Response.Write(" added");
else
Response.Write("Error");
return View();
}
}
}
From the above Viewstudent I selected records from the Student Table and
Inserted those iv ViewData to able to access them in the Viewstudent View
I added one View named Viewstudent.aspx in in views/Student
views/student/Viewstudent.aspx