-->

Friday, March 14, 2014

GridView Row background color in ASP.NET

Initial Steps for viewers

Step-1 : Open Visual Studio IDE
Step-2 : Add New Webform into your project
Step-3 : Open Design page for adding control onit.
Step-4 : You can change Background color from property menu.
GridView Row background color in ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>

 <RowStyle BackColor="#FF3399" />

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>

Code generate the following output

GridView Row background color in ASP.NET

Thursday, March 13, 2014

GridView Row border color in ASP.NET

Initial Steps for viewers

Step-1 : Open Visual Studio IDE
Step-2 : Add New Webform into your project
Step-3 : Open Design page for adding control onit.
Step-4 : You can change Border color from property menu.
GridView Row border color in ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>

 <RowStyle BorderColor="Red" />

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>

Code generate the following output

How to use Default Constraint on Column in SQL

A default constraint can be used to assign a constant value to a column, and the user need not insert value for such a column. Only one default constraint can be created for a column but the column cannot be an identity column. The system-supplied values, such as USER, CURRENT_USER, and user-defined values can be assigned as defaults.

The syntax of applying the default constraint while creating a table is:

CREATE TABLE table_name
(
Col_name [CONSTRAINT constraint_name] DEFAULT (constant_expression|NULL)
(col_name [, col_name [, …]])
.
)
Where,

  • Constraint_name specifies the name of the constraint to be created.
  • Constant_expression specifies an expression that contains only constant values. This can contain a NULL value.

In the example of creating the EmployeeLeave table, you can insert a default constraint to add a default value for the LeaveType column. You can set the default leave type as PL. You can use the following statement to create the default constraint:

CREATE TABLE HumanResources.EmployeeLeave
(
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES Humanresources.Employe (EmployeeID),
LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY LeaveEndDate datetime NOT NULL,
LeaveReason varchar(100),
LeaveType char(2) CONSTRAINT chkLeave CHECK (LeaveType IN (‘ CL’, ‘SL’, ‘PL’)) CONSTRAINT chkDefLeave DEFAULT ‘PL’)

The preceding command creates the EmployeeLeave table with a default constraint on the LeaveType column, where the default value is specified as PL. The name of the constraint is chkDefLeave.

Check on Constraints

How to apply Check on Constraint on Column in SQL

A check constraint enforces domain integrity by restricting the values to be inserted in a column. It is possible to define multiple check constraints on a single column. These are evaluated in the order in which they are defined. The syntax of applying the check constraint is:

CREATE TABLE table_name
(
Col_name [CONSTRAINT onstraint_name] CHECK (expression)
(col_name [, col_name [, …]])
.
)
A single check constraint can be applied to multiple columns when it is defined at the table level
Where,

  • Constraint_name specifies the name of the constraint to be created.
  • Expression specifies the conditions that define the check to be made on the column. Expression can include elements, such as arithmetic operators, relational operators, o keywords, such as IN, LIKE, and BETWEEN.

A check constraint can be specified by using the following keywords:

  • IN: To ensure that the values entered are from a list of constant expressions.
  • LIKE: To ensure that the value entered in specific column are of a certain pattern. This can be achieved by using wildcards.
  • BETWEEN: To specify a range of constant expressions by using the BETWEEN keyword. The upper and lower boundary values are included in the range.

The rules regarding the creation of the CHECK constraint are as follows:

  • It can be created at the column level.
  • It can contain user-specified search conditions.
  • It cannot contain subqueries.
  • It does not check the existing data in the table if created with the WITH NOCHECK option.
  • It can reference other columns of the same table.

In the example of the EmployeeLeave table, you need to ensure that the leave type can take any one of the three values: CL, PL, or SL. For this, while creating the table, you can add the check constraint using the IN keyword for the LeaveType column

You can use the following statement to apply the check constraint:

CREATE TABLE HumanResources.EmployeeLeave
(
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES HumanResources.Employee (EmployeeID),
LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY KEY (EmployeeID, LeaveStartDate), LeaveEndDate datetime NOT NULL,
LeaveReson varchar(100),
LeaveType char (2) CONSTRAINT chkLeave CHECK (LeaveType IN (‘CL’, ‘SL’, ‘PL’))
)

The preceding command creates the EmployeeLeave table with a check constraint on the LeaveType column. The name of the constraint is chkLeave.

How to use Foreign Key Constraint on Column in SQL

Database developer can use the foreign key constraint to remove the inconsistency in two tables when the data in one table depends on the data in another table.

A foreign key constraint associates one or more columns (the foreign key) of a table with an identical set of columns (a primary key column) in another table on which a primary key constraint has been defined. The syntax of applying the foreign key constraint when creating table is:

CREATE TABLE table_name
(
Col_name [CONSTRAINT constraint_name FOREIGN KEY (col_name [, col_name [, …]])
REFERENCES table_name (column_name [, column_name [, …]])]
Col_name [, col_name [, col_name [, …]]])
Col_name [, col_name [, col_name [, …]]]
)
Where,

  • Constraint_name is the name of the constraint on which the foreign key constraint is to be defined.
  • Col_name is the name of the column on which the foreign key constraint is to be enforced.
  • Table_name is the name of the related table in which the primary key constraint has been specified.
  • Column_name is the name of the primary key column of the related table on which the primary key constraint has been defined.

