[go: up one dir, main page]

0% found this document useful (0 votes)
9 views7 pages

Example of Save

The document provides a step-by-step guide to create a simple MVC application that interacts with a SQL Server database for managing student records. It includes instructions for creating a database table, implementing CRUD operations (Create, Read, Update, Delete) in a model class, and setting up corresponding views and controllers. The final output is a functional web application that allows users to manage student information through a user interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Example of Save

The document provides a step-by-step guide to create a simple MVC application that interacts with a SQL Server database for managing student records. It includes instructions for creating a database table, implementing CRUD operations (Create, Read, Update, Delete) in a model class, and setting up corresponding views and controllers. The final output is a functional web application that allows users to manage student information through a user interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Example of Save,update,delete and display the record from database :

(i) First of all we have to create one table in sqlserver database as :--

Create table stud_info(roll varchar(50),name varchar(50),address varchar(50),phone


varchar(50))
(ii) Create a new mvc internet application.
(iii) Add one Model file as “Class1.cs”.
(iv) Write the code in “Class1.cs” file as :-

using System.Data.SqlClient;
using System.Data;

namespace MvcApplication125.Models
{
public class stud
{
public String roll { get; set; }
public String name { get; set; }
public String address { get; set; }
public String phone { get; set; }
public stud(String p1 = "", String p2 = "", String p3 = "", String p4 = "")
{
roll = p1;
name = p2;
address = p3;
phone = p4;
}
}
public class Class1
{
SqlConnection con = new SqlConnection("server=india; database=tiwari; integrated
security=sspi");
public List<stud> show_record()
{
SqlDataAdapter da = new SqlDataAdapter("select * from stud_info", con);
DataTable dt = new DataTable();
da.Fill(dt);
List<stud> col = new List<stud>();
foreach (DataRow row in dt.Rows)
{
col.Add(new stud(row["roll"].ToString(), row["name"].ToString(),
row["address"].ToString(),
row["phone"].ToString()));
}
return (col);
}
public void save_record(String roll, String nm, String add, String ph)
{
SqlCommand cmd = new SqlCommand("insert into stud_info
(roll,name,address,phone)
values(@roll,@name,@address,@phone)",
con);
cmd.Parameters.AddWithValue("@roll", roll);
cmd.Parameters.AddWithValue("@name", nm);
cmd.Parameters.AddWithValue("@address", add);
cmd.Parameters.AddWithValue("@phone", ph);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public stud search_record(String roll)
{
SqlDataAdapter da = new SqlDataAdapter("select * from stud_info where
roll='"+roll+"'", con);
DataTable dt = new DataTable();
da.Fill(dt);
stud st1 = new stud(roll,dt.Rows[0]["name"].ToString(),
dt.Rows[0]["address"].ToString(), dt.Rows[0]
["phone"].ToString());
return (st1);
}
public void update_record(String roll, String nm, String add, String ph)
{
SqlCommand cmd = new SqlCommand("update stud_info set name=@name,
address=@address, phone=@phone where roll=@roll", con);
cmd.Parameters.AddWithValue("@roll", roll);
cmd.Parameters.AddWithValue("@name", nm);
cmd.Parameters.AddWithValue("@address", add);
cmd.Parameters.AddWithValue("@phone", ph);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void delete_record(String roll)
{
SqlCommand cmd = new SqlCommand("delete from stud_info where roll=@roll",
con);
cmd.Parameters.AddWithValue("@roll", roll);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}

(v) Write the code in “Home” controller as :--

using MvcApplication125.Models;

namespace MvcApplication125.Controllers
{
public class HomeController : Controller
{
Class1 c1 = new Class1();
public ActionResult Index()
{
return View(c1.show_record());
}

public ActionResult create()


{
return View();
}
public ActionResult save_record(String txt_roll="", String txt_nm="",
String txt_add="", String txt_ph="")
{
c1.save_record(txt_roll, txt_nm, txt_add, txt_ph);
return RedirectToAction("Index");
}

public ActionResult edit(String roll = "")


{
return View(c1.search_record(roll));
}

public ActionResult update(String roll = "",String name="",String address="",


String phone="")
{
c1.update_record(roll, name, address, phone);
return RedirectToAction("Index");
}

public ActionResult delete(String roll = "")


{
return View(c1.search_record(roll));
}
public ActionResult delete_record(String roll = "")
{
c1.delete_record(roll);
return RedirectToAction("Index");
}

public ActionResult details(String roll="")


{
return View(c1.search_record(roll));
}
}
}

(vi) Write the code in “index.cshtml” view page as :--

@model IEnumerable<MvcApplication125.Models.stud>
@Html.ActionLink("Create New Record", "create")<br />
<table style="width:80%; border:1px solid red" >
<tr style="font-size:18px; font-weight:bold; border:1px solid blue">
<td style="width:15%">Roll</td>
<td style="width:15%">Name</td>
<td style="width:15%">Address</td>
<td style="width:15%">Phone</td>
<td style="width:40%">Operation</td>
</tr>
@foreach (var c1 in Model)
{
<tr style="font-size:16px; font-weight:bold; border:1px solid green">
<td> @c1.roll </td><td> @c1.name</td><td> @c1.address </td><td>
@c1.phone</td><td> @Html.ActionLink("Edit", "edit", new {roll=@c1.roll}) &nbsp;&nbsp;
@Html.ActionLink("Delete","delete",new {roll=@c1.roll}) &nbsp;&nbsp;
@Html.ActionLink("Details","details",new {roll=@c1.roll}) </td>
</tr>
}
</table>

(vii) Write the code in “create.cshtml” view page :--

@using (Html.BeginForm("save_record","Home"))
{
<table style="width:50%">
<tr>
<td style="width:50%">
Roll
</td>
<td style="width:50%">
@Html.TextBox("txt_roll")
</td>
</tr>
<tr>
<td style="width:50%">
Name
</td>
<td style="width:50%">
@Html.TextBox("txt_nm")
</td>
</tr>
<tr>
<td style="width:50%">
Address
</td>
<td style="width:50%">
@Html.TextBox("txt_add")
</td>
</tr>
<tr>
<td style="width:50%">
Phone
</td>
<td style="width:50%">
@Html.TextBox("txt_ph")
</td>
</tr>
<tr>
<td style="width:50%">
<input type="submit" value="Save Record" />
</td>
<td style="width:50%">
@Html.ActionLink("Home", "Index")
</td>
</tr>
</table>
}

Write the code in “edit.cshtml” view page as :--

@model MvcApplication125.Models.stud
@using (Html.BeginForm("update","Home"))
{
<table style="width:50%">
<tr>
<td style="width:50%">
Roll
</td>
<td style="width:50%">
<input type="text" name="roll" value="@Model.roll" readonly="true" />
</td>
</tr>
<tr>
<td style="width:50%">
Name
</td>
<td style="width:50%">
@Html.TextBoxFor(m=>m.name)
</td>
</tr>
<tr>
<td style="width:50%">
Address
</td>
<td style="width:50%">
@Html.TextBoxFor(m=>m.address)
</td>
</tr>
<tr>
<td style="width:50%">
Phone
</td>
<td style="width:50%">
@Html.TextBoxFor(m=>m.phone)
</td>
</tr>
<tr>
<td style="width:50%">
<input type="submit" value="Update Record" />
</td>
<td style="width:50%">
@Html.ActionLink("Home", "Index")
</td>
</tr>

</table>
}

(viii) Write the code in “delete.cshtml” view page as :--

@model MvcApplication125.Models.stud
@using (Html.BeginForm("delete_record","Home"))
{
<table style="width:50%">
<tr>
<td style="width:50%">
Roll
</td>
<td style="width:50%">
<input type="text" name="roll" value="@Model.roll" readonly="true" />
</td>
</tr>
<tr>
<td style="width:50%">
Name
</td>
<td style="width:50%">
@Html.TextBoxFor(m=>m.name, new {disabled="disable"})
</td>
</tr>
<tr>
<td style="width:50%">
Address
</td>
<td style="width:50%">
@Html.TextBoxFor(m=>m.address, new {disabled="disable"})
</td>
</tr>
<tr>
<td style="width:50%">
Phone
</td>
<td style="width:50%">
@Html.TextBoxFor(m=>m.phone, new {disabled="disable"})
</td>
</tr>
<tr>
<td style="width:50%">
<input type="submit" value="Delete Record" />
</td>
<td style="width:50%">
@Html.ActionLink("Home", "Index")
</td>
</tr>

</table>
}

(ix) Write the code in “details.cshtml” view page as :--

@model MvcApplication125.Models.stud

<h2>Roll : @Model.roll</h2>
<h2>Name : @Model.name</h2>
<h2>Address : @Model.address</h2>
<h2>Phone : @Model.phone</h2>
@Html.ActionLink("Home", "Index")

(x) Press “F5” key to execute the program.

The output of above program is :

You might also like