Tuesday 30 April 2013

MVC 4 - How to edit multiple records at once

This example shows how to perform bulk edit of records
I am using FormCollection for this purpose.

Watch this example on YouTube:







I will be editing FirstName and LastName in table above.(Please ignore DepartmentID column, I am not going to use it in this tutorial)  For that purpose I will create EF connection to the database (step by step examples in other MVC tutorials or in the video above) and will modify my View
@model IEnumerable<TestMultiUpdateWithMVC.Models.User>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()){
<table>
    @Html.ValidationSummary(true)
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.HiddenFor(modelItem => item.UserId)
        </td>
        <td>
            @Html.EditorFor(modelItem => item.FirstName)
            @Html.ValidationMessageFor(model => item.FirstName)
        </td>
        <td>
            @Html.EditorFor(modelItem => item.LastName)
            @Html.ValidationMessageFor(model => item.LastName)
        </td>
    </tr>
}

</table>
<input type="submit" value="save" />
}

So it produces page that presents all users with Save button



Now I will add the following code to the controller
        [HttpPost]
        public ActionResult Index(FormCollection c)
        {
            int i = 0;
            if (ModelState.IsValid)
            {
                var UserIDArray = c.GetValues("item.UserId");
                var UserFirstNameArray = c.GetValues("item.FirstName");
                var UserLastNameArray = c.GetValues("item.LastName");

                for (i = 0; i < UserIDArray.Count(); i++)
                {
                    User usr = db.Users.Find(Convert.ToInt32(UserIDArray[i]));
                    usr.FirstName = UserFirstNameArray[i];
                    usr.LastName = UserLastNameArray[i];
                    db.Entry(usr).State = EntityState.Modified;
                }
                db.SaveChanges();
            }
            return View(db.Users.ToList());
        }

That's it!  - Since I know that all arrays will have the same size, I can load all values to my arrays, then update my user(s).










Wednesday 24 April 2013

CSS Describe all parts - Rule Set vs Selector vs Declaration vs Property vs Value

Watch this example on YouTube:
    <style type="text/css">
        p
        {
            color: Red;
        }
        .LeftAligment
        {
            text-align: left;
        }
    </style>

______________________________________________________________________
Rule Set:
p
{
    color: Red;
}

______________________________________________________________________
Selector:
p
h1

______________________________________________________________________
Declaration:
color: Red;
______________________________________________________________________
Property
color:
______________________________________________________________________
Value:
Red;
______________________________________________________________________

CSS - Simple example how to add style sheet to web form


Watch this example on YouTube:



Inline example:
Copy code below 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication19.WebForm2" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        h1 {
            font-size: 30px;
            color: red;
        }
        p {
            color:blue;
            font-style: italic;
        }
        .RightAlign {
            text-align: right;
        }
         div
        {
            width:100px;
            padding:50px;
            border:5px solid black;
            margin: 1px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p> this is normal text</p>
        <p class="RightAlign">This is right aligned text</p>
    </div>
    </form>
</body>
</html>

CSS Selectors - Universal vs Type vs Class vs ID

Watch this example on YOUTube:
Code below creates simple web form with simple style sheet that has 4 selectors.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication19.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        * {
            color: green;
        }
        h1 {
            color: blue;
        }
        .RedFont {
            color: red;
        }
        #YellowFont {
            color: yellow;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p>This text will use universal selector - will be in green </p>
        <h1>This text will use type selector - will be in blue</h1>
        <div class="RedFont">This text will use class selector - will be in red</div>
        <div id="YellowFont">This text will use id selector - will be in yellow</div>
    </div>
    </form>
</body>
</html>

CSS - clear: both example


Watch this example on YouTube:

This is ready to copy and paste example
Try it with  clear: both; and without to see the difference
 Without clear:both footer will overlap with #MainContent

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>

      <style type="text/css">
        #PageWrapper
        {
            width: 844px;  
        }
        #Header
        {
            background-color: Red;
            width: 844px;
            height: 86px;
        }

        #MenuWrapper
        {
            width: 844px;
        }

        #MainContent
        {
            width: 664px;
            float: left;
        }

        #Sidebar
        {
            background-color: Gray;
            width: 180px;
            float: left;
        }


        #Footer
        {
            background-color: Red;
            width: 844px;
            clear: both;
        }

    </style>
 </head>

<body>
    <form id="form1" runat="server">
    <div id="PageWrapper">
        <div id="Header">
            This is header
        </div>
        <div id="MenuWrapper">
            This is menu
        </div>
        <div id="MainContent">
            This is main content
        </div>
        <div id="Sidebar">
            This is sidebar
        </div>
        <div id="Footer">
            This is footer
        </div>
    </div>
    </form>
</body>
</html>

Thursday 18 April 2013

VB.NET EXCEL - how to fix error The RPC server is unavailable

Replace :     
                    SaveExcel()
                    oExcel = CreateObject("Excel.Application")
                    oExcel.Workbooks.Open(FileLocation & FileName)
                    oBook = oExcel.ActiveWorkbook
                    oSheet.Range("A1").Value = "TEST"
With:
                    SaveExcel()
                    oExcel = CreateObject("Excel.Application")
                    oExcel.Workbooks.Open(FileLocation & FileName)
                    oBook = oExcel.ActiveWorkbook
                    oSheet = oExcel.Worksheets(1)
                    oSheet.Range("A1").Value = "TEST"