Home > ASP.Net > Bind RadioButtonList with Image from Database

Bind RadioButtonList with Image from Database


try this example to Bind RadioButtonList with Image from Database

Step1 :- Open MS SQL Server 2005 and create Database which contains Table called “Photo” whith the following fields

  • Id –> (int) Primary key
  • Title –> nvarchar(50)
  • Pic –> nvarchar(20)

Step2 :- Open MS VS2005 and craete new website and add new folder called “images” which will contain all image that i will save it’s name with it’s Extension in my table

Step3 :- add new web page and add RadioButtonList control

Step4 :- in code behind of web page add the following code

public DataSet GetPhoto()
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandText = "select * from Photo";
        comm.CommandType = CommandType.Text;
        SqlDataAdapter adapter = new SqlDataAdapter(comm);
        return ds;

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            DataSet photoDS = GetPhoto();
            if (photoDS.Tables.Count > 0)
                DataTable dt = photoDS.Tables[0];
                foreach (DataRow row in dt.Rows)
                    ListItem item = new ListItem();
                    item.Text = "<img Width=\"100\" Height=\"100\" runat=\"Server\" src=\"images/" + row["Pic"].ToString() + "\" alt="+row["Title"].ToString()+" />";
                    item.Value = row["Id"].ToString();
                //-- To Select First item in RadioButtonList
                RadioButtonList1.Items[0].Selected = true;

Step5:- in web.config add the connectionstring

        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />

after that run the website

Hope this helps

Good Luck.

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: