Wednesday, 16 January 2013

Read Xml file in GridView in asp.net using c#.


In this post I will explain how to read XML file in GridView in asp.net using c#.

Description:-

I have written many post regarding gridView . In this post I will explain how to read a xml file in griview or how to bind xml data to gridview. For this first Create an xml file and put some dummy data in it.
Create a xml file name Employee.xml and insert some data in this file.

Employee.xml:-

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <EmpName>Sushil Kumar</EmpName>
    <EmailId>Sushilct25@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>9998882345</PhoneNo>
  </employee>
  <employee>
    <EmpName>Prdeep Kumar</EmpName>
    <EmailId>pradeep@gmail.com</EmailId>
    <Designation>Manager</Designation>
    <PhoneNo>12344567890</PhoneNo>
  </employee>
  <employee>
    <EmpName>sushil Modi</EmpName>
    <EmailId>modi@gmail.com</EmailId>
    <Designation>Soft. Developer</Designation>
    <PhoneNo>224525426</PhoneNo>
  </employee>
  <employee>
    <EmpName>Rahul Ranjan</EmpName>
    <EmailId>ranjan@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>1245225</PhoneNo>
  </employee>
  <employee>
    <EmpName>Chulbul Pandey</EmpName>
    <EmailId>pandey@gmail.com</EmailId>
    <Designation>Economist</Designation>
    <PhoneNo>235235353</PhoneNo>
  </employee>
</employees>

After that add a web page to the project and drag  a GidView on this page like this..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read Xml File In GridView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84"
            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
            CellSpacing="2">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

After that add the following namespace  in codebehind:-

using System.Data;
using System.Xml;

Code behind page:-
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    public void BindGridView()
    {
        XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("Employee.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlReader);
        xmlReader.Close();
        if (ds.Tables.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columnCount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
            GridView1.Rows[0].Cells[0].Text = "No Record Found....";
        }
    }

Output  like this:-


Sunday, 13 January 2013

Read Xml file in DropDownList in asp.net using c#.


In this post I will explain how to read XML file in DropDownList in asp.net using c#.

Description:-


I have written many post regarding DropDownList. In this post I will explain how to read a xml file in DropDownList or how to bind xml data to DropDownList. For this first Create an xml file and put some dummy data in it.

Create a xml file name Employee.xml and insert some data in this file.

Employee.xml:-

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <EmpName>Sushil Kumar</EmpName>
    <EmailId>Sushilct25@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>9998882345</PhoneNo>
  </employee>
  <employee>
    <EmpName>Prdeep Kumar</EmpName>
    <EmailId>pradeep@gmail.com</EmailId>
    <Designation>Manager</Designation>
    <PhoneNo>12344567890</PhoneNo>
  </employee>
  <employee>
    <EmpName>sushil Modi</EmpName>
    <EmailId>modi@gmail.com</EmailId>
    <Designation>Soft. Developer</Designation>
    <PhoneNo>224525426</PhoneNo>
  </employee>
  <employee>
    <EmpName>Rahul Ranjan</EmpName>
    <EmailId>ranjan@gmail.com</EmailId>
    <Designation>Engg.</Designation>
    <PhoneNo>1245225</PhoneNo>
  </employee>
  <employee>
    <EmpName>Chulbul Pandey</EmpName>
    <EmailId>pandey@gmail.com</EmailId>
    <Designation>Economist</Designation>
    <PhoneNo>235235353</PhoneNo>
  </employee>
</employees>

After that add a web page to the project and drag  a dropdownlist on this page like this..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read Xml file in DrpDownList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

After that add the following namespace  in codebehind:-

using System.Data;
using System.Xml;

Code behind page:-

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDownList();
        }
    }
    public void BindDropDownList()
    {
        XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("Employee.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlReader);
        xmlReader.Close();
        if (ds.Tables.Count != 0)
        {
            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "EmpName";
            DropDownList1.DataValueField = "EmpName";
            DropDownList1.DataBind();
        }
       
    }

         Output Like this..



If you like this post please spread this among in your friends.


Wednesday, 9 January 2013

Login example using stored procedure in asp.net Part-2.


In the previous  I had described tables and stored procedure in” Login example using stored procedurein asp.net Part-1.
In this part I will describe the remaining part . In this section create a separate connection class and add four web page in your project. Create the hierarchy like this..
 

Code for Connection class:-
Connection.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Connection
/// </summary>
public class Connection
{
    public SqlConnection con = new SqlConnection("server=SUSHIL-PC;database=Login ;Integrated Security=true;");
                public Connection()
                {
                                //
                                // TODO: Add constructor logic here
                                //
                }
    public  void ConnOpen()
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
    public void ConnClose()
    {
        try
        {
            if (con.State == ConnectionState.Open)
            {
                con.Dispose();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }
}

Design Login Page Like this.



Login .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;
using System.Security;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    Connection sqlConn = new Connection();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
       
        SqlCommand com = new SqlCommand("sp_checkUser",sqlConn.con); // sp_checkUser is stored procedure
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("userName",txtUseName.Text);
        SqlParameter p2 = new SqlParameter("pass", txtPass.Text);
        com.Parameters.Add(p1);
        com.Parameters.Add(p2);
        sqlConn.ConnOpen();
        SqlDataReader dr = com.ExecuteReader();
        try
        {
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Response.Redirect("Home.aspx");
                }
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "You are Existing User";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
           sqlConn.ConnClose();
        }
    }
}

Design NewUser page like this:-



