Saturday, 29 July 2017

Convert DataTable to List


Create DataTable:
private void tempattributetable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Image_Path "typeof(int));
            dt.Columns.Add("ItemDetail_ID "typeof(string));
            dt.Columns.Add("IsDefaultImage "typeof(string));
            Session["dt"] = dt;

        }
Create Class:
public class MyType
        {
            public string Image_Path { get; set; }
            public int ItemDetail_ID { get; set; }
            public bool IsDefaultImage { get; set; }
        } 
Convert DataTable to List:

private void BindGrid()
{
DataTable dtimg = (Session["dt"]) as DataTable;
                    List<MyType> list = new List<MyType>();

                    var imgList = (from DataRow dr in dtimg.Rows
                                   select new MyType()
                                   {
                                       Image_Path = dr["Image_Path"].ToString(),
                                       ItemDetail_ID = Convert.ToInt32(dr["ItemDetail_ID"].ToString()),
                                       IsDefaultImage = Convert.ToBoolean(dr["IsDefaultImage"].ToString())
                                   }).ToList();
   if (imgList.ToList().Count > 0)
                    {
                        img.Visible = true;
                        img.DataSource = imgList.ToList();
                        img.DataBind();
                    }
}

Create DataTable and store data in datatable and bind with gridview


Create DataTable:
 private void temptable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ItemDetail_ID", typeof(int));
            dt.Columns.Add("Item_Price", typeof(string));
            dt.Columns.Add("Item_Barcode", typeof(string));
            dt.Columns.Add("Quantity", typeof(string));
            dt.Columns.Add("image", typeof(string));
            Session["dt"] = dt;
            BindGrid();
        }
Bind DataTable with GridView:
   protected void BindGrid()
        {
            gvattributes.DataSource = Session["dt"] as DataTable;
            gvattributes.DataBind();
        }

Enter Data in DataTable:

protected void btnSave_Click(object sender, EventArgs e)
{
DataTable dt=Session["dt"] as DataTable;
  dr = dt.NewRow();
                    dr["ItemDetail_ID"]= txtItemDetail_ID.Text.ToString();
                    dr["Item_Price"] = txtItem_Price.Text;
                    dr["Item_Barcode"] = txtItem_BarCode.Text;
                    dr["Quantity"] = txtQuantity.Text;
                    dr["image"] = txtItemDetail_ID.Text.ToString();
 Session["dt"] = dt;
BindGrid();
}

INSERT DATA IN DATABASE USING MVC


CREATE CLASS FILE IN MODEL:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Testmvcapp.Models
{
    public class ClsUser
    {
        public int MobileID { getset; }

       
        public string User_Id { getset; }

        [Required(ErrorMessage = "Please Enter UserName")]
        [Display(Name = "Enter UserName")]
        [MaxLength(50, ErrorMessage = "Exceeding Limit")]
        public string User_Name { getset; }

        [Required(ErrorMessage = "Please Enter Password")]
        [Display(Name = "Enter Password")]
        [MaxLength(50, ErrorMessage = "Exceeding Limit")]
        public string User_Password { getset; }

        [Required(ErrorMessage = "Please Enter Confirm Password")]
        [Display(Name = "Enter Confirm Password")]
        [Compare("User_Password", ErrorMessage = "Password and Confirmation Password must match.")]
        [MaxLength(50, ErrorMessage = "Exceeding Limit")]
        public string User_ConfPassword { getset; }

        [Required(ErrorMessage = "Please Enter Name")]
        [Display(Name = "Enter Name")]
        [DataType(DataType.Text)]
        public string User_FullName { getset; }

    }
}
ADD DEFAULTCONTROLLER IN CONTROLLER:
  Database2Entities ob = new Database2Entities();

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(ClsUser us)
        {
            if (ModelState.IsValid) 
            {
                TblUser user = new TblUser();
                user.User_FullName = us.User_FullName;
                user.User_Name = us.User_Name;
                user.User_Password = us.User_Password;
                user.User_Code = 123;
                ob.TblUsers.Add(user);
                ob.SaveChanges();
                return View("Index");
            }
            else
            {
                return View();
            }

        }
Create View Page in View:
@model Testmvcapp.Models.ClsUser
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>  
    <script src="~/Scripts/jquery-3.1.1.js"></script>
    <script src="~/Scripts/bootstrap.js"></script>
    <script src="~/Scripts/jquery-3.1.1.min.js"></script>
</head>
<body style="background-image:url(../Images/img11.jpg);background-repeat:no-repeat;background-size:cover;">
   @using (Html.BeginForm())
{
        <div style="padding-left:20%;padding-right:20%;padding-top:10%">
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    Enter Name
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    @Html.TextBoxFor(a => a.User_FullName, new { @class = "text-primary", width = "100%" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_FullName)
                    </span>
                   
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    User Name
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    @Html.TextBoxFor(a => a.User_Name, new { @class = "text-primary", width = "100%" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_Name)
                    </span>
                    
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    Enter Password
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">

                    @Html.TextBoxFor(a => a.User_Password, new { @class = "text-primary", width = "100%", type = "password" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_Password)
                    </span>
                    @*<input type="text" id="txtpassword" style="width:280px;" class="text-primary" />*@
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    ReEnter Password
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    @Html.TextBoxFor(a => a.User_ConfPassword, new { @class = "text-primary", width = "100%", type = "password" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_ConfPassword)
                    </span>

                   
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">

                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    <input type="submit" value="Upload" class="btn btn-primary" onclick="location.href='@Url.Action("Index""DefaultController")'" />
                  
                </div>

            </div>
        </div>
   }
</body>
</html>