Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Displaying Data from SQL Database in GridView & Repeater Controls

Last post 04-08-2008 6:50 PM by cyberstore05. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 04-08-2008 6:50 PM

    • cyberstore05
    • Not Ranked
    • Joined on 01-04-2008
    • United Kingdom
    • New Member
    • Points 30

    Displaying Data from SQL Database in GridView & Repeater Controls

    Hi There,

     I am trying to display data from an SQL database using the GridView and Repeater controls in ASP.net, but I’ve run into some difficulty. Primarily, when a user selects an item from a menu, data from an SQL table is displayed in Gridview and Repeater. The data displayed in Repeater is a subset of that in GridView. As you might suspect, I want both sets of data displayed simultaneously.  I am using SQL stored procedure to extract the data from two tables. 

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

             If Not Page.IsPostBack Then

             Dim ConnectionString As String = "Data Source=CYBERSTORE\SQLEXPRESS;Initial Catalog=ProductsDB;Integrated Security=True"

             Using SqlCon1 As New SqlConnection(ConnectionString)

                         SqlCon1.Open()

                        

                         Dim MenuSearch As String = Request.QueryString("MenuSearch")

     

                         Using SqlCom1 As New SqlCommand("ProdProcMSearch", SqlCon1)

                                     SqlCom1.CommandType = CommandType.StoredProcedure

                                     SqlCom1.Parameters.Add("@OuterCategory", Data.SqlDbType.nvarchar, 50)

                                     SqlCom1.Parameters("@OuterCategory").Value = "' & MenuSearch.Text & '"                                        

                                     GrdMenu.DataSource = SqlCom1.ExecuteReader()

                                     GrdMenu.DataBind()

                                     'GrdMenu.DataSource.SqlCommand = SqlCom1

                                     SqlCon1.Close()

                         End Using

             End Using

             End If

    End Sub

     

    Protected Sub GridViewDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)    

             Dim ConnectionString As String = "Data Source=CYBERSTORE\SQLEXPRESS;Initial Catalog=ProductsDB;Integrated Security=True"     

             Using SqlCon2 As New SqlConnection(ConnectionString)  

                         SqlCon2.Open()                 

                                             

                 Dim SqlCom2 As New SqlCommand("SELECT Brand, BrandCount FROM ProdTabMSearchSet",SqlCon2)     

                    

                 Repeater1.DataSource = SqlCom2.ExecuteReader()     

                         Repeater1.DataBind()                         

                 SqlCon2.Close()

             End Using

    End Sub

     Note: MenuSearch is meant to be the query string that is  passed on from a master page. GridView is populated by data from table ProdTabMSearch, and Repeater from ProdTabMSearchSet. GridViewDataBound is GridView’s OnRowDataBound tag.

    The following is a snipet of the SQL procedure I’m using:

     CREATE PROCEDURE ProdProcMSearch@OuterCategory nvarchar(50) ASBEGIN      SELECT ProdID, ProdName, ProdTitle, Brand     INTO ProdTabMSearch     FROM dbo.ProductsTab     WHERE  OuterCategory = @OuterCategory        SELECT Brand, COUNT(*) AS BrandCount     INTO ProdTabMSearchSet     FROM dbo.ProdTabMSearch     WHERE OuterCategory = @OuterCategory      GROUP BY Brand      SELECT ProdID, ProdName, ProdTitle, Brand       FROM ProdTabMSearch CategorySelectedENDGO EXECUTE [dbo].[ProdProcMSearch] GO I hope the above gives you an idea of what I’m trying to do. Any help will be grately appreciated.
    • Post Points: 5
Page 1 of 1 (1 items)