Tuesday, August 19, 2014

mvc checkboxlist


//Set up a viewmodel MyViewModel to contain the following

        [DisplayName("Available Products")]
        public IList AvailableProducts { get; set; }
        public IList SelectedProducts { get; set; }

        public PostedProducts PostedProducts { get; set; }



//The definition of ProductDropdownItemViewModel and PostedProducts are:

    public class PostedProducts
    {
        public int[] ProductIds { get; set; }
    }

    public class ProductDropdownItemViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }        
    }


//In your controller method populate the above viewmodel

  e.g. 
                MyViewModel.AvailableProducts = db.Products
                .Select(p => new ProductDropdownItemViewModel 
            {
                Id = p.Id,
                Name = p.Name
            }).ToList();


//In your view display the checkbox list this way

  e.g. the postback method name is Edit

     @model MyViewModel
     @using (Html.BeginForm("Edit", FormMethod.Post, new { enctype = "multipart/form-data" }))
     {
        @Html.CheckBoxListFor(x => x.PostedProducts.ProductIds,
                      x => x.AvailableProducts,
                      x => x.Id,
                      x => x.Name,
                      x => x.SelectedProducts,
                      MvcCheckBoxList.Model.Position.Vertical)
     }


//In your postback method Edit use the following code to retrieve your selected items


       public ActionResult Edit(PostedProducts postedProducts)
       {

                foreach (var id in postedProducts.ProductIds)
                {
                     //do whatever you like with the selected product id
                }

       }

Tuesday, August 05, 2014

tsql create table if not already exists


IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[table1]') AND type in (N'U'))

BEGIN

CREATE TABLE [dbo].[table1](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Name] [nvarchar](150) NOT NULL,
 CONSTRAINT [PK_abc] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



END
go

c# read data from .xsl


public void ReadExcel(HttpPostedFileBase file, string currentUserName)
        {

            DataTable tbContainer = new DataTable();
            string strConn = XlsConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file.FileName +";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
            string sheetName = "sheet1";
            FileInfo fileInfo = new FileInfo(file.FileName);
            OleDbConnection cnnxls = new OleDbConnection(strConn);
            OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), cnnxls);
            DataSet ds = new DataSet();
            oda.Fill(tbContainer);
            var newParcelTubeExport = new ParcelTubeExport();

            //your code to utilize the data

         }