using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
///
/// Summary description for MagazineData
///
public class MagazineData
{
private string magName;
private string magType;
private SqlConnection connect;
public MagazineData()
{
InitializeConnection();
}
public MagazineData(string magazineName, string magazineType)
{
magName = magazineName;
magType = magazineType;
InitializeConnection();
}
public DataTable GetMagazineTypes()
{
DataTable table = new DataTable();
string sql = "Select Distinct magType from Magazine";
SqlCommand cmd = new SqlCommand(sql, connect);
SqlDataReader reader = null;
try
{
connect.Open();
reader = cmd.ExecuteReader();
table.Load(reader);
}
catch (SqlException)
{
//we make our own exception and give it a message
Exception ex = new Exception("Database could not be Found");
//throw it back to the form
throw ex;
}
finally
{
//these happen no matter what
if (reader != null)
{
reader.Close();
}
connect.Close();
}
return table;
}
private void InitializeConnection()
{
connect = new SqlConnection
("Data Source=localhost;initial catalog=MagazineSubscription;integrated Security=true");
}
}
Here is Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<h1>Enter a new Magazine</h1>
<asp:Label ID="Label1" runat="server" Text="Enter New Magazine">
</asp:Label><asp:TextBox ID="txtMagazine" runat="server"></asp:TextBox><br />
<asp:DropDownList ID="ddlMagazineType" runat="server">
</asp:DropDownList><br />
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MagazineData md = new MagazineData();
try
{
DataTable tbl = md.GetMagazineTypes();
ddlMagazineType.DataSource = tbl;
ddlMagazineType.DataTextField = "MagType";
ddlMagazineType.DataBind();
}
catch (Exception ex)
{
Response.Redirect("Default2.aspx?err=" + ex.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
Default2.aspx
<%@ 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>
<h1>Error Page</h1>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Default2.aspx.cs
using System;
using System.Collections.Generic;
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)
{
Label1.Text = Request.QueryString["err"];
}
}
No comments:
Post a Comment