Skip to main content


Showing posts from June, 2014

How to create Indexes on Views: SQL Server

Similar to the tables, you can create indexes on views. By default, the views created on a table are no indexed. However, you can index the views when the volume of data in the underlying tables is large and not frequently updated. Indexing a view helps in improving the query performance.

Another benefit of creating an indexed view is that the optimizer starts using the view index in queries that do not directly name the view in the FROM clause. If the query contains references to columns that are also present in the indexed view, and the query optimizer estimates that using the indexed view offer the lowest cost access mechanism, the query optimizer selects the indexed view.

When indexing a view, you need to first create a unique clustered index on a view. After you have defined a unique clustered index on a view, you can create additional non-clustered indexes. When a view is indexed, the rows of the view are stored in the database in the same format as a table.
Guidelines for Crea…

How to apply restrictions at time of Modifying Data using Views

Views do not maintain a separate copy of the data, but only display the data present in the base tables. Therefore, you can modify the base tables by modifying the data in the view, however, the following restrictions exist while inserting, updating, or deleting data through views:

You cannot modify data in a view if the modification affects more than one underlying table. However, you can modify data in a view if the modification affects only one table at a time.You cannot change a column that is the result of a calculation, such as a computed column or an aggregate function.
For example, a view displaying the employee id, manger id, and rate of the employees has been defined using the following statement:

SELECT i.EmployeeID, i.MangerID, j.Rate FROM HumanResources.Employee AS i
JOIN HumanResources.EmployeePayHistory AS j ON
i.EmployeeID = j.EmployeeID

After creating the view, if you try executing the following update statement, it generates an error. This is because…

Creating View and Guidelines in SQL Server

Database administrator might want to restrict access of data to different users. They might want some users to be able to access all the columns of a table whereas other users to be able to access only selected columns. The SQL Server allows you to create views to restrict user access to the data. Views also help in simplifying query execution when the query involves retrieving data from multiple tables by applying joins.

A view is a virtual table, which provides access to a subset of columns from one or more tables. It is a query stored as an object in the database, which does not have its own data. A view can derive its data from one or more tables, called the base tables or underlying tables. Depending on the volume of data, you can create a view with or without an index. As a database developer, it is important for you to learn to create and manage views.
Creating Views A view is a database object that is used to view data from the tables in the database. A view has a structure s…

Returns Radio Button Input element using Html.RadioButton() handler: MVC

Html.RadioButton() handler, used to return radio button input element to be input true or false value by user. Checked radio button will return true and un-checked will return false, otherwise it returns null to store/use the value.

Html.RadioButton() handler method is used to present mutually exclusive option i.e. true of false. Using this radiobutton method makes it easy to bind to view data or model is so easy in compare to using simple input radio element. This handler provides mostly same features as Html.CheckBox() handler discussed earlier.
htmlHelper: specify html helper instance that this method specify name of input element used to access the value in controller.value: specify value of radio button element. If none is assigned then this attribute is used to access the value.isChecked: to be set true of false for radio button. True to select the element, OW false.htmlAttributes: specify an object that contains html attributes to set for the element.

Returns Password input element using Html.Password() Handler: MVC

Html.Password() handler, used to return password input element to be input password value by user. This article describes about to using password html helper that will make text unreadable by other users in MVC application.

Like all other input elements, using Html.Password() html handler users are not able to read input value. This handler is mostly used in login process of any ASP.Net MVC application because of some special features of this helper.

This helper commonly have following features:

Text input in the control is un-readable.Does not allow user to copy the text input in the control.Does not repopulate its value from ModelState object.
htmlHelper: html helper instance that this method used to specify the name of form field that may be used to look up the value.value: specify the value of this element.htmlAttributes: specify html attributes to be set for the element like placeholder, id, any class or other values. Overall this will generate an input el…

How to find no of days between two different date objects in

We have already discussed about that article , but today we add two months in current date time object , after that i will find how many days remaining from the current date and time. Lets take an simple example
<form id="form1"
    <asp:Button ID="Button1"
    Width="71px" />
    <asp:Label ID="Label1"
Code Behind
protected void Button1_Click(object sender, EventArgs e)

            DateTime today = DateTime.Today;
            Label1.Text = "today : " + today.ToLongDateString();
            DateTime twoMonthLater = today.AddMonths(2);
            TimeSpan TS = twoMonthLater - today;
            int days = TS.Days;
            Label1.Text += &quo…

C# - Arrays

Introduction If we create a variable of type int also assign 10 as value in it. If we want to store more than one value in it then its not possible. That why array comes to the picture, If you want to store similar type and more then one value into it then should take array. Array Definition  "An array is a collection of similar data type or you can say a single variable hold multiple other variables, those variable known as label of array. Lets take an simple example, suppose you have two things, which are ball and bat and you want to store it, use a box. Put the both things in separate boxes, also with label. Here 'array' is a box and label is 'variable name'. Basically array consists of two things first one is index and second one is value, If you want to access specific value in an array, use index. When we insert items into an array, default index started from 0. You can insert items randomly at any index position. Lets take an example for clarification that h…

C# - Variables

