Hi kvjadon65,
Refer below example.
Database
CREATE TABLE MSTStudent
(
[RollNo] VARCHAR(50) PRIMARY KEY,
[Name] VARCHAR(100),
[Class] VARCHAR(100)
)
GO
CREATE TABLE DTLStudent
(
[DetailsId] INT IDENTITY PRIMARY KEY,
[Subject] VARCHAR(100),
[Max_Mark] INT,
[Martk_obtain] INT,
[RollNo] VARCHAR(50)
)
GO
CREATE PROCEDURE Students_SelectStudentDetails
AS
BEGIN
SELECT ms.[RollNo]
,ms.[Name]
,ms.[Class]
,ds.[Subject]
,ds.[Max_Mark]
,ds.[Martk_obtain]
FROM DTLStudent ds
INNER JOIN MSTStudent ms ON ms.RollNo = ds.RollNo
END
GO
CREATE PROCEDURE Students_InsertStudentDetails
@RollNo VARCHAR(50)
,@Name VARCHAR(100)
,@Class VARCHAR(100)
,@Subject VARCHAR(100)
,@MaxMark INT
,@MarkObtain INT
AS
BEGIN
INSERT INTO MSTStudent
VALUES (@RollNo
,@Name
,@Class)
INSERT INTO DTLStudent
VALUES (@Subject
,@MaxMark
,@MarkObtain
,@RollNo)
END
Model
public class StudentModel
{
public string RollNo { get; set; }
public string Name { get; set; }
public string Class { get; set; }
public string Subject { get; set; }
public int MaxMark { get; set; }
public int MarkObtain { get; set; }
}
Namespaces
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
ViewBag.Students = this.GetStudents();
return View();
}
[HttpPost]
public ActionResult Index(StudentModel studentModel)
{
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Students_InsertStudentDetails"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@RollNo", studentModel.RollNo);
cmd.Parameters.AddWithValue("@Name", studentModel.Name);
cmd.Parameters.AddWithValue("@Class", studentModel.Class);
cmd.Parameters.AddWithValue("@Subject", studentModel.Subject);
cmd.Parameters.AddWithValue("@MaxMark", studentModel.MaxMark);
cmd.Parameters.AddWithValue("@MarkObtain", studentModel.MarkObtain);
cmd.ExecuteNonQuery();
con.Close();
}
}
ViewBag.Students = this.GetStudents();
return RedirectToAction("Index");
}
private List<StudentModel> GetStudents()
{
List<StudentModel> students = new List<StudentModel>();
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Students_SelectStudentDetails"))
{
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
students.Add(new StudentModel
{
RollNo = Convert.ToString(sdr["RollNo"]),
Name = Convert.ToString(sdr["Name"]),
Class = Convert.ToString(sdr["Class"]),
Subject = Convert.ToString(sdr["Subject"]),
MarkObtain = Convert.ToInt32(sdr["Max_Mark"]),
MaxMark = Convert.ToInt32(sdr["Martk_obtain"])
});
}
con.Close();
}
}
return students;
}
}
View
@model ADO_Net_MVC.Models.StudentModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Student Details</th>
</tr>
<tr>
<td>Roll No: </td>
<td>@Html.TextBoxFor(m => m.RollNo)</td>
</tr>
<tr>
<td>Name: </td>
<td>@Html.TextBoxFor(m => m.Name)</td>
</tr>
<tr>
<td>Class: </td>
<td>@Html.TextBoxFor(m => m.Class)</td>
</tr>
<tr>
<td>Subject: </td>
<td>
@Html.DropDownListFor(m => m.Subject, new List<SelectListItem>
{ new SelectListItem{Text="Hindi", Value="Hindi"},
new SelectListItem{Text="English", Value="English"},
new SelectListItem{Text="Maths", Value="Maths"},
new SelectListItem{Text="Physics", Value="Physics"},
new SelectListItem{Text="Chemistry", Value="Chemistry"}},
"Please select")
</td>
</tr>
<tr>
<td>Max Mark: </td>
<td>@Html.TextBoxFor(m => m.MaxMark)</td>
</tr>
<tr>
<td>Mark Obtain: </td>
<td>@Html.TextBoxFor(m => m.MarkObtain)</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>Roll No</th>
<th>Name</th>
<th>Class</th>
<th>Subject</th>
<th>Max Mark</th>
<th>Mark Obtain</th>
</tr>
@foreach (var item in ViewBag.Students)
{
<tr>
<td>@item.RollNo</td>
<td>@item.Name</td>
<td>@item.Class</td>
<td>@item.Subject</td>
<td>@item.MaxMark</td>
<td>@item.MarkObtain</td>
</tr>
}
</table>
</body>
</html>
Screenshot