Hi indradeo,
Check this example. Now please take its reference and correct your code.
HTML
Login
<div class="box">
<h1>Contract Closure Login Screen</h1>
<div style="text-align: center;">
<table align="center">
<tr>
<td class="auto-style4">User id</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" class="email"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4">Password:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" class="email"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Login" CssClass="auto-style5" Height="28px" Width="111px" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
</div>
Default
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnRowEditing="OnRowEditing" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfUser" runat="server" Value='<%# Eval("UserName") %>' />
<asp:Button ID="btn_Edit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="btn_Cancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Closed" runat="server" CommandName="Closed" Text="Closed" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
Code
Login
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Users where Username=@username and password=@password", con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Session["UserName"] = TextBox1.Text;
Response.Redirect("Default.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGridView();
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
private void BindGridView()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Users", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hfUserName = e.Row.FindControl("hfUser") as HiddenField;
Button btnEdit = e.Row.FindControl("btn_Edit") as Button;
Button btnClose = e.Row.FindControl("btn_Closed") as Button;
btnEdit.Visible = hfUserName.Value.Trim().ToLower() == Convert.ToString(Session["UserName"]).ToLower() ? true : false;
btnClose.Visible = hfUserName.Value.Trim().ToLower() == Convert.ToString(Session["UserName"]).ToLower() ? true : false;
}
}
Screenshot