A variable is everything in programming, in which we can store values also change it during program execution. Before declare a variable in c#, must use type of it, so you say Each variable in C# has a specific type. You can perform multiple operation on a single variable, which value stored in memory. Now in this topic we will cover
1. How to declare a variable 2. How to internalize a variable. 3. Types of a variable
1. How to declare a variable  during the declaration of a variable, we use type of it. Through comma separation we declare multiple variable with single type. Lets take a simple syntax with example.
Syntax of single variable declaration  <data_type> single_variable_declaration; Example of single variable declaration int Employee_age;
Syntax of list of variable declaration <data_type> variable1, variable2, .....variableN; Example of list of variable declaration String Employee_Name, Employee_address, Employee_Country;
2. How to initialize a variable The meaning of in…

C# - Type Conversion

In Type casting you can change types or convert one types of data into another type.In c#, Basically there are two types. First one implicit type casting and another one is explicit type casting. Both are very useful in c# application development like, in web application text box return by default text value and you want to take int type value then you must convert text to int.
Implicit type conversion - conversion is performed by c# compiler automatically with type-safe manner. Now lets to take an simple example here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
    class Program
        static void Main(string[] args)
            Double d;
            int a=15;
            d = a;
Code Generate the following output

Explicit type conversion - conversion is performed by programmer explicitly using the pre-defined functions. Suppo…

How to add image and icon on button control in windows form c#

On a Button control, Text is not enough to making it beautiful. So If you want to make attractive windows application then you should go for images. In this article i will show you how to add image on button control also how to align with text. Now lets to start.
Step-1 : Add a new windows form project in visual studio, i have visual studio 2013, you can work in earlier version.
Step-2 : Add a button control in designer view of form
Step-3 : Select 'Image' property of Button control. Button control-->Property-->Image

Step-4 : Click on "ok" Button
Step-5 : After adding the image on button control, Set "ImageAlign" property of Button control to MiddleLeft.
Step-6 : Also set "TextAlign" property of Button control to MiddleRight.
Step-7 : Set Text Property of button control, decide you. In this article button with image is related to fruits so i giving you fruits title.
Text = " Fruits"
Now, your button control looking like

Change Text to Voice in windows form c#

Suppose your pronunciation is not right at this time and you want to use text to voice converter software. We are presenting to you  a very easy and simple converter, which convert text to voice. There are various steps to designing this types of converter, these are:Step-1 : Create a new project in windows form. Step-2 : Add a reference file (System.Speach) in the project. Step-3 : Add one rich textBox and menu strip control on windows form. Step-4 : Add items into menuStrip control looking like Step-5 : Add two namespaces in code file, which are using System.Speech; using System.Speech.Synthesis;
// Both are used for reading text and doing operation on it. Step-6 : Copy this code and paste into your menu strip item handlers.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Speech; using System.Speech.Synthesis;
namespace text2spech {     public partial cl…

Returns CheckBox Input element using Html.CheckBox() Handler: MVC

Html.CheckBox() handler, used to return check box input element to be input true or false value by user. Selected checkbox will return true and un-selected will return false, otherwise it returns null for the programmer to store/use the value.

In compare to other input element, this handler is easy to bind to with the data source provided by the programmer for the page. This handler/method have its own parameters list as Html.Label() or Html.TextBox() handlers have.
ParametersName: specifies the name of form field. It is string type of parameter.isChecked: programmer should pass true to select the checkbox, otherwise false. As the name implies it is Boolean type of parameter.htmlAttributes: specifies an object that contains all the html attributes to set for the element as discussed earlier.@Html.CheckBox("isMarried", true);

This line will return an check box input element on the page with checked true value, as passed with the method. The name of form field will be isMarri…

How to Execute Server-side code based on condition: Razor

Razor syntax helps developers to execute the code based on some condition specified like if-else or may be switch statement. Programmer can easily decide, which statement(s) need to be executed, based on the condition given within if statement. IF-Else Statement: To test a condition by using IF statement, write a condition in if parenthesis, start an IF block and at the last write the statements to be executed in the curly braces i.e. called if block. This block will be execute when the given condition is true, to execute some statements on the false of this condition, programmer have to write ELSE part.
@{     var value = 5; }
@if (value >4) {     <p style="font-size:20px; font-weight:bold">Value is greater than "4"</p> } else {     <p style="font-size:20px; font-weight:bold">Value is less than or equal to "4"</p> }
In the above block variable value have initial value 5. According to the given condition the first block will exe…

How to Optimize Indexes in SQL Server

SQL Server automatically maintains indexes whenever insert, update, or delete operations are made to the underlying data. These modifications cause the information within the index to become scattered in the database. Fragmentation exists when indexes within the index to become scattered in the database.

Fragmentation exists when indexes have pages where the logical ordering does not match the physical ordering within the data file. Heavily fragmented indexes can degrade the query performance and cause your application to respond slowly.

Fragmentation normally occurs when a large number of insert and update operations are performed on the table. Fragmented data can cause the SQL Server to perform unnecessary data reads. This affects the performance of the query. Therefore, index defragmentation is necessary to speed up the query performance.

The first step in deciding which defragmentation method to use is to determine the degree of index fragmentation. You can detect index fragmenta…

Managing indexes with pre-defined Query: SQL Server

In addition to creating indexes in sql server, database developer also need to maintain them to ensure their continued optimal performance. The common index maintenance tasks include disabling, enabling, renaming, and dropping an index. As a database developer, you need to regularly monitor the performance of the index and optimize it.
Disabling Indexes When an index is disabled, the user is not able to access the index. If a clustered index is disabled then the table data is not accessible to the user. However, the data still remains in the table, but is unavailable for Data Modification Language (DML) operations until the index is dropped or rebuilt.

To rebuild and enable a disabled index, use the ALTER INDEX REBUILDstatement or the CREATE INDEX WITH DROP_EXISTING statement.

The following query disables a non-clustered index, IX_EmployeeID, on the Employee table.

Enabling Indexes After an index is disabled, it remains in the disabled stat…

How to Create Partition Scheme and Clustered Index: SQL Server

After creating the partition function, programmer needs to create a partition scheme to associate it with the partition function. Based on the boundary values defined in the partition function, there will be five partitions. The data of each partition is stored in a filegroup. You should have the same number of filegroups as partitions. If there are five partitions, you need to create five filegroups: fg1, fg2, fg3, fg4, fg5.

The following statements create the PSOrderDate partition scheme, associating it with the PFOrderDate partition function:

TO (fg1, fg2, fg3, fg4, fg5)

The partition scheme, PSOrderDate, created in the preceding statement directs each partition to a separate filegroup.
Creating a Clustered Index After creating the partition scheme, you need to associate it with a clustered index. As the clustered index is created on the attribute having unique and non-null values, you can create the index on the SalesOrder…

How to Create Partitioned Indexes in SQL Server

In SQL Server, indexes can also be partitioned based on the value ranges. Similar to the partitioned tables, the partitioned indexes also improve query performance. Partitioning enables you to manage and access subsets of data quickly and efficiently. When indexes become very large, you can partition the data into smaller, more manageable sections as the data is spread across file groups in a database.

Consider an example. In the Adventure Works database, the SalesOrderHeader table contains the details about the order received by Adventure Works, Inc. As the data in this table is large, the query takes a long time to execute. To solve this problem, you can create a partitioned index on the table. Partitioning an index will distribute the data in the table into multiple filegroups, thereby partitioning the table. This will enable the database engine to read or write data quickly. This also helps in maintaining the data efficiently.

Partitioning is allowed only in the Enterprise Editio…

How to bind Gridview using strong model binding in

In our previous article, we have already learn how to bind GridView using, Entityframework, etc. Now, today we will learn strong model binding. Follow my steps
Step-1 :  Create a course.cs class, which is include in my previous article
Step-2 : Create a another class, which named as DataContext.cs class also include in my previous article.
Step-3 : After adding the two class in the project , add a new web form, which named as "getItem.aspx".
Step-4 : Add GridView control on web form
Step-5 :  Your source code looking like this

<form id="form1" runat="server">

