Wednesday, 19 December 2012

Auto Redirect to another page after some time in Asp.Net.


In this post I will explain  how to Auto Redirect to another page after some time in Asp.Net.

Description:-

In the previous article I had explained ‘Auto Refresh webpage after some time in asp.net’. Auto Redirect to another page can be done by various method i.e we can write the code in client side as well as in server side. Here in this post I have explain in simple code. Just add the following code in header section of the page.

<meta http-equiv="refresh" content="10,url=”Http://www.google.com”" />
Here 10 is time in seconds after which page will redirect to goggle.com.

Monday, 17 December 2012

Auto Refresh webpage after some time in asp.net.


In this post I will explain you how to auto refresh web page in asp.net.

Description:-

AutoRefresh  means web page reload the same page after some time. You have seen  google news is auto refresh after some time. If we change something in web-page then it automatically load the data when the will refresh.
There are two methods to refresh the page in asp.net.

First:-
You can add the following code in the header section in web-page in asp.net.
<meta http-equiv="refresh" content="10" />

Where 10 refers no of seconds after that page will refresh.
 This code will be also work for java,php ,perl and  HTML .

Second:-  Server side code
You can add thefollowing code in page-load function in code behind page in asp.net.

Response.AddHeader("refresh","10");

Friday, 14 December 2012

How to use Hard Disk Space as Virtual RAM in Window 7.


In this article I will explain how to use Hard Disk free space as Virtual RAM in windows 7.

Description:-

This is useful for those person whose laptop or Desktop RAM is below 2GB because If you have 3 or 4 GB RAM in their system then it sufficient for Laptop or Desktop . If  you have less than 2 GB RAM then it is difficult to operate huge program in system. Some people have problem or they are not able to add extra ram in their system . So those people use hard disk free space as virtual ram. So please follow the following step which is given below.
For  Window 7/Vista:-
·         Right click on computer icon on desktop and click on properties.
·         Click Advance System Setting.
·         Click on Advance tab , then Click on Settings in perform section.
·         Click on Advance tab and then click on Change Button.
·         Uncheck checkbox and click on custom size  and click on set button .
·         Set Initial Size =1024 and Maximum Size= 4096
·         Click ok button.
See the Figure below. You will better understand….




After that you Restart your system .

For Window XP:-
·         Right click on My Computer and click on properties.
·         Click Advance System Setting.
·         Click on Advance tab , then Click on Settings in perform section.
·         Click on Advance tab and then click on Change Button in virtual memory section.
·         Select drive then select custom size , set initial size and maximum size , clock on set button and click ok.

*  Set Initial and maximum same . 

Thursday, 13 December 2012

How to check user name using Ajax in Asp.Net.


In this article I will explain how to check user name availability using in Asp.Net.
Decription:-
User name check is required before you register first time  because it may be possible user name exist. It is widely used in website for registration . For this I have taken a textbox and one button in wenpage. When user type user name in textbox and when he leave the texbox it automatically check user name . For automatic check user name I have used Ajax  in Asp.Net.
Default.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"
                ontextchanged="TextBox1_TextChanged" ></asp:TextBox><asp:Label ID="lblmessge" runat="server"
                    ></asp:Label>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

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

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if(TextBox1.Text!=string.Empty)
        {
            SqlConnection con = new SqlConnection(@"Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True");
            SqlCommand com = new SqlCommand("select count(*) from user_registration where user_name=@user_name",con);
            SqlParameter sqlp = new SqlParameter("@user_name",SqlDbType.VarChar);
            sqlp.Value = TextBox1.Text.Trim().ToString();
            com.Parameters.Add(sqlp);
            con.Open();
            int res =Convert.ToInt32( com.ExecuteScalar());
            con.Close();
            if (res >= 1)
            {
                lblmessge.Text = "User Name not Available";
                lblmessge.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lblmessge.Text = " Available";
                lblmessge.ForeColor = System.Drawing.Color.Green;
            }
        }
    }
}

Debug code and Run.

Wednesday, 12 December 2012

Open a page in New Window using JavaScript in Asp.Net


In this Post I will explain how to open page a new window using Jscript in Asp.Net.
Description:-
You have seen in various website when you click on link ,  it open the page in new window. Here I have used a javaScript code to open new window in Asp.Net and many article related  to jquery and javaScript.
JavaScript code:-
<script type="text/javascript">
         function openWindowPopUp() {
             var popUpUrl = 'PopUpWindow.aspx?fn=' + document.getElementById('<%= lblFirstName.ClientID %>').innerHTML +'&ln'+ document.getElementById('<%= lblLastName.ClientID %>').innerHTML;
             var name = 'popUp';
             var appearence = 'dependent=yes,menubar=no,resizable=no,' +
                              'status=no,toolbar=no,' +
                              'left=5,top=280,width=230px,height=200px';
             var openNewWindow = window.open(popUpUrl, name, appearence);
             openNewWindow.focus();

         }

    </script>

