How to store and retrieve information using web.config

So you like the idea of storing variables centrally, and want to know how to implement that in your c# web site.

Simply add the following node to your web.config if it does not already exist

[codesyntax lang="xml"]

<configuration>
  <appSettings>
    <add key="brad" value="this came from web.config"/>
  </appSettings>
</configuration>

[/codesyntax]

Then import the Configuration namespace at the top of your codebehind with

[codesyntax lang="csharp"]

using System.Web.Configuration;

[/codesyntax]

Finally in your webform or codebehind file to retrieve the stored value from the key/value pair stored in web.config simply use this statement

[codesyntax lang="csharp"]

yourVariable = WebConfigurationManager.AppSettings["Brad"];

[/codesyntax]

Programatically building an HTML table in C#

These are the minimum using directives to add at the top of the code behind for building an HTML table from a SQL Server to a SqlDataReader concisely.

[codesyntax lang="csharp"]

using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;

[/codesyntax]

Your table in the aspx file can be as simple as

[codesyntax lang="html4strict"]

<table id="Products" runat="server"></table>

[/codesyntax]

And the code behind that does the work,  taking the value from a dropdown list control called Category, and retrieving data from Northwind is

[codesyntax lang="csharp"]

using(SqlConnection cnn = new SqlConnection("Data Source=YOURSERVER; Database=Northwind; UID=YOURDBUSERID; PWd=YOURDBPASSWORD"))
      {
        SqlCommand cmd;
        SqlDataReader rdr;

          HtmlTableRow row = new HtmlTableRow();

          HtmlTableCell cell = new HtmlTableCell("th");
          cell.InnerText = "Product ID";
          row.Cells.Add(cell);

          cell = new HtmlTableCell("th");
          cell.InnerText = "Product Name";
          row.Cells.Add(cell);

          cell = new HtmlTableCell("th");
          cell.InnerText = "Units in stock";
          row.Cells.Add(cell);

          Products.Rows.Add(row);

          cmd = new SqlCommand("SELECT ProductID, ProductName, UnitsInStock FROM Products WHERE CategoryID = "
            + Category.SelectedValue, cnn);
          cnn.Open();
          rdr = cmd.ExecuteReader();

          while (rdr.Read())
          {
            row = new HtmlTableRow();

            cell = new HtmlTableCell();
            cell.InnerText = rdr["ProductID"].ToString();
            row.Cells.Add(cell);

            cell = new HtmlTableCell();
            cell.InnerText = rdr["ProductName"].ToString();
            row.Cells.Add(cell);

            cell = new HtmlTableCell();
            cell.InnerText = rdr["UnitsInStock"].ToString();
            row.Cells.Add(cell);

            Products.Rows.Add(row);

          }
        }

[/codesyntax]

Databound DropDownList in C#

Remember to add the usual using directives at the start of the codebehind

[codesyntax lang="csharp"]

using System.Data;
using System.Data.SqlClient;

[/codesyntax]

For a DropDownList on your ASP.NET page with an ID of Category such a

[codesyntax lang="html4strict"]

<asp:DropDownList ID="Category" runat="server">
</asp:DropDownList>

[/codesyntax]

The following in your code behind will bind data from an example Northwind database on SQL Server to the text and values of the DropDownList

[codesyntax lang="csharp"]

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;

namespace AppDevStuff
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      using(SqlConnection cnn = new SqlConnection("Data Source=YOURSERVERNAME; Database=Northwind; UID=YOURUSERNAME; PWd=YOURPASSWORD"))
      {
        SqlCommand cmd;
        SqlDataReader rdr;
        if (!Page.IsPostBack)
        {
          cmd = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories ORDER BY CategoryName;", cnn);
          cnn.Open();
          rdr = cmd.ExecuteReader();

          Category.DataSource = rdr;
          Category.DataTextField = "CategoryName";
          Category.DataValueField = "CategoryID";
          Category.DataBind();
        }
      }
    }
  }
}

[/codesyntax]