Grid.MVC with MultiSelect DropDownList with CheckBox auto selected from database in MVC

In this tutorial, I am going to explain you bind how to bind multiple selection (MultiSelect) DropDownList with CheckBox in Grid.MVC in ASP.Net MVC from database using jQuery, Bootstrap and MultiSelect JS. I will be using Visual Studio 2013 and ADO.Net code to get data from SQL-Server Database.

As you can see in below screen-shot, multi-select dropdownlist is auto-selected. Here in Subject column , subject value is concatenated with comma from database, while binding to MVCGrid subject is auto selected in last column as shown below.

Grid.MVC with MultiSelect DropDownList with CheckBox auto selected from database in MVC

Creating Table and Procedure

Use below script to create a Student Table and procedure to fetch data from database.

SQL Server Instead Of Insert Trigger Example

SQL Server Instead Of Insert Trigger Example

In this tutorial, I am going to explain you INSTEAD OF INSERT Trigger in SQL-Server with example. In the previous tutorials, I had explained introduction to triggers in SQL-Server. Please go through the links provided below.

Related Links

There are two main types of DML trigger.1. After Triggers (also called as FOR trigger)2. Instead of Triggers

After Trigger (using FOR/AFTER CLAUSE)

This trigger fires after SQL Server completes the execution of the action successfully that fired it. Example: If you insert record/row in a table then the trigger associated with the insert event on this table will fire only after the row passes all the checks, such as primary key, rules, and constraints. If the record/row insertion fails, SQL Server will not fire the After Trigger.

Instead of Trigger (using INSTEAD OF CLAUSE)

This trigger fires before SQL Server starts the execution of the action that fired it. This is much more different from the AFTER trigger, which fires after the action that caused it to fire. We can have an INSTEAD OF insert/update/delete trigger on a table that successfully executed but does not include the actual insert/update/delete to the table. Example: If you insert record/row in a table then the trigger associated with the insert event on this table will fire before the row passes all the checks, such as primary key, rules, and constraints. If the record/row insertion fails, SQL Server will fire the Instead of Trigger.

Instead of Insert Trigger Example

When we try to insert a record into a table then instead of insert trigger is fired if trigger is associated with that table. Insert of trigger is mostly used with Views in SQL, but in this tutorial we are going to implement instead of trigger with single table. In article, we will use instead of trigger with Views. Now let's implement the trigger.

Create a table Employee with 3 columns EmployeeId, Name and Age. EmployeeId will be auto generated. Below is the code to create Employee table and insert some dummy records.

Create XML document in C# using LINQ XDocument Class

In this articles, I am going to explain you how to create XML document in C# using LINQ XDocument class. Below is step by step tutorial.

Namespace Required

Create an empty asp.net web application and add a webform to it.
Include below namespace.

How to extract text from PDF file using iTextSharp with C#

In this tutorial, I am going to explain you how to extract text from PDF file using iTextSharp with C# in ASP.NET. Below is step by step tutorial.

Creating ASP.NET Empty Application

Create an ASP.NET Empty WebForm project as shown below.
Go to FileNewProject. A new window will be open as shown below.
Now go to WebVisual Studio 2012 → select .NET Framework 4.5 → select ASP.NET Empty Web Application and give project name and click on OK.

Creating asp.net 4.5 empty project

Now, an asp.net empty project will be created. Add a new webform to application.

Installing iTextSharp

Now the next step is to add iTextSharp reference to your application. We can add reference by two ways.
First: Download from Internet
Click on the below link to download the dll.
https://github.com/itext/itextsharp Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.
or Second: Nuget Package Manager
Go to TOOLS → Library Package Manager → Manage NuGet Packages for Solution.. and a new window will open. Type and search for iTextSharp and click on Install button as shown below. Once installed successfully, you can check iTextSharp in references folder.

Adding iTextSharp
Installing iTextSharp

You can also install by using Package Manager Console.
Go to TOOLS → Library Package Manager → Package Manager Console → write Install-Package iTextSharp and press enter. This will install iTextSharp in application.

In aspx file

In designer file create two button controls, first button is used to generate pdf file and second button is used to extract text from pdf file. One textbox control to display extracted text from pdf. Designer file look like as shown below.

Aspx designer file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <table>
        <tr>
            <td><b>Extract Text from PDF file using iTextSharp</b></td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF File" OnClick="btnGeneratePDF_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnExtract" runat="server" Text="Extract Text From PDF File" OnClick="btnExtract_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Style="width: 500px; min-height: 150px;"> 
                </asp:TextBox>
            </td>
        </tr>
    </table>
</div>
</form>
</body>
</html>

C# Code

Complete C# code is given below.

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
 
}
 
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }
 
    // create pdf file and save it to the root directory of the application 
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
 
    Document doc = new Document();
 
    PdfWriter.GetInstance(doc, fs);
 
    doc.Open();
 
    Paragraph page = new Paragraph("This is first page (page number 1)");
    doc.Add(page);
 
    Paragraph para1 = new Paragraph();
    Chunk c1 = new Chunk(@"This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph.");
    c1.SetBackground(BaseColor.YELLOW);
    para1.Add(c1);
    doc.Add(para1);
 
    Paragraph para2 = new Paragraph();
    Chunk c2 = new Chunk(@"This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph.");
    c2.SetBackground(BaseColor.GREEN);
    para2.Add(c2);
    doc.Add(para2);
 
    doc.Close();
}
 
protected void btnExtract_Click(object sender, EventArgs e)
{
    //string FilePath = @"H:\\Demo\\WebApplication1\\WebApplication1\\Example.pdf";
 
    string FilePath = Server.MapPath("Example.pdf");
 
    if (File.Exists(FilePath))
    {
        string ExtractedData = string.Empty;
 
        using (PdfReader reader = new PdfReader(FilePath))
        {
            ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
 
            // 1. if pdf document has only one page
            //here second parameter is PDF Page number
            ExtractedData = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
 
 
            /*// 2. if pdf ducument has more than one page
            // iterating through all pages
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                ExtractedData = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
            }*/
 
 
            /*// if pdf single page is having more than one paragraph
            // then split paragraph using newline
            ExtractedData = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
            string[] lines = ExtractedData.Split('\n');
            StringBuilder sb = new StringBuilder();
            foreach (string line in lines)
            {
                // 
            }*/
 
        }
        TextBox1.Text = ExtractedData;
    }
}
}
}

When you click on the Generate PDF File button, a PDF will be generated and will be saved at root directory of application. When you open pdf file, you will see 3 paragraph as shown below.

PDF file generated using iTextSharp

Now when you click on Extract Text From PDF File, all the text from page one will be extracted and displayed to the TextBox. You can iterate through all the pages using foor loop. Code is added and commented above.

Extract text from PDF using iTextSharp

SQL-Server Date and Time Functions

SQL-Server Date and Time Functions

In this tutorial, I am going to explain you SQL-Server Date and Time functions with example. Below is the list of most commonly used Date and Time function.

  • GETDATE
  • SYSDATETIME
  • CURRENT_TIMESTAMP
  • GETUTCDATE
  • SYSDATETIMEOFFSET
  • SYSUTCDATETIME
  • ISDATE
  • DAY
  • MONTH
  • YEAR
  • DATENAME
  • DATEPART
  • DATEADD
  • DATEDIFF

Below is explanation for each function.

GETDATE Function

Syntax: GETDATE( ) Description: It is used to return the current database system timestamp as a datetime value without the database time zone offset.This value is derived from the operating system of the computer on which the instance of SQL Server is running. Example:

Implement Chart using DotNet HighCharts in ASP.NET MVC

In this article, I am going to explain you how to implement chart using DotNet HighCharts in ASP.NET MVC. Here we will create chart using hard-coded value but in later articles I will explain draw chart from database. Below is step by step tutorial.

Creating ASP.NET MVC Empty Application

Now the next step is to create ASP.NET MVC empty application as shown below.
Go to FileNewProject. A new window will be open as shown below.
Now go to Web and select .NET Framework 4.5 and give project name and click on OK .

Creating asp.net mvc 5 application

Now new window will open as shown below.
Now Select Empty Template, check on MVC checkbox and click on OK.

Selecting asp.net mvc 5 empty template

Now, a new project will be created as shown below.

Asp.net mvc 5 empty folder structure

Installing DotNet.HighCharts

Now the next step is DotNet.HighCharts. Go to TOOLS → Library Package Manager → Manage NuGet Packages for Solution.. and a new window will open. Type and search for DotNet.HighChart and click on Install button as shown below. Once installed successfully, you can check DotNet.HighChart in references folder.

Adding DotNet.HighCharts
Installing DotNet.HighCharts

You can also install by using Package Manager Console. Go to TOOLS → Library Package Manager → Package Manager Console → write Install-Package DotNet.HighCharts and press enter. This will install DotNet.HighCharts in application.

Adding Controller

Next step is to add Controller to application. Add a new empty Controller class named as HomeController. After adding controller, a new action method will be created. Replace all code with below code.

SQL-Server built-in String function Part 2

SQL-Server built-in String function Part 2

In this tutorial, I am going to explain you SQL-Server built-in String functions with example. Below is the list of most commonly used SQL-Server System defined String function.

We will discuss in this articleAlready discussed in previous article
  • CHARINDEX
  • SUBSTRING
  • REPLICATE
  • SPACE
  • PATINDEX
  • REPLACE
  • STUFF
  • ASCII
  • CHAR
  • UPPER
  • LOWER
  • LTRIM
  • RTRIM
  • REVERSE
  • LEN
  • LEFT
  • RIGHT
Click here to read more

Below is explanation for each function.

CHARINDEX Function

Syntax: CHARINDEX ( expressionToFind , expressionToSearch, [ Start_location ] ) Description: It is used to find index position of specified expression in a Given Expression. Start_location parameter is optional. Searching starts from first position if Start_location is not supplied. Returns Zero if specified expression is not found. Example: