Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 6th, 2005, 10:38 PM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default Data Binding Using DataGrid

Hi, I have a dropdownlist for category selection. after the selection, the data that is in the category selected should be displayed.

But the problem is, when I selected any category, all the data in the table is displayed regardless which category is selected. For example, there are 4 categories, books, journals, magazines n others. When I select any of these, it displayed all the categories. i only want it to display the particular category i choose, not all.

Code:
  Private Sub ddlView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlView1.SelectedIndexChanged
        Dim Str As String
        Str = "Select * From Library where CategoryID = '" & ddlView1.SelectedValue
        SqlDataAdapter1.Fill(DsLIbrary1)
        DataGrid1.DataSource = DsLIbrary1
        DataGrid1.DataBind()
End Sub
Another problem is, it don't allow paging to be done even though i set allow paging= true in the properties for the dropdownlist.

Code:
Private Sub DataGrid1_PageIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        DataGrid1.DataBind()
End Sub
Anyone can help? Thanks!

Irene

 
Old June 7th, 2005, 12:20 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

Have you given AutoPostBack to true??

Can u send the htm portion of the drown??

Regards
Ganesh
 
Old June 7th, 2005, 12:41 AM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I set auto postback to true already.

Code:
<asp:dropdownlist id="ddlView1" style="Z-INDEX: 104; LEFT: 344px; POSITION: absolute; TOP: 56px" runat="server"
                ForeColor="Black" Height="32px" Width="168px" AutoPostBack="True">
                <asp:ListItem Value="1">Books</asp:ListItem>
                <asp:ListItem Value="2">Journals</asp:ListItem>
                <asp:ListItem Value="3">Magazines</asp:ListItem>
                <asp:ListItem Value="4">Others</asp:ListItem>
            </asp:dropdownlist>
 
Old June 7th, 2005, 12:52 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI,
Have you must corected a problem in this line,

Str = "Select * From Library where CategoryID = '" & ddlView1.SelectedValue

opened single quote which may not be required as the field is an ID

Also be sure you a are not reloading the whole grid & dropdowm in every post back

Prashant


 
Old June 7th, 2005, 03:08 AM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I delete the single quote already. But it still display the same result.
when I generate the dataset, I select the data from all 4 categories.

What should I do to make sure I don't reload the whole grid n dropdownlist
in every post back?

 
Old June 7th, 2005, 03:48 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

You should check Not IsPostback in you page load before loading

Prashant

 
Old June 7th, 2005, 05:17 AM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I feel that even though I retrieve the CategoryID from str statement,
I don't pass the value to display the data after search, am I right?

I should not call the databind() in page load, else the data will be displayed
once the page is loaded, rite?

I still can't figure out how to solve this problem.

Can somebody help me? Thanks!

 
Old June 7th, 2005, 11:38 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

There is a basic problem here...


Private Sub ddlView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlView1.SelectedIndexChanged
        Dim Str As String
        Str = "Select * From Library where CategoryID = '" & ddlView1.SelectedValue
        SqlDataAdapter1.Fill(DsLIbrary1)
        DataGrid1.DataSource = DsLIbrary1
        DataGrid1.DataBind()
End Sub

Where is the new query (Str) ever getting assigned to the data adapter? All you are doing is reloading the original query that was created for you by the data adapter wizard.

You need to assign the new query to the data adapter select command property:

        SqlDataAdapter1.SelectCommand.CommandText = Str
        SqlDataAdapter1.Fill(DsLIbrary1)

-Peter
 
Old June 7th, 2005, 11:27 PM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now I can view the data for the particular category i choose, but i still
can't navigate through the records. i.e, I hv 143 records, I set 1 page to display 10 records.
the problem is here.. when i click the 2nd page onwards, it does not display any data.

Code:
Private Sub DataGrid1_PageIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        DataGrid1.DataBind()
End Sub
even though i debug, it does not display any error.
Can anybody help me? Thanks!








 
Old June 8th, 2005, 12:54 AM
Authorized User
 
Join Date: Jul 2004
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are binding the grid using DataGrid1.DataBind(). But you have not
specified the data to bind.

    Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub

//make the relevant changes as per your requirement
 Private Sub BindGrid()
        Dim cnn As SqlConnection
        Dim da As SqlDataAdapter
        Dim ds As New DataSet()

        cnn = New SqlConnection(ConfigurationSettings.AppSettings(). Item("ConnectionString"))
        da = New SqlDataAdapter("select lastname,firstname,city from employees", cnn)
        da.Fill(ds, "employees")

        DataGrid1.DataSource = ds
        DataGrid1.DataMember = "employees"
        DataGrid1.DataBind()

    End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with binding data in Datagrid Samatha ASP.NET 1.0 and 1.1 Professional 6 December 6th, 2006 09:13 AM
Datagrid binding two dropdown inkrajesh ASP.NET 1.0 and 1.1 Basics 0 July 18th, 2006 10:46 PM
Binding Data to a DataGrid RPG SEARCH ASP.NET 1.0 and 1.1 Basics 5 August 9th, 2004 02:27 PM
DataGrid Binding GrindCrusher Classic ASP Databases 0 February 21st, 2004 05:52 PM
Binding an aarraylist to a datagrid Delano ADO.NET 2 September 22nd, 2003 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.