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
[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;
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();
}
}
}
[/codesyntax]
Here is your control
[codesyntax lang="csharp"]
<asp:GridView ID=”gv” runat=”server” />
[/codesyntax]
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
[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]
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.
[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]
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
[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]