NewUser.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 _Default : System.Web.UI.Page
{
    Connection sqlConn = new Connection();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void txtUserName_TextChanged(object sender, EventArgs e)
    {
        if (txtUserName.Text != string.Empty)
        {
            SqlCommand com = new SqlCommand("sp_userNameAvailability",sqlConn.con);
            com.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("userName", txtUserName.Text);
            //p1.Value = txtUserName.Text.Trim().ToString();
            com.Parameters.Add(p1);
            sqlConn.ConnOpen();
            SqlDataReader dr = com.ExecuteReader();
           // int result = Convert.ToInt32(com.ExecuteScalar());
           
            if (dr.HasRows)
            {
                dr.Read();
                lblMessage.Visible = true;
                lblMessage.Text = "User Name not Available";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = " Available";
                lblMessage.ForeColor = System.Drawing.Color.Green;
            }
            sqlConn.ConnClose();
            
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        SqlCommand com = new SqlCommand("sp_newUser", sqlConn.con);
        com.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("userName", txtUserName.Text);
        SqlParameter p2 = new SqlParameter("firstName", txtFirstNAme.Text);
        SqlParameter p3 = new SqlParameter("lastName", txtLastName.Text);
        SqlParameter p4 = new SqlParameter("pass", txtPass.Text);
        SqlParameter p5 = new SqlParameter("emailId", txtEmailId.Text);
        SqlParameter p6 = new SqlParameter("securityQuestion", txtSecurityQuestion.Text);
        SqlParameter p7 = new SqlParameter("answer", txtAnswer.Text);

       
        com.Parameters.Add(p1);
        com.Parameters.Add(p2);
        com.Parameters.Add(p3);
        com.Parameters.Add(p4);
        com.Parameters.Add(p5);
        com.Parameters.Add(p6);
        com.Parameters.Add(p7);
        sqlConn.ConnOpen();

                int res = com.ExecuteNonQuery();
       
        sqlConn.ConnClose();
        if (res > 0)
        {
            lblRegister.Visible = true;
            lblRegister.Text = "Your Registration Successfully";
            lblRegister.ForeColor = System.Drawing.Color.Green;
            Response.Redirect("Registration_Confirm.aspx");
        }
        else
        {
            lblRegister.Visible = true;
            lblRegister.Text = "Registration Unsuccessfull";
            lblRegister.ForeColor = System.Drawing.Color.Red;
        }
    }
}

Add two more page just for giving a message to user like regiatration confirmation and Home page.

When you click on login button and after successful  login it will redirect to page Home.aspx. If  click on register button then it will redirect to registration_confirm.aspx  .

If any problem regarding this post free to contact me . My email id sushilct86@gmail.com .

Click Here to download the code.

Sunday, 6 January 2013

Login example using stored procedure in asp.net Part-1.


In this I will explain login example using stored procedure in asp.net.

After  good reponse of my previous post “ Login xample in asp.net”  , I have decide to write a post for login using stored procedure in asp.net. You will be familier to stored procedure because I have used many procedure in my previous post. So I think that you know how to create stored procedure in sql server . In this post I will explaine each and every thing in simple language.
 First create three tables in sqlserver.
1     1.)    First Table :- checkUser

CREATE TABLE [dbo].[checkUser](
     [userName] [nvarchar](50) NOT NULL,
     [pass] [nvarchar](50) NOT NULL
        )

2    2.)    Second Table :- checkUserAvailability
CREATE TABLE [dbo].[userNameAvailability](
     [userName] [nvarchar](50) NOT NULL
)

3    3.)    Third Table :- newUser
CREATE TABLE [dbo].[newUser](
     [userName] [nvarchar](50) NOT NULL,
     [firstName] [nvarchar](50) NOT NULL,
     [lastName] [nvarchar](50) NOT NULL,
     [pass] [nvarchar](50) NOT NULL,
     [emailId] [nvarchar](50) NOT NULL,
     [securityQuestion] [nvarchar](50) NOT NULL,
     [answer] [nvarchar](50) NOT NULL
)

After that create a stored procedure for these three tables.  Here  I will use stored procedure because procedure have amny advantages over simple sql query.
Advantage of stored procedure:-
Click here .
Now it’s time to make stored procedure:-
For  Table checkUser:-
CREATE PROCEDURE sp_checkUser
(
@userName varchar(50),
@pass varchar(50)
)
AS
BEGIN
     select * from checkUser where userName=@userName and pass=@pass;
END
            GO
For Table userNameAvailability :-
CREATE PROCEDURE sp_userNameAvailability
(
@userName varchar(50)
)
AS
BEGIN
     select * from userNameAvailability where userName=@userName;
END
            GO
For Table newUser :-
CREATE PROCEDURE sp_newUser
(
@userName varchar(50),
@firstName varchar(50),
@lastName varchar(50),
@pass varchar(50),
@emailId varchar(50),
@securityQuestion varchar(50),
@answer varchar(50)
)
AS
BEGIN
     insert into newUser(userName,firstName,lastName,pass,emailId,securityQuestion,answer) values(@userName,@firstName,@lastName,@pass,@emailId,@securityQuestion,@answer);
END
            GO
                                                                                                      to be continued………

Friday, 4 January 2013

Download jquery-1.7.2.min.js or jquery-1.8.2.min.js file

In this post I have given a link to download jquery 1.7.2.min.js and 1.8.2.min.js file.

Download Links:-

For 1.7.2.min.js :- jquery1.7.2.min.js or click here
For 1.8.2.min.js :- jquery-1.8.2.min.js or click here