Wednesday, 18 April 2012

How to show record in grid view only login user?

Introduction:-Here we will learn how to show database record in gridview when submit only login user in asp.net application ? Here we can learn how to show individual record with the help of session . so you can try code asp.net because this code is very useful in your application .



Here we will design  login page in asp.net design page-  


<table class="style1" style="background-color: #C0C0C0">
    <tr>
        <td class="style3">
            &nbsp;</td>
        <td class="style4">
            <strong>Welcome to Login </strong>
        </td>
    </tr>
    <tr>
        <td class="style3">
            User Name</td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server" BorderStyle="Groove"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style3">
            Password</td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server" BorderStyle="Groove"
                TextMode="Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login"
                BorderStyle="Groove" ForeColor="#6600CC" />
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
</table>

first of all we will write namespace and than create connection path with the help of web config page , Here write a code of login page and give user information in session. imporatant note:"if u have two table so comman field must be in table" .

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class Default : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        string s = ConfigurationManager.ConnectionStrings["conn"].ToString();
        con = new SqlConnection(s);
    }
    SqlDataReader dr1;
    protected void Button1_Click(object sender, EventArgs e)
    {
         con.Open();
        SqlCommand cmd1 = new SqlCommand("select * from rahi(table name) where User_id=@User_id and Password=@Password", con);
        cmd1.Parameters.AddWithValue("@User_id", TextBox1.Text);
        cmd1.Parameters.AddWithValue("@Password", TextBox2.Text);

        dr1 = cmd1.ExecuteReader();

        if (dr1.Read())
        {
            Session["User_id"] = dr1[1].ToString();

            Session["Email_id"] = dr1[4].ToString();
            //Session["Name"] = dr1[3].ToString();
            Response.Redirect("Default2.aspx");
        }
        else
        {
            Response.Write("<script> alert('No data has been selected');</script>");
            TextBox1.Text = "";
            TextBox2.Text = "";
        }
        con.Close();
    }
    }

Here add new page (Default2.aspx) design page in same web application and write design code of gridview and set property size whatever you want ....

<div style="overflow: scroll" >
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
        GridLines="None" Width="831px">
        <AlternatingRowStyle BackColor="White" />
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#3692C7" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#3692C7" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView></div>







Now fetch record in gridview with the help of session please make a two table so comman field must be in table for Example Email_id is same both table.. 

Default2.aspx.cs


using System.Data.SqlClient;
using System.Configuration;
using System.Data;  


public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con;
    string s = ConfigurationManager.ConnectionStrings["conn"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        con = new SqlConnection(s);
        lblwelcome.Text = "Welcome   " + Session["User_id"] + "!    You have been registered Now!";
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select * from rani(table name 2 note: if u have two table so comman field must be in table) where  Email_id='" + Session["Email_id"] +  "'", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
       GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
}

No comments:

Post a Comment

What is ASP.NET? Components of ASP.NET

ASP.Net Definition -It is used for creating web-based applications.ASP.Net is a web development platform provided by Microsoft. ASP.NET i...