Model class Department
:
[Table("Department")]
public class Department
{
[Display(Name ="Sno")]
public int id { get; set; }
[Display(Name = "Department_Id")]
public String Dno { get; set; }
[Required]
[Display(Name ="Department_Name")]
public String Dname { get; set; }
}
Repository class
public List<Department> GetDepartmentList()
{
connection();
List<Department> list = new List<Department>();
SqlCommand cmd = new SqlCommand("SpDepartmentList", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
foreach (DataRow dr in dt.Rows)
{
list.Add(
new Department
{
id = Convert.ToInt32(dr["id"]),
Dno = Convert.ToString(dr["Dno"]),
Dname = Convert.ToString(dr["Dname"]),
//DnoList = Convert.To(dr["DnoList"])
});
}
return list;
}
Submitting data using ADO.NET in the Repository
class:
public bool AddDepartment(Department obj)
{
connection();
SqlCommand cmd = new SqlCommand("spAddDepartments", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Dname", obj.Dname);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
return true;
}
else
{
return false;
}
}
Controller
[HttpGet]
public ActionResult AddDepartment()
{
// Here I want get Dno id displayed that is generated by
// database with that id I have to submit data to data table
return View();
}
[HttpPost]
public ActionResult AddDepartment(Department Dep)
{
try
{
if (ModelState.IsValid)
{
DepartmentRep Repo = new DepartmentRep();
if (Repo.AddDepartment(Dep))
{
ModelState.Clear();
ViewBag.Message = "Details added successfully";
}
}
return View();
}
catch
{
return View();
}
}
Table definition:
CREATE TABLE [dbo].[Department]
(
[id] INT IDENTITY (1, 1) NOT NULL,
[Dno] AS (CONCAT('Dno_', [id])),
[Dname] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Dno] ASC)
);
I have populate Dno
that is an identity automatically generated by database. With that id I have to submit data to database in to the table using ADO.NET.
Please can anyone help me?
I am new to this website if I did any mistake please excuse me.
1条答案
按热度按时间iezvtpos1#
To display the
Dno
(identity column) generated by the database when adding a newDepartment
, you can follow these steps:AddDepartment
action method to fetch the generatedDno
after inserting the data into the database using ADO.NET.Dno
to your view, so it can be displayed to the user.Here's how you can modify your code:
Controller (Add the
OUTPUT
clause to the SQL query):Repository (Return the generated Dno):
View (Display the generated Dno):
In this modified code:
AddDepartment
method in your repository now returns the generatedDno
as anint
. It also defines anOUTPUT
parameter in the SQL command to retrieve the generatedDno
.AddDepartment
action method now fetches the generatedDno
from the repository and passes it to the view usingViewBag
.ViewBag.GeneratedDno
is greater than 0 (indicating a valid generatedDno
) and displays it to the user if available.