In the example of the EmployeeLeave table under the HumanResources schema, you need to add the foreign key constraint to enforce referential integrity. In the HumanResources schm,the EmployeeID column is set as a primary key in the Employee table. Therefore, you need to set EmployeeID in the EmployeeLeave table as a foreign key.

In the preceding example, you can use the following statement to apply the foreign key constraint in the EmployeeLeave table:

CREATE TABLE HumanResources.EmployeeLeave
(
EmployeeID int CONSTRAINT fkEmployeeID FOREIGN KEY REFERENCES
HumanResources.Employee (EmployeeID),
LeaveStartDate datetime CONSTRAINT cpkLeaveStartDate PRIMARY KEY (EmployeeID, LeaveStartDate) ,



)

The preceding statement creates the EmployeeLeave table with a foreign key constraint on the EmployeeID column. The name of the constraint is fkEmployeeID.

Unique key Constraints

Linear QUEUE for Data Structure in 'C'

Linear QUEUE:
Linear Queue is a Queue in which the elements are arranged in a linear order, one after the other like the elements of one-dimensional array or linked list. The arrangement of elements and storage is similar to array or linked list, but the insertion and deletion are restricted.
Implementation of Linear QUEUE using one-dimensional array:
                                                    The linear QUEUE can be represented graphically as consecutive blocks when it is implemented using one-dimensional array. N the maximum number of elements can be added into the QUEUE is called as size of QUEUE. To represent a QUEUE an array of size N can be used. The capacity of QUEUE in this case will  be N.

In the above representation the size of QUEUE is 8. FRONT and REAR index variables can be used to do the DELETE and ADD operations respectively. Initially when the QUEUE is empty FRONT and REAR are assigned with a value 0,because of Lower Bound 1 of array. When both are equal to 0, then the QUEUE is said to be empty.
         When first element is added into the QUEUE, REAR and FRONT are both incremented by 1 and the element to be added is placed at REAR index of QUEUE, QUEUE is name of the array i.e.  FRONT<--1,   REAR<--1    and QUEUE[REAR]<--ITEM. ITEM is the element  to be added to QUEUE. In the further addition operation, the REAR is incremented and the ITEM is copied at REAR index. When the REAR is equal to N, the QUEUE, QUEUE is said to be full. If any addition is done when the QUEUE is full, ‘overflow’ occurs.
         When the element is to be deleted from the QUEUE, the element stored in the QUEUE with FRONT index is deleted by copying it in ITEM and the incrementing FRONT by 1. When FRONT is equal to 0, the QUEUE is empty. When the QUEUE is empty and if the deletion operation is done ‘underflow’, occurs. As an example, consider a QUEUE of size8. It is initially represented as follows:
When element is deleted from the QUEUE, ITEM is copied with A, because QUEUE [FRONT] gives A and FRONT is incremented, QUEUE becomes,
When G is added into QUEUE, the QUEUE becomes,

When I is added into the QUEUE, overflow occurs because REAR=8, the size of QUEUE and QUEUE is full. Even though 1 2 3 4 indices are free, the overflow occurs, this is the disadvantage Circular Queue is used.

Wednesday, March 12, 2014

About the Default Edit Action Methods and View in MVC

Earlier article was about the create action of the controller, which in other words used to create/insert a record into the database using an MVC application. This article have a brief description about Edit action created by default, it’s Get and post method.

According to name, Get method is used to get a particular model according to passed id and return to the view that will show that model. The below code will return the details of a particular Student. User can edit particular information in the textboxes shown on the form and click on submit button.

// GET: /Student/Edit
public ActionResult Edit(int id = 0)
{
Student student = db.Students.Find(id); //db is object of our context class
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
The first line is used to find the existing record by the primary key (id) passed as parameter. Checking all the required condition, it will return the selected student on the Edit view. After clicking on the submit button, compiler goes to the post method of the same action i.e. Post Edit action, as written below:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Student student)
{
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
This method have a parameter of type student that was passed through the Get method of the same action. The first two line of this code are attributes used to tell the compiler about this method i.e. this is post method and validates fields on submit method. In the create action we have add the student, but in this case we need to just change the state to Modified to update all the fields provided to the user to edit on the view.

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>……………….
……………….
……………….

This view is strongly typed view because of the first line and have all the fields of student class (only name is shown here, remaining are something like this).

So turn back on our previous discussion that was about the post Edit action. This action will then check the model is valid or not (Validation). If valid then it will update this student in the student table and then save the changes to the database. If not it will return to the same page with the errors generated.

The last line will return to the same action i.e. Edit with the model passed as parameter and of course with validation errors, you can check this to not enter some fields in the form and submit the data.
© Copyright 2013 Computer Programming | All Right Reserved