Click here to Skip to main content
15,886,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried a very basic databinding in a webform with SQLSERVER2008..everything seems to be perfect(NO ERRORS),but still couldn't display it on browser while it runs..i see nothing jus a blank page.
What could be wrong?
-i lookedupon db connection its fine


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con =new SqlConnection ("data source=.; database=sample ; integrated security=SSPI; ");
        SqlCommand cmd = new SqlCommand("Select * from Login_Info", con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        GridView1.DataSource = rdr;
        GridView1.DataBind();
        con.Close();
    }
}
Posted
Updated 18-Feb-15 4:48am
v3

1 solution

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.

C#
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();
            }
        }
    }
}
 
Share this answer
 
Comments
[no name] 18-Feb-15 12:25pm    
+5 for that
Member 1097736 18-Feb-15 12:32pm    
@RICHARD Deeming ...i followed up an video tutorial from youtube
LINK-> https://www.youtube.com/watch?v=aoFDyt8oG0k
meanwhile he did the same and got output!

still the code aint helping out!
Richard Deeming 18-Feb-15 12:36pm    
If you look carefully at the code he's typed, you'll see he's binding the list in the Page_Load event.

You're trying to bind it in the GridView1_SelectedIndexChanged event.

The Page_Load event fires every time the page is loaded. The GridView1_SelectedIndexChanged event only fires when GridView1 already has some data, and the use selects a different row.
Member 1097736 18-Feb-15 12:42pm    
THanks RICHARD! _/\_
Member 1097736 18-Feb-15 13:19pm    
So will this code be perfect?
<pre>#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(" data source=.; database=sampl ; integrated security=SSPI; ");
SqlCommand cmd = new SqlCommand("Select * from studet", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
con.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900