Monday, 22 June 2015

How to Implemented Bind Records in MVC4 using Sqlserver From Controller to View



How to Establish the connection and Fetch Records from database and Fill data Sqladapter then
using loop assign the each row records to list object pass the list from Controller to View 

 SqlConnection sqlcon = new SqlConnection("Data source=PC-NAME;Initial Catalog=Test;Integrated security=True;");
        SqlDataAdapter sqladp;
        DataTable dt = new DataTable();
        List lstemp = new List();
        public ActionResult Index()
        {
            sqladp = new SqlDataAdapter("Select * from TblEmp", sqlcon);
            sqladp.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                lstemp.Add(new Employee() { EmpId =int.Parse(dr[0].ToString()),EmpName=dr[1].ToString(),EmpNo=dr[2].ToString()});
            }
            return View(lstemp);
        }

No comments:

Post a Comment