here is the code for the ListView, but remember to look at chapter 11
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default"
ViewStateMode="Enabled" ViewStateEncryptionMode="Always"%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("ServiceName")%></td>
<td><%#Eval("ServiceDescription")%></td>
</tr>
</ItemTemplate>
</asp:ListView>
</table>
</div>
</form>
</body>
</html>
Here is the ADO code behind for that page
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection
("Data Source=Localhost;initial catalog=CommunityAssist;integrated security=true");
string sql = "Select ServiceName, ServiceDescription from [Service]";
SqlCommand cmd = new SqlCommand(sql, connect);
SqlDataReader reader=null;
try
{
connect.Open();
reader = cmd.ExecuteReader();
ListView1.DataSource = reader;
ListView1.DataBind();
}
catch (SqlException )
{
Session["error"] = "There is an error in the SQL String";
Response.Redirect("Default2.aspx");
}
catch (Exception)
{
Session["error"] = "Something went wrong, sorry";
Response.Redirect("Default2.aspx");
}
finally
{
reader.Dispose();
connect.Close();
}
}
}
Here is the source for the Error page
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
And here is the c# from the error page
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["error"] != null)
{
//Exception ex = (Exception)Session["error"];
//Label1.Text = ex.Message;
Label1.Text = Session["Error"].ToString();
}
}
}
Linq
Remember with LINQ you need to add a new a LINQ to SQL object.
It adds the LINQ Designer
From the solution explored drag tables from a connection onto the designer. This causes Visual Studio to generate classes we can access.
Here is the code for the Linq example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
CommunityAssistDataContext dc = new CommunityAssistDataContext();
var donors = from d in dc.PersonContacts
where d.ContactTypeKey == 6
orderby d.Person.LastName
select new
{
d.Person.LastName,
d.Person.FirstName,
d.ContactInfo
};
GridView1.DataSource = donors.ToList();
GridView1.DataBind();
}
catch (Exception ex)
{
}
}
}
No comments:
Post a Comment