ammukarthi
ammukarthi

Reputation: 45

how to display data from database in vb.net

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???

Upvotes: 2

Views: 5310

Answers (2)

Farhan Mukadam
Farhan Mukadam

Reputation: 470

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

Upvotes: 1

gzaxx
gzaxx

Reputation: 17590

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.

Upvotes: 2

Related Questions