Recent twitter entries...

Databinding a GridView in C# with ADO.NET

Posted by Brad | Posted in C# | Posted on 13-07-2010

Something simple, you want to use ADO.NET to bind data from SQL Server to a GridView, here is your code behind

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;
using System.Configuration;
 
public partial class Default2 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string conn = ConfigurationManager.ConnectionStrings["STRINGNAME"].ConnectionString;
    SqlConnection con = new SqlConnection(conn);
 
    using (con)
    {
      SqlCommand cmd = new SqlCommand("SELECT * FROM Table", con);
      con.Open();
      SqlDataReader rdr = cmd.ExecuteReader();
      gv.DataSource = rdr;
      gv.DataBind();
    }
  }
}

Here is your control

<asp:GridView ID="gv" runat="server" />

How to store and retrieve information using web.config

Posted by Brad | Posted in C# | Posted on 30-06-2010

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

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

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

using System.Web.Configuration;

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

yourVariable = WebConfigurationManager.AppSettings["Brad"];

Programatically building an HTML table in C#

Posted by Brad | Posted in C#, Ramblings | Posted on 23-06-2009

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.

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

Your table in the aspx file can be as simple as

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

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

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);
 
          }
        }

Databound DropDownList in C#

Posted by Brad | Posted in C#, Coding | Posted on 23-06-2009

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

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

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

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

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

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();
        }
      }
    }
  }
}