You're binding the
GridView
in the
SelectedIndexChanged
event for the same
GridView
. Since the grid doesn't have any data, that event will never fire.
Try binding the grid from the
Page_Load
event instead. You'll also want to wrap it in an
if (!IsPostBack) { ... }
block.
using System;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTheGrid();
}
}
private void BindTheGrid()
{
using (SqlConnection con = new SqlConnection ("data source=.; database=sample ; integrated security=SSPI; "))
using (SqlCommand cmd = new SqlCommand("Select * from Login_Info", con))
{
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
GridView1.DataSource = rdr;
GridView1.DataBind();
}
}
}
}