Neste
asked on
Bind data to a gridview using datareader
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...
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">
</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
ASKER
Cant make it work whit your code, the gridview is still empty when i click the button :(
Any ideas ?
Any ideas ?
ASKER
Please any help is more then welcome, i really need to have this done in 2 days...thanks
did you define the bound columns definitions in the gridview already?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
I think the problem is in the quesry
"SELECT * FROM [tblTopics] WHERE [tblTopics.TopicSubject] = @TopicSubject"
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.
Open in new window