Design Page code:-
Create a Default.aspx page and write the following code which is given below.
Default.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript">
         function openWindowPopUp() {
             var popUpUrl = 'NewPopUpWindow.aspx?fn=' + document.getElementById('<%= lblFirstName.ClientID %>').innerHTML +'&ln'+ document.getElementById('<%= lblLastName.ClientID %>').innerHTML;
             var name = 'popUp';
             var appearence = 'dependent=yes,menubar=no,resizable=no,' +
                              'status=no,toolbar=no,' +
                              'left=5,top=280,width=230px,height=200px';
             var openNewWindow = window.open(popUpUrl, name, appearence);
             openNewWindow.focus();

         }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    First Name:<asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label><asp:TextBox ID="TextBox1"
            runat="server"></asp:TextBox><br />
       Last Name:<asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label><asp:TextBox
                ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button"  />
    </div>
    </form>
</body>
</html>

Write code behind page for Default.aspx .
Default.aspx.cs:-
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)
    {

        string updateValue = @"function update(popvalues)
        {
            document.getElementById().innerHTNL=popvalues[0];
            document.getElementById().innerHTNL=popvalues[1];
        }";
        this.ClientScript.RegisterStartupScript(Page.GetType(), "updateValue", updateValue.ToString(), true);
        Button1.Attributes.Add("onclick", "openWindowPopUp('NewPopUpWindow.aspx')");
    }

}

After that create second page which will be populate when you click on button1.
NewPopUpWindow.aspx:-
<body>
    <form id="form1" runat="server">
    <div>
       First Name:<asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label><asp:TextBox ID="TextBox1"
            runat="server"></asp:TextBox><br />
       Last Name:<asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label><asp:TextBox
                ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button"  />
    </div>
    </form>
</body>

Debug code and Run.

Tuesday, 11 December 2012

Cascaded DropDown in Asp.net.


In this post I will explain Cascaded DropDown in Asp.Net.
Description:-

Cascaded DropDown means when we select one dropdownlist then the second dropdownlist will bind the data according to selection of first dropdownlist. In simple word when we select country in one dropdownlist then in second dropdownlist city will come according to country. It’s better to understand with pracrical example so follow the code.
First make the three table country state and city.

Country Table:-

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[country](
                [country_id] [int] NOT NULL,
                [country_name] [varchar](max) NOT NULL,
 CONSTRAINT [PK_country] PRIMARY KEY CLUSTERED
(
                [country_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

State Table :-

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[state](
                [state_id] [int] NOT NULL,
                [state_name] [varchar](max) NOT NULL,
                [country_id] [int] NOT NULL,
 CONSTRAINT [PK_state] PRIMARY KEY CLUSTERED
(
                [state_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[state]  WITH CHECK ADD  CONSTRAINT [FK_state_country] FOREIGN KEY([country_id])
REFERENCES [dbo].[country] ([country_id])
GO

ALTER TABLE [dbo].[state] CHECK CONSTRAINT [FK_state_country]
GO

City Tabel:-

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[city](
                [city_id] [int] NOT NULL,
                [city_name] [varchar](max) NOT NULL,
                [state_id] [int] NOT NULL,
 CONSTRAINT [PK_city] PRIMARY KEY CLUSTERED
(
                [city_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[city]  WITH CHECK ADD  CONSTRAINT [FK_city_city] FOREIGN KEY([state_id])
REFERENCES [dbo].[state] ([state_id])
GO

ALTER TABLE [dbo].[city] CHECK CONSTRAINT [FK_city_city]
GO

Run the following code in your database.
Now create web page.

DropDown.aspx:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
<tr>
<td colspan="2" height="200px" valign="bottom">
<h3>Cascading Dropdowns Sample</h3>
</td>
</tr>
<tr>
<td>
Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select Region:
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server"
        onselectedindexchanged="ddlRegion_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
</table>
    </div>
    </form>
</body>
</html>

Code behind page:-

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

public partial class DropDown : System.Web.UI.Page
{
    private String strConnection = @"Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindContrydropdown();
        }
    }
    protected void BindContrydropdown()
    {
        //conenction path for database
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from country", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        ddlCountry.DataSource = ds;
        ddlCountry.DataTextField = "country_name";
        ddlCountry.DataValueField = "country_id";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));

    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from state where country_id=" + CountryID, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        ddlState.DataSource = ds;
        ddlState.DataTextField = "state_name";
        ddlState.DataValueField = "state_id";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        if (ddlState.SelectedValue == "0")
        {
            ddlRegion.Items.Clear();
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        int StateID = Convert.ToInt32(ddlState.SelectedValue);
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from city where state_id=" + StateID, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        ddlRegion.DataSource = ds;
        ddlRegion.DataTextField = "city_name";
        ddlRegion.DataValueField = "city_id";
        ddlRegion.DataBind();
        ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        if (ddlState.SelectedValue == "0")
        {
            ddlRegion.Items.Clear();
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }
    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
Debug and Run.