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);
        }

How to Add Records using Ms-Access Database in MVC4 With Pure Ms-Access


There are two types of methods HttpPost and HttpGet Post method is used to save the record by inserting the values. Get method is used to show the entry form design 

 [HttpPost]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection frm1)
        {
            Employee emp = new Employee();
            emp.EmpId =  Convert.ToInt32(frm1["EmpId"]);
            emp.EmpName = frm1["EmpName"];
            emp.EmpNo = frm1["EmpNo"];

            string mycon = @"PROVIDER=Microsoft.JET.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("\\Temp.mdb");
            OleDbConnection con = new OleDbConnection(mycon);
            con.Open();
            OleDbCommand command = new OleDbCommand("Insert into Table1 values(@EmpId,@EmpNo,@EmpName)", con);
            command.CommandType = CommandType.Text;
            command.Parameters.Add("@EmpId", emp.EmpId);
            command.Parameters.Add("@Empno", emp.EmpNo);
            command.Parameters.Add("@EmpName",emp.EmpName);
            int rows = command.ExecuteNonQuery();
            return View();
        }

How to  binding Gridview using Linq and Populated Records.


Code Given Below


 using (DataClassesDataContext Dbcontext = new DataClassesDataContext())
        {

            Table_11 Emp = new Table_11();
            Emp.Ename = TxtName.Text;
            Emp.Eempno = TxtEmpno.Text;
            Emp.Eamt = TxtAmount.Text;
            Dbcontext.Table_11s.InsertOnSubmit(Emp);
            Dbcontext.SubmitChanges();
            lblMsg.Text = "SucessfullY Saved!..";

            if (Cmd_Save.Text == "Update")
            {
                var products = (from p in Dbcontext.Table_11s
                                where p.id == intid
                                select p).Single();
                Dbcontext.SubmitChanges();  
            }
        }

private void BindGrdis()
    {
        using (DataClassesDataContext Dbcontext = new DataClassesDataContext())
        {
            var Test = from p in Dbcontext.Table_11s select new { Ename = p.Ename, Eempno = p.Eempno, Eamt = p.Eamt };

            grd1.DataSource = Test; 
            grd1.DataBind(); 

        }
    }