Kamis, 11 Juli 2013

How to use ImageMap Control in ASP.NET

Introduction

An image map is a picture on a webpage that provides various links , called hotspots, to navigate to other web pages , depending on the place where the user click (on single image). Web Designer frequently use image maps in their websites . An image map can be included in your web page by using the ImageMap Control.

An ImageMap control exists within the System.Web.UI.WebControls namespace. Image maps are often real maps; for example , you can display the map of the USA and define hotspot regions for each of its state and then navigate to the corresponding page containing the information for the selected state.

Public Properties of the ImageMap Class
Enable : Obtains or sets a value indicating whether the control can respond to user interaction.

HotSpotMode : Obtains or sets the default behavior for the HotSpot objects of an ImageMap control when the HotSpot objects are clicked.

HotSpots : Obtains or sets a group of HotSpot objects that represents the defined hotspot regions in an ImageMap Control.

Target : Obtains or sets the target window to show the Web page content when the ImageMap control is clicked.

Public Event of the ImageMap Class
Click : Occurs when a HotSpot object in an ImageMap control is clicked.
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:ImageMap ID="ImageMap1" runat="server" Height="227px" HotSpotMode="Navigate" ImageUrl="~/imagemap.jpg" Width="227px">
            <asp:RectangleHotSpot HotSpotMode="Navigate" Left="10" NavigateUrl="http://www.google.com" Top="100" />
            <asp:RectangleHotSpot HotSpotMode="Navigate" Left="3" NavigateUrl="http://www.microsoft.com" Top="200" />
        </asp:ImageMap>
   
    </div>
    </form>
</body>
</html>
Output
hotspot
 

Rabu, 10 Juli 2013

Address Book Project in c#

Introduction:
Address book stores information of your family member or other person. This project contain some forms such as login form .
login form


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AddressBook
{
    public partial class LoginForm : Form
    {
        Thread appThread;
        public LoginForm()
        {
            InitializeComponent();
        }
        private void loginButton_Click(object sender, EventArgs e)
        {
            if (unameTextBox.Text == "admin" && passwordTextBox.Text == "password")
            {
                this.Close();
                appThread = new Thread(LaunchAdminApplication);
                appThread.SetApartmentState(ApartmentState.STA);
                appThread.Start();
            }
            else
                MessageBox.Show("Invalid Credentials", "Login Error!");
        }
        private void LaunchAdminApplication()
        {
            Application.Run(new MainForm());
        }
    }
}
Here  "unameTextBox" is the name property of the username textbox and "passwordTextBox" is the property of Password text box . if username and password are right then you can enter into new thread. A message box generated if your  username and password are wrong .

manage form




Your project will submit after 5 hour 

Senin, 08 Juli 2013

Crime File management System Project in ASP.NET

Aim of the project
The basic aim of this project is store criminal record ,  search criminal record and find current location of the criminal.

Security of the project :

Project contain username and password field for security with proper validation (client side and server side validation) . Project security screen shot is

crimefile



  <p>
        <h2>Login for security</h2></p>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" BackColor="#CCCC00" BorderColor="Black" BorderStyle="Solid" BorderWidth="4px" />
        <p>
        Enter member username :&nbsp;
        <asp:TextBox ID="usrtxt" runat="server" Width="182px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="usrtxt" ErrorMessage="Enter Username" ForeColor="Maroon">*</asp:RequiredFieldValidator>
    </p>
    <p>
        Enter&nbsp; member password : <asp:TextBox ID="pwd" runat="server" TextMode="Password" Width="182px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="usrtxt" ErrorMessage="Enter password" ForeColor="Maroon">*</asp:RequiredFieldValidator>
    </p>
    <p>
        <asp:Button ID="Button1" runat="server" Text="Member Login" OnClick="Button1_Click" />
    </p>
    <p>
        <asp:Label ID="Label1" runat="server"></asp:Label>


