-->

Friday, February 7, 2014

How to generate dynamic page title in ASP.NET

Page control provides page parameters, such as title, metaDescription etc. You can access all the parameters using Page Control in code file. First Check page URL on Page_Load method using Request object. Lets take an simple example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Page.Request .Url.ToString () =="http://localhost:11596/WebSite13/Default.aspx")
        {
            Page.Title = "Welcome to dotprogramming";
            Page.MetaDescription = " Its Cover all programming language";
            Response.Write("<h2>Dynamic Page title </h2>");


        }
    }
}

The code generate the following output

How to generate dynamic page title in ASP.NET

How to generate dynamic page title in ASP.NET

Thursday, February 6, 2014

How to Create Database and Add in the MVC Application

In MVC application, to store our data for further references, we have to create a database and use that for SQL querying. Earlier we have created the database using Entity Framework that was a code first approach, in this article we will create a Database first and then code to use that.

  • Right click on the App_Data folder and Add New Item.
  • Under Visual C# in left side tree, click on Data node and select SQL Server Database.
  • The default name for the database is Database1 with .mdf file extension and it may change (I have change it StudentDb.mdf). Click on Add to add the database in our application.

How to Create Database and Add in the MVC Application

As soon as we create this database, our server explorer have a connection with the same as shown.

How to Create Database and Add in the MVC Application

  • Write click on the Table node under newly created database in Server Explorer, and click on Add New Table
  • A design will open with three columns, first for name, second for data type and the last one for Allow nulls or not.
  • Add some Fields in this design as I have added below:

How to Create Database and Add in the MVC Application

While we look in the T-SQL of this table creation, just under the design window i.e.

CREATE TABLE [dbo].[Student]
(
[Id] INT NOT NULL PRIMARY KEY, 
[Name] VARCHAR(50) NOT NULL, 
[Age] INT NOT NULL, 
[City] VARCHAR(50) NULL
)

Change the database name from this T-SQL (Student here) and click on the Update button at left upper of the designer. It will show Preview Database Update window, click on Update Database button and your changes have been saved to database, means Student table have been created in the database.

Add this connection string in your web.config file to establish the connection with the database.

<add name="StudentConnection" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\StudentDb.mdf;Integrated Security=True"/>

The database have been created and added in our project by this connection string, we will do some insert, update and delete with this database in later articles.

How to Querying Data by using Subqueries in SQL Programming

In SQL programming, while using subqueries, you can use the =,>, and < comparison operators to create a condition that checks the value returned by the subquery. When a subquery returns more than one value, you might need to apply the operators to all the values returned by the subquery. To perform this task, you can modify the comparison operators in the subquery. The SQL Server provides the ALL and ANY keywords that can be used to modify the existing comparison operators.

The ALL keyword returns a TRUE value, if all the values that are retrieved by the subquery satisfy the comparison operator. It returns a FALSE value if only some values satisfy the comparison operator or if the subquery does not return any rows to the outer statement.

The ANY keyword returns a TRUE value if any value that is retrieved by the subquery satisfies the comparison operator. It returns a FALSE value if no values in the subquery satisfy the comparison operator or if the subquery does not return any rows to the outer statement.

The following list shows the operators that can be used with the ALL and ANY keywords:

  • >ALL, greater than the maximum value in the list.
    The expression|column_name> ALL (10, 20, 30) means ‘greater than 30’
  • >ANY, greater than the minimum value in the list.
    The expression|column_name >ANY (10, 20, 30) means ‘greater than 10’
  • =ANY, any of the values in the list. It acts in the same way as the IN clause.
    The expression|column_name = ANY (10, 20, 30) means ‘equal to either 10 or 20 or 30’
  • <>ANY, not equal to any value in the list.
    The expression|column_name <>ANY (10, 20, 30) means ‘not equal to 10 or 20 or 30’
  • <>ALL, not equal to all the values in the list. It cats in the same way as the NOT IN clause.
    The expression|column_name <>ALL (10, 20, 30) means ‘not equal to 10 and 20 and 30’

The following example displays the employee ID column and the title of all the employees whose vacation hours are more than the vacation hours of employees designated as Recruiter:

SELECT BusinessEntityID, JobTitle
FROM HumanResources.Employee
WHERE VacationHours >ALL (SELECT VacationHours
FROM HumanResources.Employee WHERE JobTitle = 'Recruiter')

In the preceding example, the inner query returns the vacation hours of all the employees who are titled as recruiter. The outer query uses the ‘>ALL’ comparison operator. This retrieves the details of those employees who have vacation hours greater than all the employees titled as recruiter.

The output of the query is displayed in the following figure.

How to Querying Data by using Subqueries in SQL Programming

Wednesday, February 5, 2014

How to Querying Data by using Subqueries in SQL Programming

IN

In SQL programming, if a subquery returns more than one value, you might need to execute the outer query if the values within the column specified in the condition match any value in the result set of the subquery. To perform this task, you need to use the IN keyword.
The syntax of using the IN keyword is:

SELECT column, column [,column]
FROM table_name
WHERE column [ NOT ] IN
(SELECT column FROM table_name [WHERE
Conditional_expression])

Consider an example. You need to retrieve the BusinessEntityID attribute of all the employees who live in Bothell, from the EmoloyeAddress table in the Adventure Works database.

To perform this task, you need to use a query to obtain the AddressID of all the addresses that contain the word Bothell. You can then obtain the BusinessEntityID from the Employee table where the AddresID matches any of the AddressIDs returned by the previous query.
To perform this task, you can use the following query:

SELECT BusinessEntityID FROM Person.BusinessEntityAddress
WHERE AddressID IN (SELECT AddressID FROM Person.Address WHERE City = 'Bothell')

The output of the subquery will show all the ID falls in the category.

EXISTS

You can also use a subquery to check if a set of records exist. For this, you need to use the EXISTS clause with a subquery. The EXISTS keyword, always returns a TRUE or FALSE value.
The EXISTS clause checks for the existence of rows according to the condition specified in the inner query and passes the existence status to the outer query. The subquery returns a TRUE value if the result of the subquery contains any row.

The query introduced with the EXISTS keyword differs from other queries. The EXISTS keyword is not preceded by any column name, constant, or other expression, and it contains an asterisk (*) in the SELECT list of the inner query. The syntax of the EXISTS keyword in the SELECT query is:

SELECT column, column [column]
FROM table_name
WHERE EXISTS (SELECT column FROM table_name [WHERE
Conditional_expression] )

Consider an example. The users of AdventureWorks, Inc. need a list containing the BusinessEntityID and Title of all the employees who have worked in the Marketing department at any point of time. The department ID of the Marketing department is 4.
To generate the required list, you can write the following query by using the EXISTS keyword:

SELECT BusinessEntityID, Title FROM HumanResources.Employee
WHERE EXISTS
(SELECT * FROM HumanResources.EmployeeDepartmentHistory WHERE
BusinessEntityID = HumanResources.Employee. BusinessEntityID AND DepartmentID = 4)

The following figure displays the output generated by the query.

How to Querying Data by using Subqueries in SQL Programming

A subquery must be enclosed within parentheses and cannot use the ORDER BY or the COMPUTE BY clause.

Make Shutdown timer in C# winforms

Introduction

First of all, I would like to thank all of the readers who have read my previous articles. What a great support i have got from you people.I really felt great when binding navigator article was displayed on the dotprogramming page. Today we will learn, How to create Shutdown timer in c#

Design pattern

Step-1 : Add One timer control onto the winform. Also set two property

Enabled =True;
Interval=1000

Step-2 : Add Two NumericUpDown control onto the form.

Code Pattern

Step-1 : Handle Timer_Tick Event . Copy this code and paste into your Timer_Tick event.

            int a = DateTime.Now.Minute;
            int b = DateTime.Now.Hour;
            int c = Convert.ToInt32 (numericUpDown1.Value);
            int d = Convert.ToInt32(numericUpDown2.Value);


            if (a == d && b == c)
            {
                Process.Start("shutdown", "/s /t 0");
            }

Note : Add  System.Diagnostics namespace for Process class. Here DateTime class use 24 hour time , i want to tell you that your system watch display 8:20 PM.
 Here DateTime Class represents Hour and minute. look like
8 pm (System watch)   = 20 (According to DateTime class)



Code generate the following output
Make Shutdown timer in C# winforms

Tuesday, February 4, 2014

How to Querying Data by using Subqueries in SQL Programming: Part 1

In SQL programming, while querying data from multiple tables, you might need to use the result of one query as an input for the condition of another query. For example, in the Adventure Works database, you need to view the designation of all the employees who earn more than the average salary. In such cases, you can use subqueries to assign values to the expressions in other queries.

A subquery is an SQL statement that is used within another SQL statement. Subqueries are nested inside the WHERE or HAVING clause of the SELECT, INSERT, UPDATE, and DELETE statements. The query that represents the parent query is called an outer query, and the query that represents the subquery is called an inner query. The database engine executes the inner query first and returns the result to the outer query to calculate the result set.

Depending on the output generated by the subquery and the purpose for which it is to be used in the outer query, you can use different keywords, operators, and functions in subqueries.

Using the IN and EXISTS keywords

A subquery returns values that are used by the outer query. A subquery can return one or more values. Depending on the requirement, you can use these values in different ways in the outer query.

For example, in the Adventure Works database, you need to display the department name for an employee whose BusinessEntityID is 46. To perform this task, you can use the following query:

SELECT Name FROM HumanResources.Department
WHERE DepartmentID =
(SELECT DepartmentID FROM HumanResources.EmployeeDepartmentHistory WHERE BusinessEntityID = 46 AND EndDate IS NULL)

In the preceding query, the inner subquery returns the DepartmentID column of the emoloyee with BusinessEntityID as 46. Using this DepartmentID, the outer query returns the name of the department from the Department table i.e. Production.

In this query, EndDate is NULL Signifying that you need to extract the ID of the department where the emoloyee is currently working.

In the preceding example, the subquery returns a single value. However, at times, you need to return more than one value from the subquery. In addition, you might need to use a subquery only to check the existence of some records and based on that you need to execute the outer query.

You can specify different kinds of conditions on subqueries by using the following keywords:

Comments in c# console application

Comments are non-executale statements. Generally created for making user-friendly application. If we talk about c# comments, there are three types.

first one single line second one is multi-line and last one is XML documentation comments. Look like

Single-line Comments   --- // (created through double slash)
Multi-line Comments  ---- /*   */  
XML Documentation comments --- ///

Note : Don't try to comment every line of code. Use comments only for code, which is difficult to understand.

If you want to create comment in c# application , you can use keyboard shortcut. CTRL+K with C for comment, CTRL+K with U for un-comment.

Lets create a simple application demo for understanding  all types of comments.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Addtwonumber qd = new Addtwonumber();

            //int number = 10;

            /*if (number == 10)
            {
                Console.WriteLine(number);


            }*/
        }
    }
  /// <summary>
  /// This class is designed for adding two number
  /// </summary>
    public class Addtwonumber
    {

    }
}
Code generate the following output
This example cover all types of comments. According to screen snap, you can better understand XML comments. If you don't use XML comment , before creating the class. Intellisense doesn't dispay the summary of the class. 

© Copyright 2013 Computer Programming | All Right Reserved