Populate RadioButtonList From SQL Database

<%@ Control Language="C#" ClassName="PollControlAsync" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        BindRadioButtonListData(1);
    }

    
    private void BindRadioButtonListData(int PollID)
    {
        // connection string
        string connectionString = ConfigurationManager.ConnectionStrings["PollConnectionString"].ConnectionString;

        // Sql connection object initialized with connection string used to connect        
        using (SqlConnection mySqlConnection = new SqlConnection(connectionString))
        {
            try
            {
                // open the Sql connection
                mySqlConnection.Open();

                // Sql Command object initialized with SQL query to retrieve the categories
                SqlCommand mySqlCommand = new SqlCommand("SELECT * FROM [PollAnswers] WHERE ([PollID] = @PollID) ORDER BY [SortOrder]", mySqlConnection);   
                mySqlCommand.Parameters.AddWithValue("@PollID", PollID);             
                
                
                // Sql Data Adapter object initialized by passing the Sql Command object
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);


                // DataSet object to store the retrieved SQL data items
                DataSet myDataSet = new DataSet();

                // fill the DataSet
                mySqlDataAdapter.Fill(myDataSet);

                // Set DataSet object as DataSource for the RadioButtonList
                rblPollAnswers.DataSource = myDataSet;

                // Specify the Field Name that you want to display as
                // text label for Radio Button list item
                rblPollAnswers.DataTextField = "DisplayText";

                // Specify the Field Name that you want to use as
                // value for each list item
                rblPollAnswers.DataValueField = "PollAnswerID";

                // Finalize the DataBinding
                rblPollAnswers.DataBind();

            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
            finally
            {
                // close the Sql Connection
                mySqlConnection.Close();
            }
        }



    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
    }

    /// <summary>
    /// Gets or sets the poll ID.
    /// </summary>
    /// <value>The poll ID.</value>
    public int PollID
    {
        get
        {
            if (ViewState["PollID"] == null)
                return -1;
            else
                return (int)ViewState["PollID"];
        }
        set { ViewState["PollID"] = value; }
    }
  

</script>
<asp:RadioButtonList ID="rblPollAnswers" runat="server" />

Advertisements

One thought on “Populate RadioButtonList From SQL Database

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s