        <asp:GridView ID="GridView1" runat="server" DataKeyNames ="ID" AutoGenerateColumns="false" ItemType="course" SelectMethod="getCourse">

                <asp:BoundField DataField="ID" HeaderText="Course_Id" />

How to Bind Gridview using Entity Framework in ASP.NET

In our previous article, we have already learned that how to bind gridview using Today we will learn simplest technique to bind gridview using entity framework. Lets start to bind.
Step-1 : Add a Course class with some attributes like
using System; using System.Collections.Generic; using System.Linq; using System.Web;
/// <summary> /// Summary description for course /// </summary> public class course {     public int ID { get; set; }     public string cname { get; set; }     public string ccode { get; set; }
} In this code ID is the primary key of the table. Learn How to make primary key in entity framework. Step-2 : Add a DataContext class for creating database with some table like.
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web;
/// <summary> /// Summary description for DataContext /// </summary> public class DataContext : DbContext { public DataContext():base ("connection1") { // // TODO: Add constructor …

How to download EntityFramework.dll file, Add System.Data.Entity namespace

This namespace take some classes of entity framework for accessing data. In this article we will learn how to add System.Data.Entity namespace because Entity framework.dll missing by default. So first to add entityframework.dll file as a reference.
How to download
Step-1 : Select Package Manager console from Tool menu.

Step-2 : Write
PM> Install-package entityframework

Note : Internet connection must.
Now, Using this method you can download latest version of entityframework.

How to populate comboBox in windows form c#

ComboBox is a collection, in which we can take Text as well as value of the text. After array, developer designed comboBox or DropDownList because array contains one attribute of object, its does not take two value at a time. Suppose you want to store employee name and age into memory. Where we would not use array. Here we will bind the comboBox in different manner like
Method-1 : Bind ComboBox from dataset and datatable using ADO.NET
Method-2 : Bind ComboBox using Entity framework.
Method-1 I have a database table like Students, which take some value. The Data View of table, which is stored in database.

C# Code for binding ComboBox (Binding from DataReader)

using System;
using WindowsFormsApplication10;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity;
using System.Data.SqlClient;

namespace WindowsFormsApplication10
    public partial class Form1 : Form