I'm in the process of learning ASP.net and any help would be greatly appreciated.
I have a project that has the following requirement: For Insert/Update/Delete operations you must use a custom form For Insert/Update/Delete operations you must use a custom form.
We are using the Northwind Database from Microsoft. I have a data access layer (folder called DAL) that has the following files:
Customers.cs
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
using
System.Web;
using
System.Data.SqlClient;
//Access to the database using ADO.Net
using
System.Configuration;
namespace
NRTHWND
{
public class Customers
{
//Set the connection string from the web.config available to methods/functions within this class
//Set the connection string from the web.config available to methods/functions within this class
private static string _cnnString = "";
public static string ConnectionString
{
get { return _cnnString; }
set { _cnnString = value; }
}
public static DataTable GetAllCustomers()
{
SqlDataAdapter adp =
new SqlDataAdapter("SELECT ContactName as [Contact Name]", _cnnString);
DataSet ds =
new DataSet("Customers");
adp.Fill(ds,
"Customers");
return ds.Tables["Customers"];
}
public static List<CustomersDT> GetListOfCustomers()
{
List<CustomersDT> customerList = new List<CustomersDT>();
SqlDataAdapter adp =
new SqlDataAdapter("SELECT CustomerID,CustomerName FROM Customers", _cnnString);
adp.SelectCommand.Connection.Open();
SqlDataReader dr =
null;
dr = adp.SelectCommand.ExecuteReader();
if (dr.HasRows) {
while (dr.Read()) {
CustomersDT ds =
new CustomersDT();
ds.CustomerID =
Convert.ToInt32(dr["CustomerID"]);
ds.CompanyName = dr[
"CompanyName"].ToString();
//Add the customer to the list
customerList.Add(ds);
}
//Close the connections
dr.Close();
adp.SelectCommand.Connection.Close();
}
return customerList;
}
public static CustomersDT GetCustomer(int CustomerId)
{
CustomersDT ds =
new CustomersDT();
SqlDataAdapter adp =
new SqlDataAdapter("SELECT CustomerID,CompanyName FROM Customers where CustomerID=@CustomerID", _cnnString);
adp.SelectCommand.Parameters.AddWithValue(
"@CustomerID", CustomerId);
adp.SelectCommand.Connection.Open();
SqlDataReader dr =
null;
dr = adp.SelectCommand.ExecuteReader();
if (dr.HasRows) {
while (dr.Read()) {
ds.CustomerID =
Convert.ToInt32(dr["CustomerID"]);
ds.CompanyName = dr[
"CompanyName"].ToString();
if (UTIL.Utilities.IsDBNULL(dr["Date"]) != null){
ds.Date = (
DateTime)dr["Date"];
}
}
//Close the connections
dr.Close();
adp.SelectCommand.Connection.Close();
}
return ds;
}
public static void InsertCustomer(CustomersDT Customer)
{
using (SqlConnection nwConn1 = new SqlConnection(_cnnString)) {
SqlCommand nwCommand =
new SqlCommand();
SqlDataAdapter nwDataAdapter =
new SqlDataAdapter(nwCommand);
nwCommand.CommandText =
"insert into Customers (CompanyName,CustomerID) Values (@CompanyName,@CustomerID)";
//set the command type
nwCommand.CommandType = CommandType.Text;
nwCommand.Connection = nwConn1;
//create and add the parameters
nwCommand.Parameters.AddWithValue(
"@CompanyName", Customer.CompanyName);
nwCommand.Parameters.AddWithValue(
"@CustomerID", Customer.CustomerID);
nwConn1.Open();
nwCommand.ExecuteNonQuery();
}
}
public static void UpdateCustomer(CustomersDT Customer)
{
using (SqlConnection nwConn1 = new SqlConnection(_cnnString)) {
SqlCommand nwCommand =
new SqlCommand();
SqlDataAdapter nwDataAdapter =
new SqlDataAdapter(nwCommand);
nwCommand.CommandText =
"Update Customers Set CompanyName=@CompanyName,Date=@Date where CustomerID=@CustomerID";
//set the command type
nwCommand.CommandType = CommandType.Text;
nwCommand.Connection = nwConn1;
//create and add the parameters
nwCommand.Parameters.AddWithValue(
"@CompanyName", Customer.CompanyName);
nwCommand.Parameters.AddWithValue(
"@CustomerID", Customer.CustomerID);
nwCommand.Parameters.AddWithValue(
"@Date", Customer.Date);
nwConn1.Open();
nwCommand.ExecuteNonQuery();
}
}
public static void DeleteCustomer(int CustomerID)
{
using (SqlConnection nwConn1 = new SqlConnection(_cnnString)) {
SqlCommand nwCommand =
new SqlCommand();
SqlDataAdapter nwDataAdapter =
new SqlDataAdapter(nwCommand);
nwCommand.CommandText =
"delete from Customers where CustomerID=@CustomerID";
//set the command type
nwCommand.CommandType = CommandType.Text;
nwCommand.Connection = nwConn1;
//create and add the parameters
nwCommand.Parameters.AddWithValue(
"@CustomerID", CustomerID);
nwConn1.Open();
nwCommand.ExecuteNonQuery();
}
}
}
}
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
using
System.Web;
using
System.Data.SqlClient;
//Access to the database using ADO.Net
using
System.Configuration;
namespace
NRTHWND
{
public class Customers
{
//Set the connection string from the web.config available to methods/functions within this class
//Set the connection string from the web.config available to methods/functions within this class
private static string _cnnString = "";
public static string ConnectionString
{
get { return _cnnString; }
set { _cnnString = value; }
}
public static DataTable GetAllCustomers()
{
SqlDataAdapter adp =
new SqlDataAdapter("SELECT ContactName as [Contact Name]", _cnnString);
DataSet ds =
new DataSet("Customers");
adp.Fill(ds,
"Customers");
return ds.Tables["Customers"];
}
public static List<CustomersDT> GetListOfCustomers()
{
List<CustomersDT> customerList = new List<CustomersDT>();
SqlDataAdapter adp =
new SqlDataAdapter("SELECT CustomerID,CustomerName FROM Customers", _cnnString);
adp.SelectCommand.Connection.Open();
SqlDataReader dr =
null;
dr = adp.SelectCommand.ExecuteReader();
if (dr.HasRows) {
while (dr.Read()) {
CustomersDT ds =
new CustomersDT();
ds.CustomerID =
Convert.ToInt32(dr["CustomerID"]);
ds.CompanyName = dr[
"CompanyName"].ToString();
//Add the customer to the list
customerList.Add(ds);
}
//Close the connections
dr.Close();
adp.SelectCommand.Connection.Close();
}
return customerList;
}
public static CustomersDT GetCustomer(int CustomerId)
{
CustomersDT ds =
new CustomersDT();
SqlDataAdapter adp =
new SqlDataAdapter("SELECT CustomerID,CompanyName FROM Customers where CustomerID=@CustomerID", _cnnString);
adp.SelectCommand.Parameters.AddWithValue(
"@CustomerID", CustomerId);
adp.SelectCommand.Connection.Open();
SqlDataReader dr =
null;
dr = adp.SelectCommand.ExecuteReader();
if (dr.HasRows) {
while (dr.Read()) {
ds.CustomerID =
Convert.ToInt32(dr["CustomerID"]);
ds.CompanyName = dr[
"CompanyName"].ToString();
if (UTIL.Utilities.IsDBNULL(dr["Date"]) != null){
ds.Date = (
DateTime)dr["Date"];
}
}
//Close the connections
dr.Close();
adp.SelectCommand.Connection.Close();
}
return ds;
}
public static void InsertCustomer(CustomersDT Customer)
{
using (SqlConnection nwConn1 = new SqlConnection(_cnnString)) {
SqlCommand nwCommand =
new SqlCommand();
SqlDataAdapter nwDataAdapter =
new SqlDataAdapter(nwCommand);
nwCommand.CommandText =
"insert into Customers (CompanyName,CustomerID) Values (@CompanyName,@CustomerID)";
//set the command type
nwCommand.CommandType = CommandType.Text;
nwCommand.Connection = nwConn1;
//create and add the parameters
nwCommand.Parameters.AddWithValue(
"@CompanyName", Customer.CompanyName);
nwCommand.Parameters.AddWithValue(
"@CustomerID", Customer.CustomerID);
nwConn1.Open();
nwCommand.ExecuteNonQuery();
}
}
public static void UpdateCustomer(CustomersDT Customer)
{
using (SqlConnection nwConn1 = new SqlConnection(_cnnString)) {
SqlCommand nwCommand =
new SqlCommand();
SqlDataAdapter nwDataAdapter =
new SqlDataAdapter(nwCommand);
nwCommand.CommandText =
"Update Customers Set CompanyName=@CompanyName,Date=@Date where CustomerID=@CustomerID";
//set the command type
nwCommand.CommandType = CommandType.Text;
nwCommand.Connection = nwConn1;
//create and add the parameters
nwCommand.Parameters.AddWithValue(
"@CompanyName", Customer.CompanyName);
nwCommand.Parameters.AddWithValue(
"@CustomerID", Customer.CustomerID);
nwCommand.Parameters.AddWithValue(
"@Date", Customer.Date);
nwConn1.Open();
nwCommand.ExecuteNonQuery();
}
}
public static void DeleteCustomer(int CustomerID)
{
using (SqlConnection nwConn1 = new SqlConnection(_cnnString)) {
SqlCommand nwCommand =
new SqlCommand();
SqlDataAdapter nwDataAdapter =
new SqlDataAdapter(nwCommand);
nwCommand.CommandText =
"delete from Customers where CustomerID=@CustomerID";
//set the command type
nwCommand.CommandType = CommandType.Text;
nwCommand.Connection = nwConn1;
//create and add the parameters
nwCommand.Parameters.AddWithValue(
"@CustomerID", CustomerID);
nwConn1.Open();
nwCommand.ExecuteNonQuery();
}
}
}
}
and CustomersDT.cs
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
namespace
NRTHWND
{
public class CustomersDT
{
public int CustomerID;
public string CompanyName;
//This is becuase in the database the value can be null
public Nullable<DateTime> Date;
}
}
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
namespace
NRTHWND
{
public class CustomersDT
{
public int CustomerID;
public string CompanyName;
//This is becuase in the database the value can be null
public Nullable<DateTime> Date;
}
}
In the website portion (folder called website) I have editCustomers.aspx
<%
@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="EditCustomers.aspx.cs" Inherits="_EditCustomers" %>
<%
@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
<!
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>Edit the Customer Table</title>
<style type="text/css">
.style1
{
width: 400px;
}
</style>
</
head>
<
body>
<form id="form1" runat="server">
<table align="center" class="style1">
<tr>
<td class="style2" colspan="2">
<asp:Label ID="lblUserMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
Select Customer
</td>
<td>
<asp:DropDownList ID="dlCustomers" runat="server" AutoPostBack="True"
DataTextField="CompanyName"
DataValueField="CustomerID"
onselectedindexchanged="dlCustomers_SelectedIndexChanged"
ondatabound="dlCustomers_DataBound">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style2">
Customer Name
</td>
<td>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtCompanyName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
Date Created
</td>
<td>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtDate" Display="Dynamic" ErrorMessage="MM/DD/YYYY"
ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" />
</td>
</tr>
</
table>
</form>
</
body>
</
html>
<%
@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="EditCustomers.aspx.cs" Inherits="_EditCustomers" %>
<%
@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
<!
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>Edit the Customer Table</title>
<style type="text/css">
.style1
{
width: 400px;
}
</style>
</
head>
<
body>
<form id="form1" runat="server">
<table align="center" class="style1">
<tr>
<td class="style2" colspan="2">
<asp:Label ID="lblUserMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
Select Customer
</td>
<td>
<asp:DropDownList ID="dlCustomers" runat="server" AutoPostBack="True"
DataTextField="CompanyName"
DataValueField="CustomerID"
onselectedindexchanged="dlCustomers_SelectedIndexChanged"
ondatabound="dlCustomers_DataBound">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style2">
Customer Name
</td>
<td>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtCompanyName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
Date Created
</td>
<td>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtDate" Display="Dynamic" ErrorMessage="MM/DD/YYYY"
ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" />
</td>
</tr>
</
table>
</form>
</
body>
</
html>
and editCustomers.aspx.cs
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
using
NRTHWND;
using
System.Web.UI.WebControls;
using
System.Configuration;
partial
class _EditCustomers : System.Web.UI.Page
{
private string _dlDefault = "--- Select Item ---";
private string _connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void btnSave_Click(object sender, System.EventArgs e)
{
if (dlCustomers.SelectedValue == _dlDefault)
{
return;
}
CustomersDT customer =
new CustomersDT();
customer.CustomerID =
Convert.ToInt32(dlCustomers.SelectedValue);
customer.CompanyName = txtCompanyName.Text;
customer.Date =
Convert.ToDateTime(txtDate.Text);
try
{
//Update the customer
NRTHWND.Customers.UpdateCustomer(customer);
lblUserMessage.Text =
"The customer was successfully updated";
//rebind the dropdownlist with the changes to the customer name
}
catch (Exception ex)
{
lblUserMessage.Text =
"There was an error updating the customer please contact the site administrator <br>" + ex.Message.ToString();
}
}
protected void dlCustomers_DataBound(object sender, EventArgs e)
{
//example of inserting a default value in the 0 index of the field
dlCustomers.Items.Insert(0, _dlDefault);
dlCustomers.SelectedValue = _dlDefault;
}
protected void dlCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
//clear the message
lblUserMessage.Text =
"";
//check that the default is not selected
if (dlCustomers.SelectedValue == _dlDefault)
{
return;
}
//Load Companies
LoadCustomers();
//Retrieve the information from the database and fill the form for update
int CustomerId = Convert.ToInt32(dlCustomers.SelectedValue);
CustomersDT CustomersDT =
default(CustomersDT);
CustomersDT = NRTHWND.Customers.GetCustomer(CustomerId);
//Set the controls on the form to the values for the selected customers
txtCompanyName.Text = CustomersDT.CompanyName;
if (UTIL.Utilities.IsDBNULL(CustomersDT.Date) != null)
{
txtDate.Text = CustomersDT.Date.ToString();
}
else
{
txtDate.Text =
"";
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
Customers.ConnectionString = _connString;
//Only run this on the initial page load
if (Page.IsPostBack == false)
{
LoadCustomers();
}
}
private void LoadCustomers()
{
//Load the dropdownlist with the customers from the database
dlCustomers.Items.Clear();
List<CustomersDT> customers = null;
customers = NRTHWND.Customers.GetListOfCustomers();
//Add the default
dlCustomers.Items.Add(_dlDefault);
//Loop through and Add all the items from the database
foreach (CustomersDT i in customers)
{
ListItem li =
new ListItem(i.CompanyName, i.CustomerID.ToString());
dlCustomers.Items.Add(li);
}
}
}
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
using
NRTHWND;
using
System.Web.UI.WebControls;
using
System.Configuration;
partial
class _EditCustomers : System.Web.UI.Page
{
private string _dlDefault = "--- Select Item ---";
private string _connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void btnSave_Click(object sender, System.EventArgs e)
{
if (dlCustomers.SelectedValue == _dlDefault)
{
return;
}
CustomersDT customer =
new CustomersDT();
customer.CustomerID =
Convert.ToInt32(dlCustomers.SelectedValue);
customer.CompanyName = txtCompanyName.Text;
customer.Date =
Convert.ToDateTime(txtDate.Text);
try
{
//Update the customer
NRTHWND.Customers.UpdateCustomer(customer);
lblUserMessage.Text =
"The customer was successfully updated";
//rebind the dropdownlist with the changes to the customer name
}
catch (Exception ex)
{
lblUserMessage.Text =
"There was an error updating the customer please contact the site administrator <br>" + ex.Message.ToString();
}
}
protected void dlCustomers_DataBound(object sender, EventArgs e)
{
//example of inserting a default value in the 0 index of the field
dlCustomers.Items.Insert(0, _dlDefault);
dlCustomers.SelectedValue = _dlDefault;
}
protected void dlCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
//clear the message
lblUserMessage.Text =
"";
//check that the default is not selected
if (dlCustomers.SelectedValue == _dlDefault)
{
return;
}
//Load Companies
LoadCustomers();
//Retrieve the information from the database and fill the form for update
int CustomerId = Convert.ToInt32(dlCustomers.SelectedValue);
CustomersDT CustomersDT =
default(CustomersDT);
CustomersDT = NRTHWND.Customers.GetCustomer(CustomerId);
//Set the controls on the form to the values for the selected customers
txtCompanyName.Text = CustomersDT.CompanyName;
if (UTIL.Utilities.IsDBNULL(CustomersDT.Date) != null)
{
txtDate.Text = CustomersDT.Date.ToString();
}
else
{
txtDate.Text =
"";
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
Customers.ConnectionString = _connString;
//Only run this on the initial page load
if (Page.IsPostBack == false)
{
LoadCustomers();
}
}
private void LoadCustomers()
{
//Load the dropdownlist with the customers from the database
dlCustomers.Items.Clear();
List<CustomersDT> customers = null;
customers = NRTHWND.Customers.GetListOfCustomers();
//Add the default
dlCustomers.Items.Add(_dlDefault);
//Loop through and Add all the items from the database
foreach (CustomersDT i in customers)
{
ListItem li =
new ListItem(i.CompanyName, i.CustomerID.ToString());
dlCustomers.Items.Add(li);
}
}
}
The errors I'm trying to figure out are:
Error 1 c:\Desktop\Final\Website\SearchCustomers.aspx.cs(10): error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
Error 1 c:\Desktop\Final\Website\SearchCustomers.aspx.cs(10): error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
Error 2 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). \Desktop\Final\Website\SearchCustomers.aspx.cs 10
Error 2 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). \Desktop\Final\Website\SearchCustomers.aspx.cs 10
Error 3 'ASP.searchcustomers_aspx.SupportAutoEvents': no suitable method found to override c:\AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 205
Error 3 'ASP.searchcustomers_aspx.SupportAutoEvents': no suitable method found to override c:\AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 205
Error 4 'ASP.searchcustomers_aspx.GetTypeHashCode()': no suitable method found to override c:\AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 631
Error 4 'ASP.searchcustomers_aspx.GetTypeHashCode()': no suitable method found to override c:\AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 631
Error 5 'ASP.searchcustomers_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override \AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 636
Error 5 'ASP.searchcustomers_aspx.ProcessRequest(System.Web.HttpContext)': no suitable method found to override \AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 636
Error 6 'ASP.searchcustomers_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable' c:\AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 178
Error 6 'ASP.searchcustomers_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable' c:\AppData\Local\Temp\Temporary ASP.NET Files\website\37df428a\b0d335c7\App_Web_ug3scxve.0.cs 178