Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Bind data to a gridview using datareader

Posted on 2009-04-12
7
3,086 Views
Last Modified: 2012-05-06
Hi there, i am trying to Read some data from the db and bind it to gridview using datareader, i think my code should be right but anyway i don't get the data binded to the gridview and i don't get any errors...

Here is my code...

<table class="style1">
        <tr>
            <td class="style4">
                <asp:Label ID="lblSearch" runat="server" Text="Search:"></asp:Label>
            </td>
            <td class="style3">
                <asp:TextBox ID="txtSearch" runat="server" style="margin-left: 0px" 
                    Width="250px"></asp:TextBox>
            </td>
            <td class="style5">
                <asp:Button ID="btnSearch" runat="server" Text="Submit" />
            </td>
            <td>
                <asp:RequiredFieldValidator ID="rvSearch" runat="server" 
                    ErrorMessage="Field is required!" ControlToValidate="txtSearch"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style2" colspan="4">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2" colspan="4">
                <asp:GridView ID="gvSearch" AutoGenerateColumns="false" runat="server">
                </asp:GridView>
            </td>
        </tr>
    </table>
--------------------------------------
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Search()
    End Sub
    Sub Search()
 
        Dim strSearch As String = txtSearch.Text
 
        Dim MyConn As OleDbConnection = New OleDbConnection(ConfigurationManager.AppSettings("strConn"))
        Dim MySQL As String = "SELECT * FROM [tblTopics] WHERE [tblTopics.TopicSubject] = @TopicSubject"
        Dim MyCmd As New OleDbCommand(MySQL, MyConn)
 
        With MyCmd.Parameters
            .Add(New OleDbParameter("@TopicSubject", strSearch))
        End With
 
        MyConn.Open()
 
        Dim objDataReader As OleDbDataReader = MyCmd.ExecuteReader()
 
        gvSearch.DataSource = objDataReader
        gvSearch.DataBind()
 
        MyConn.Close()
 
    End Sub
End Class

Open in new window

0
Comment
Question by:Neste
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24126789
why a datareader?
    Sub Search()
 
        Dim strSearch As String = txtSearch.Text
 
        Dim MyConn As OleDbConnection = New OleDbConnection(ConfigurationManager.AppSettings("strConn"))
        Dim MySQL As String = "SELECT * FROM [tblTopics] WHERE [tblTopics.TopicSubject] = @TopicSubject"
        Dim MyCmd As New OleDbCommand(MySQL, MyConn)
 
        With MyCmd.Parameters
            .Add(New OleDbParameter("@TopicSubject", strSearch))
        End With 
        MyConn.Open() 
        Dim MyAdapter as New DataAdapter(MyCmd)
        Dim Mydataset as New DataSet()
        MyAdapter.Fill(mydataset)  
        gvSearch.DataSource = mydataset
        gvSearch.DataBind()
 
        MyConn.Close()
 
    End Sub

Open in new window

0
 

Author Comment

by:Neste
ID: 24128141
Cant make it work whit your code, the gridview is still empty when i click the button :(

Any ideas ?
0
 

Author Comment

by:Neste
ID: 24128212
Please any help is more then welcome, i really need to have this done in 2 days...thanks

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24128217
did you define the bound columns definitions in the gridview already?
0
 

Accepted Solution

by:
apramod earned 125 total points
ID: 24128443
From the above code what i find is you have not defined bound columns in your gridview. You are getting data but u need to bind those columnd definations in gridview.
Ex:
asp:GridView ID="gvSearch" runat="server" AllowPaging="true" AutoGenerateColumns="False"
                    OnPageIndexChanging="grdLanguage_PageIndexChanging" OnSelectedIndexChanged="grdLanguage_SelectedIndexChanged"
                    >
                    <Columns>
<asp:BoundField DataField="Search" HeaderText="Language" SortExpression="SearchName" ItemStyle-HorizontalAlign="left" >
                            <ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundField>
</Columns>
                </asp:GridView>
0
 
LVL 6

Expert Comment

by:havj123
ID: 24128647
The better way is to use dataset instead of datareader to fill a gridview.

I think the problem is in the quesry

"SELECT * FROM [tblTopics] WHERE [tblTopics.TopicSubject] = @TopicSubject"
0
 

Expert Comment

by:apramod
ID: 24128733
Even though if you use dataset to bind to gridview you need to bind the colums to gridview by using "BoundField" where you will bind the columns you want to see in grid, without that you will not see the columns.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question