views:

18

answers:

3

Hello,

I am trying to display a dataset to my ASP.NET application. It seems that when I click the button event, the data is not displaying in the grid.

I have a basic page with the following:

<form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" Width="200" Height="300">
                </asp:GridView>
                <asp:Button runat="server" id="UpdateButton1" onclick="UpdateButton_Click" text="Update" />               
            </ContentTemplate>
        </asp:UpdatePanel>

    </form>

Then in the code behind, I have the following:

protected void UpdateButton_Click(object sender, EventArgs e)
        {
            string SQLConfigSettings = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(SQLConfigSettings);

            sqlconn.Open();

            SqlDataAdapter adapter = new SqlDataAdapter("Select * from Student", sqlconn);

            DataSet ds = new DataSet();
            adapter.Fill(ds);

            GridView1.DataSource = ds;
            UpdatePanel.Update();

        }

Am I missing something? Shoudlnt the dataset be diaplayed in the grid? When I click the button nothing happens.

Thanks :)

A: 

You need to add

GridView1.DataBind() right after Gridview1.DataSource.

So it becomes:

...
          DataSet ds = new DataSet();
            adapter.Fill(ds);

            GridView1.DataSource = ds;
            GridView1.DataBind();
            UpdatePanel.Update();

If you need more information about the .DataBind method check MSDN on it

JonH
This worked! Forgot to databind! :)
Mage
A: 
GridView1.DataSource = ds; 

Try to mention table inside dataset. Something like ds.Tables[0] or if you know name of table ds.Tables["table_name"]

Shekhar
@shakhar, the default takes the first table so that is unnecessary.
JonH
ok.. and its is not @shakhar . it should have been @shekhar
Shekhar
A: 

Put a breakpoint on the first line of code in UpdateButton_Click and run it.

Is that breakpoint reached?

Step through each line of code after that and examine the variable values. Is the DataSet being filled? Can you see the DataTable and its DataRows?

If you reach this event code and have data, then you need to look at the databinding. You need to do GridView.DataBind() immediately after setting the DataSource and before doing the UpdatePanel.Update(). Then you should be good to go.

DOK