Hi Friends

I am new to asp.net, i want to display menu title from database using VB in asp.net, the display should be done in list format while href tag, i am expecting the display as below

menu1 menu2 menu3

Please can any one help me to solve this issue.

Dani AI

Generated

Building on and : a compact, safe pattern that binds menu rows from your database into a semantic UL and renders them side‑by‑side. This example uses a Repeater (flexible for any number of items), disposes DB objects correctly, and HTML‑encodes the title to avoid XSS.

<asp:Repeater ID="rptMenu" runat="server">
  <HeaderTemplate><ul class="menu"></HeaderTemplate>

  <ItemTemplate>
    <li>
      <asp:HyperLink runat="server"
        NavigateUrl='<%# Convert.ToString(Eval("Url")) %>'
        Text='<%# Server.HtmlEncode(Convert.ToString(Eval("Title"))) %>' />
    </li>
  </ItemTemplate>

  <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

<style>
.menu { list-style: none; margin: 0; padding: 0; }
.menu li { display: inline-block; margin-right: 12px; }
</style>
Imports System.Data
Imports System.Data.SqlClient

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  If Not IsPostBack Then BindMenu()
End Sub

Private Sub BindMenu()
  Dim conn As String = System.Configuration.ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
  Dim sql As String = "SELECT Title, Url FROM MenuItems ORDER BY SortOrder"
  Using cn As New SqlConnection(conn)
    Using cmd As New SqlCommand(sql, cn)
      Dim dt As New DataTable()
      Using da As New SqlDataAdapter(cmd)
        da.Fill(dt)
      End Using
      rptMenu.DataSource = dt
      rptMenu.DataBind()
    End Using
  End Using
End Sub

Troubleshooting & best practices: ensure the connection string name matches web.config, call DataBind only when needed (Page_Load guarded by Not IsPostBack), and confirm the DB column names match the Eval calls. Use parameterized queries if you add user input. If items don’t render, run the SELECT directly in your DB tool to verify results, and watch the ASP.NET error details or log exceptions for hints. If already fixed it, consider the above as a more robust, secure pattern for production.

Recommended Answers

All 3 Replies

You have a couple of options to do this. Each step requires pulling the menu items out of the database of course but after that you can display the menu as a standard UL but with asp:Labels inside each <li> or use a literal control to output the html for the menu server side. The repeater is a better idea as it allows for differing amounts of menu items (to a degree) whereas the first method doesn't.

Use a Repeater and build an <UL> <LI> list.

Thanks Guys for your support. i solved the issue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.