creating a bulleted list dynamically

How do I create a bulleted list of items taken from a table in the database with a display mode in hyperlinks?
boneythomasAsked:
Who is Participating?
 
daveamourCommented:
Ok attached is the updated C# code and below is the DDL for the table

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HyperLinks]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[HyperLinks]
GO
CREATE TABLE [dbo].[HyperLinks] (
 [LinkID] [int] IDENTITY (1, 1) NOT NULL ,
 [LinkText] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL ,
 [LinkUrl] [varchar] (100) COLLATE Latin1_General_CI_AS NOT NULL
) ON [PRIMARY]
GO
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
public partial class Bullets : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BulletLinks.DataSource = GetLinks();
            BulletLinks.DataBind();
        }
    }
 
    private List<HyperLink> GetLinks()
    {
        List<HyperLink> links = new List<HyperLink>();
 
        using (SqlConnection dbConnection = new SqlConnection("Data Source=ServerName;Initial Catalog=DatabaseName;User Id=Bob;Password=MyPassword;"))
        {
            using (SqlCommand dbCommand = new SqlCommand("Select LinkText, LinkUrl From HyperLinks"))
            {
                dbCommand.Connection = dbConnection;
 
                dbCommand.CommandType = CommandType.Text;
 
                dbConnection.Open();
 
                SqlDataReader reader = dbCommand.ExecuteReader();
 
                while (reader.Read())
                {
                    string linkText = reader.GetString(0);
                    string linkUrl = reader.GetString(1);
 
                    links.Add(new HyperLink(linkText, linkUrl));
                }
            }
        }
 
        return links;
    }
}
 
public class HyperLink
{
    public HyperLink(string linkText, string linkUrl)
    {
        this.linkText = linkText;
        this.linkUrl = linkUrl;
    }
    
    private string linkText;
    private string linkUrl;
 
    public string LinkText
    {
        get
        {
            return linkText;
        }
 
        set
        {
            linkText = value;
        }
    }
 
    public string LinkUrl
    {
        get
        {
            return linkUrl;
        }
 
        set
        {
            linkUrl = value;
        }
    }
}

Open in new window

0
 
daveamourCommented:
Do you know how to query your database or do you need code for that too  - whay kin dof database is it?
0
 
daveamourCommented:
Sorry just noticved you posted in SQL 2005 so I assume thats the database!
Also can I assume this is an asp.net application?  If so which version of the framework are you using?
 
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
boneythomasAuthor Commented:
I am using 2.0 framework
0
 
daveamourCommented:
Ok here is some code without the database access.
You would add the database code to populate the list of Links.  Let me know if you need an example of the database code too.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bullets.aspx.cs" Inherits="Bullets" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <ul>
            <asp:Repeater ID="BulletLinks" runat="server">
                <ItemTemplate>
                    <li><a href=<%# DataBinder.Eval(Container.DataItem, "LinkUrl") %>><%# DataBinder.Eval(Container.DataItem, "LinkText") %></a></li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
    </form>
</body>
</html>
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Bullets : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BulletLinks.DataSource = GetLinks();
            BulletLinks.DataBind();
        }
    }
 
    private List<HyperLink> GetLinks()
    {
        List<HyperLink> links = new List<HyperLink>();
 
        //Database code goes here to populate links but we just do some manually
        //to show the idea
        HyperLink bbc = new HyperLink("BBC", "http://www.bbc.co.uk");
        HyperLink google = new HyperLink("Google", "http://www.google.co.uk");
        HyperLink ebay = new HyperLink("ebay", "http://www.ebay.co.uk");
        HyperLink msn = new HyperLink("msn", "http://www.msn.co.uk");
 
        links.Add(bbc);
        links.Add(google);
        links.Add(ebay);
        links.Add(msn);
 
        return links;
    }
}
 
public class HyperLink
{
    public HyperLink(string linkText, string linkUrl)
    {
        this.linkText = linkText;
        this.linkUrl = linkUrl;
    }
    
    private string linkText;
    private string linkUrl;
 
    public string LinkText
    {
        get
        {
            return linkText;
        }
 
        set
        {
            linkText = value;
        }
    }
 
    public string LinkUrl
    {
        get
        {
            return linkUrl;
        }
 
        set
        {
            linkUrl = value;
        }
    }
}

Open in new window

0
 
boneythomasAuthor Commented:
It would be good if you could help me out in explaining by using some database codes..thankyou
0
 
boneythomasAuthor Commented:
Thankyou for the solution. This code should work for my application
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.