Code behind code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class welcome : System.Web.UI.Page
{
 SqlDataReader rd;
protected void Page_Load(object sender, EventArgs e)
{
}
 protected void Button1_Click(object sender, EventArgs e)
{
 bool flag=true ;
using (SqlConnection con = new SqlConnection())
{
 con.ConnectionString =ConfigurationManager .ConnectionStrings ["ConnectionString"].ToString ();
con.Open ();
using (SqlCommand cmd=new SqlCommand ())
{
 cmd.CommandText ="select * from [Table]";
cmd.Connection =con;
rd=cmd.ExecuteReader (CommandBehavior .CloseConnection);
while (rd.Read ())
{
 if (rd["username"].ToString ().Equals(usrtxt .Text) && rd["password"].ToString ().Equals (pwd .Text))
{
 flag =false ;
 Session ["username"]=rd["username"].ToString ();
break ;
}
}
 if (flag ==true)
 Label1 .Text ="No record found";
else
 Response .Redirect ("~/admin/securepage.aspx");
}
}
}
}
Software requirements of the project are:
  • Visual studio 2013
How to run this project:
  • Open website in  visual studio 2013
  • Run your project
  • ByDefault your username and password are : admin and admin



Your project will submit after 5 hour 


    Minggu, 07 Juli 2013

    Round off value in DataGridView Cell

    In most of the situations programmer want to save value in decimal data type, but try to show that value in approximate format. For example we have a variable "income" that have some different value as below
    income = 3.57 (stored in database exactly)
    income = 3.6 (programmer want to show the user)

    The decimal data type can store maximum of 38 digits. It stores exact representation of the number, there is no approximation of the stored value. Decimal data type can be used to store numbers with decimals when the values must store exactly as specified. We can read more about decimal values from http://msdn.microsoft.com/en-us/library/ms187912(v=sql.105).aspx

    In above case programmer want to round off that value at run time binding. As we know about datagridview binding in which data will be shown as same as they are stored in our database. When we bind our list to datagridview it means we are binding each cell one by one. If we want to check which cell is binding to what value then we can do this in Cell Formatting event of datagridview.

    Datagridview cell formatting event occurs when the contents of a cell need to be formatted for display. If the standard formatting is insufficient, we can customize the formatting by handling this event. This event occurs every time each cell is painted. Go through datagridview cell formatting event if you want to know more about cell formatting event.

    Your cell formatting event should look like the below code if you want to round off all the decimal values bind to this datagridview.

    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    double decValue;
    if (e.Value == null)
    return;
    if (double.TryParse(e.Value.ToString(), out decValue) == false)
    return;
    e.Value = Math.Round(decValue);
    }

    This code will work only with decimal values. It will do nothing if either “value is null” or “value is not type of decimal”. If the value is of type decimal it will round off that value using Math.Round() method. This method rounds a value to its nearest integral value.

    Sabtu, 06 Juli 2013

    How to check minimum or maximum value of integer data type

    According to msdn ,  minimum and maximum value of Integer datatype are : -2,147,483,648 to 2,147,483,647.

    Let's have an example :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace tutorial
    {
       class Program
     {
       static void Main(string[] args)
     {
       int a = 0;
    Console.WriteLine("minimum value of" + int.MinValue);
    Console.WriteLine("maximum value of" + int.MaxValue);
    Console.ReadKey();
     }
     }
    }

    Output
    output
    Here:
     MinValue field Represents the smallest possible value of System.Int32. This field is constant.
    MaxValue field Represents the largest possible value of System.Int32. This field is constant

     

    Rabu, 03 Juli 2013

    Use XML file as database

    Sometimes, when we are creating small application, we have to use SQL Server database if we don’t know about any alternative solution. SQL Server databases are very familiar with programmers, but if our application is small then we don’t let the user install and maintain any database engine.

    We have an alternative solution i.e. XML (Extensible Markup Language) files that is a textual data format. It is widely used for the representation of arbitrary data structures, for example in web services. We can read more about Xml files through http://en.wikipedia.org/wiki/XML.


    To work with XML files we use XmlSerializer class that serializes and deserializes objects into and from XML documents. It is in System.Xml.Serialization namespace and enables us to control how objects are encoded into XML. The most important methods in this class are Serialize and Deserialize.



    In this article we will serialize a class Person consists of public properties, as shown below:

    public class Person
    {
         public string Name { get; set; }
         public string FName { get; set; }
         public int Age { get; set; }

    }

    Write data to XML file:

    To write data in XML file we use Serialize method that serializes the specified object and writes the XML document to a file using the specified stream.It takes two parameters.

    First parameter will be used to write the data to file, it may be either a FileStream or an XmlWriter. Second parameter is the data that is to be written.


    List<Person> personList = new List<Person>();
    XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
    using (FileStream stream = new FileStream(Environment.CurrentDirectory + "\\file.xml", FileMode.Create, FileAccess.Write))
    {
        serializer.Serialize(stream, personList);
    }


    In above code

    • Add some data to personList that are to be written to file.
      personList.Add(new Person() { Name = "Student1", FName = "Father1", Age = 25 });
      personList.Add(new Person() { Name = "Student2", FName = "Father2", Age = 24 });
      personList.Add(new Person() { Name = "Student3", FName = "Father3", Age = 26 });
      personList.Add(new Person() { Name = "Student4", FName = "Father4", Age = 23 });

    • Create an object of XmlSerializer class which will decide the type of data that is to be written (list of person here).
    • Create a stream having FileMode Create and FileAccess Write.
    • At the last serialize the list to stream and stream will write it to xml file.

    Read data from XML file:


    To read data from XML file we use Deserialize method that deserialize the xml document contained by stream. It takes only one parameter i.e. our stream.


    List<Person> accessedData = new List<Person&gt
    XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
    using (FileStream stream = new FileStream(Environment.CurrentDirectory + "\\file.txt", FileMode.Open, FileAccess.Read))
    {
        accessedData = serializer.Deserialize(stream) as List<Person>;
    }
    dataGridView1.DataSource = accessedData;

    In above code
    • Declare a list of person that will hold deserialized data.
    • Create an object of XmlSerializer class which will decide the type of data that is to be read (list of persons here).
    • Create a stream having FileMode Open and FileAccess Read.
    • At the last deserialize the data and convert them to List<Person>.
    After successfully deserialize the data in list, i am bind that list to datagridview. When i run the project, my datagridview look like following screenshot:


    We can read more about Environment.CurrentDirectory

    Selasa, 02 Juli 2013

    How to use CompareValidator Control in ASP.NET

    Introduction
    The CompareValidator control is used to compare the value entered by a user into one input control with the value entered into another input control or with an already existing value. The CompareValidator control exists within the System.Web.UI.WebControls namespace.

    Operator Properties of the CompareValidator Control
    Equal : Checks whether the compared values are equal .

    NotEqual : Checks that controls are not equal to each other.

    GreaterThan : Checks the greater than relationship.

    GreaterThanEqual : Checks for a greater than or equal to relationship.

    LessThan : Checks for a less than relationship.

    LessThanEqual : Checks for a less than or equal to relationship.

    DataTypeCheck : Compare dataTypes between the value entered into the data-entry .

    The Type property is used to specify the data type of both the comparison values, where String is the default type. Both values are automatically converted to the string data type before the comparison operator is performed . The different data types that can be use are as follows

    String : A string data type.
    Integer : An Integer data type.
    Double : A double data type.
    Date : A date data type.
    Currency : A currency data type.

    Public Properties of the CompareValidator class

    ControlToCompare : Obtains or sets the data entry , which has to be compared with the data-entry control.
    Operator : Obtains or sets the comparison operation to perform.

    ValueToCompare: Obtains or sets a constant value to compare with the value entered by a user in the data entry control being validated.

    Example


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

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

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

    <div>






    Enter password :
     
     
    <asp:TextBox ID="pwd" runat="server"></asp:TextBox>

    <br />

    <br />

    Re-Enter:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <asp:TextBox ID="repwd" runat="server"></asp:TextBox>

    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="pwd" ControlToValidate="repwd" ForeColor="Maroon">Password does not match</asp:CompareValidator>

    <br />

    <br />

    <asp:Button ID="Button1" runat="server" Text="Check Comparison" />





     
     
    </div>

    </form>

    </body>

    </html>
     
    Output
    comparevalidator