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

Simplest ConnectionString For Local SQL Server Express

Configured nothing in SQL Server or the database (DRRSandBox). Just added new one and went. As it uses SSPI (Integrated Security), it authenticates using local user account which by default has access to database.

<configuration>
  <connectionStrings>
    <add name="PollConnectionString"
 connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DRRSandBox;Integrated Security=SSPI"
       providerName="System.Data.SqlClient" />

  </connectionStrings>
...
..
.
</configuration>

How to import/export SQL tables, functions and stored procs with SQL Management Studio 2005

  • In Microsoft SQL Server Management Studio Express, expand the databases.
  • Right-Click on the database containing objects you want to copy to another database
  • Click on Tasks, then Generate Scripts… This will open up the Script Wizard, which is pretty much self-explanatory:
    • Select the database (which should be pre-selected for you),
    • Select Options (first time around you might just accept the default selections),
    • Choose the Object Types you want to script (Stored Procedures, User Defined Functions, etc),
    • Select the specific objects to be scripted,
    • and finally specify where to output the script (choose Script to new Query Window for convenience).
  • Once completed, return to the Query Window with the scripted objects.  Change the USE directive (the first line) to point to the destination database (e.g., USE [Northwind]), and execute the script.

source