-->

Sunday, February 9, 2014

How to get Total traveling time of Train in c# programming

Its a very easy concepts, finding traveling time, which is travel by a train. Suppose your train arrival Date and time is 2/9/2014  10:47:00 PM and departure date time is 2/8/2014 6:32:00 PM . Now, you want to get total traveling time. Use TimeSpan Structure for getting elapsed time. Lets take an simple example.

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

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime departure = new DateTime(2014, 2, 8, 18, 32, 0);
            DateTime arrival = new DateTime(2014, 2, 9, 22, 47, 0);
            TimeSpan travelTime = arrival - departure;
            Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
            Console.ReadKey();
        }
    }
}

Code generate the following output

How to get Total traveling time of Train in c# programming

Saturday, February 8, 2014

DropDownList AutoPostBack, Item, Index and Value in asp.net c#

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 use of DropDownlist article was displayed on the dotprogramming page. Following are the articles that I have written so far for beginners.

In this article, we will get DropdownList item text , index value and value of it. Generally, we know that it is bind with ListItem class properties. 

Lets take an simple example 


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

<!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">
    <div>
        <strong>Get DropDownList Control&nbsp; Item, Index and value </strong>
        <br />
    </div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        Height="42px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
        Width="138px">
            <asp:ListItem>Select Country</asp:ListItem>
            <asp:ListItem Value="Top Country">USA</asp:ListItem>
            <asp:ListItem Value="United State">UK</asp:ListItem>
            <asp:ListItem Value="awsome ">Itly</asp:ListItem>
        </asp:DropDownList>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

// Code behind code


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

public partial class Dropdownlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "You selected: <br /> Dropdownlist Text: " +
            DropDownList1.SelectedItem.Text.ToString() +
            "<br />Dropdown Value: " + DropDownList1.SelectedValue.ToString() +
            "<br />Dropdown Index: " + DropDownList1.SelectedIndex.ToString(); 
    }
}

Code generate the following output

DropDownList AutoPostBack, Item, Index and Value in asp.net c#

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

© Copyright 2013 Computer Programming | All Right Reserved