2

how to retrieve data from database table in vb.net. As i tried i don't get any output it only creates a blank page And my code is:

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script runat="server">
sub Page_Load()
Dim con As New SqlConnection
Dim cmd As New SqlCommand

con.ConnectionString = ///my connection string///
con.Open()
cmd.Connection = con
cmd.CommandText = "select * from det"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
End sub         

</script>
<form runat="server">
<asp:Repeater id="customers" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr bgcolor="#b0c4de">
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
<th>Gender</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#DataBinder.Eval(Container.DataItem, "id")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "name")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "address")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "age")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "gender")%> </td>

</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

And i don't get any output either error too How to resolve this one???

2 Answers 2

2

You are retrieving data but not doing anything with it:

sub Page_Load()

   Dim con As New SqlConnection
   Dim cmd As New SqlCommand

   con.ConnectionString = ///my connection string///
   con.Open()
   cmd.Connection = con
   cmd.CommandText = "select * from det"

   customers.DataSource = cmd.ExecuteReader() //here we bind data to repeater.
   customers.DataBind();

End sub         

Also I would suggest to use code behind file and put your code there as it is way clearer.

Sign up to request clarification or add additional context in comments.

Comments

1
Public Sub OnPageLoad()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = String
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM TABLE_NAME"
        con.Open()
        customers.DataSource = cmd.ExecuteNonQuery()
        customers.DataBind()
        con.Close()
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.