我在Microsoft Visual Studio中使用C#构建了一个网站。
我有一个Customer
,一个Products
,一个Orders
和一个OrdersItem
表。我的网站销售产品,有购物车和结帐页面。
客户使用她/她的用户名和密码登录网站,并选择他们想要购买的产品。然后将产品添加到购物车中,一旦客户点击Checkout,它就会打开以下页面
请参阅下面的.aspx标记
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContent" runat="server">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-lg-12">
<div class="form-group alert alert-info">
<asp:label runat="server" ID="lblCheckOut">Please enter your Details to purchase your Beer below:</asp:label>
</div>
<div class="form-group">
<asp:Label ID="lblName" Text="Your name: *" AssociatedControlID="txtName" runat="server"></asp:Label>
<asp:textbox name="txtName" id="txtName" tabindex="2" class="form-control" placeholder="Your name" required="required" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblEmail" Text="Your email: *" AssociatedControlID="txtEmail" runat="server"></asp:Label>
<asp:textbox name="txtEmail" id="txtEmail" tabindex="2" class="form-control" placeholder="Your email" required="required" TextMode="Email" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblPhone" Text="Your phone: *" AssociatedControlID="txtPhone" runat="server"></asp:Label>
<asp:textbox name="txtPhone" id="txtPhone" tabindex="2" class="form-control" placeholder="Your Phone" required="required" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblAddress1" Text="Your address: *" AssociatedControlID="txtAddress1" runat="server"></asp:Label>
<asp:textbox name="txtAddress1" id="txtAddress1" tabindex="2" class="form-control" placeholder="Your address" TextMode="MultiLine" required="required" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblAddress2" Text="Your Town:" AssociatedControlID="txtAddress2" runat="server"></asp:Label>
<asp:textbox name="txtAddress2" id="txtAddress2" tabindex="2" class="form-control" placeholder="Your Town" TextMode="MultiLine" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblAddress3" Text="Your County: *" AssociatedControlID="txtAddress3" runat="server"></asp:Label><br/>
<asp:textbox name="txtAddress3" id="txtAddress3" tabindex="2" class="form-control" placeholder="Your County" required="required" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblCountry" Text="Your Country: *" AssociatedControlID="txtCountry" runat="server"></asp:Label><br/>
<asp:textbox name="txtCountry" id="txtCountry" tabindex="2" class="form-control" placeholder="Your Country" required="required" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblTotalCost" Text="Amount to be Paid:" AssociatedControlID="ddlCreditCardType" runat="server" ></asp:Label>
<asp:textbox name="txtTotalCost" id="txtTotalCost" tabindex="2" class="form-control" placeholder="" runat="server"></asp:textbox>
</div>
<div class="form-group">
<asp:Label ID="lblCardType" Text="Card type *" AssociatedControlID="ddlCreditCardType" runat="server"></asp:Label>
<asp:dropdownlist id="ddlCreditCardType" tabindex="2" class="form-control" runat="server">
<asp:ListItem value="friendship">VISA</asp:ListItem>
<asp:ListItem value="employment">MasterCard</asp:ListItem>
<asp:ListItem value="other">Other</asp:ListItem>
</asp:dropdownlist>
</div>
<div class="form-group">
<asp:Label ID="lblCardNumber" Text="Credit card number *" AssociatedControlID="txtCardNumber" runat="server"></asp:Label><br/>
<asp:textbox name="txtCardNumber" id="txtCardNumber" tabindex="2" class="form-control" placeholder="Your card number" required="required" runat="server"></asp:textbox>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<asp:button runat="server" text="Check out" id="btnCheckOut" OnClick="btnCheckOut_Click" />
</div>
</div>
<div class="row">
<div class="col-md-1 col-md-offset-11">
<a href="/Backend/Default.aspx" class="btn btn-default pull-right">Continue Shopping</a>
</div>
</div>
</div>
</div>
</div>
</div>
</asp:Content>
这背后的.aspx.cs
代码如下。 aspx代码可以正常工作。我想要做的是获取aspx.cx代码,以便在单击Checkout时使用客户的详细信息填充表单。在ShowCustomerDetails
下面的代码中,我希望它填充名称,电子邮件和电话详细信息。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CA4.Shop
{
public partial class CheckOut : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string q = Request.QueryString["id"];
int id = 0;
int.TryParse(q, out id);
if (Session["username"] == null)
Response.Redirect("../CustomerLogin.aspx");
else
{
ShowCustomerDetails(id);
}
double CartTotalCost = 0;
if (Session["BeerCart"] == null)
{
Response.Redirect("~/Shop");
}
else
{
CartTotalCost = GetCartTotalCost();
txtTotalCost.Text = string.Format("{0:C}", CartTotalCost);
}
}
private void ShowCustomerDetails(int id)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
string getCustomerDetails = "SELECT Name, Email, Phone FROM Customers WHERE CustomerId = @id";
//get email based on id
SqlCommand getCustomerCommand = new SqlCommand(getCustomerDetails, connection);
getCustomerCommand.Parameters.AddWithValue("@id", id);
connection.Open();
SqlDataReader reader = getCustomerCommand.ExecuteReader();
while (reader.Read())
{
txtName.Text = reader["Name"].ToString();
txtEmail.Text = reader["Email"].ToString();
txtPhone.Text = reader["Phone"].ToString();
}
reader.Close();
connection.Close();
}
protected void btnCheckOut_Click(object sender, EventArgs e)
{
string Name = txtName.Text;
string Email = txtEmail.Text;
string Phone = txtPhone.Text;
string Address1 = txtAddress1.Text;
string Address2 = txtAddress2.Text;
string Address3 = txtAddress3.Text;
string Country = txtCountry.Text;
//for this example we wont store the card details
int CustomerId = 0;
CustomerId = AddCustomerDetails(Name, Email, Phone, Address1, Address2, Address3, Country);
PlaceOrderForCustomer(CustomerId);
Session["BeerCart"] = null;
Response.Redirect("~/Shop/Thanks.aspx");
}
private double GetCartTotalCost()
{
double Price = 0;
double ProductTotal = 0;
double CartTotal = 0;
Dictionary<int, int> cart = (Dictionary<int, int>)Session["BeerCart"];
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
foreach (var item in cart)
{
string query = "SELECT [BeerId], [Price] FROM [Products] WHERE [BeerId] = @id";
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@id", item.Key);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Price = double.Parse(reader["Price"].ToString());
ProductTotal = Price * item.Value;
CartTotal += ProductTotal;
}
connection.Close();
}
return CartTotal;
}
private int AddCustomerDetails(string Name, string Email, string Phone, string Address1, string Address2, string Address3, string Country)
{
int CustomerId = 0;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
string sqlStatement = "Select Name, Email, Phone, Address1, Address2, Address3, Country) Values (@Name,@Email,@Phone,@Address1,@Address2,@Address3,@Country) FROM Customers Where [CustomerId] = @id ;SELECT SCOPE_IDENTITY();";
SqlCommand command = new SqlCommand(sqlStatement, connection);
command.Parameters.AddWithValue("@Name", Name);
command.Parameters.AddWithValue("@Email", Email);
command.Parameters.AddWithValue("@Phone", Phone);
command.Parameters.AddWithValue("@Address1", Address1);
command.Parameters.AddWithValue("@Address2", Address2);
command.Parameters.AddWithValue("@Address3", Address3);
command.Parameters.AddWithValue("@Country", Country);
connection.Open();
CustomerId = int.Parse(command.ExecuteScalar().ToString());
connection.Close();
return CustomerId;
}
private void PlaceOrderForCustomer(int CustomerId)
{
int BeerId = 0;
int OrderId = CreateOrder(CustomerId);
string Brand = string.Empty;
string Name = string.Empty;
double Price = 0;
Dictionary<int, int> cart = (Dictionary<int, int>)Session["BeerCart"];
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
foreach (var item in cart)
{
string query = "SELECT [BeerId], [Brand], [Name], [Price] FROM [Products] WHERE [BeerId] = @id";
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@id", item.Key);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
BeerId = int.Parse(reader["BeerId"].ToString());
Brand = reader["Brand"].ToString();
Name = reader["Name"].ToString();
Price = double.Parse(reader["Price"].ToString());
AddItemToOrder(OrderId, BeerId, Brand, Name, Price, item.Value);
}
connection.Close();
}
}
private int CreateOrder(int CustomerId)
{
int OrderId = 0;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
string sqlStatement = "INSERT INTO [Orders] (CustomerId, OrderDate) Values (@CustomerId,@OrderDate);SELECT SCOPE_IDENTITY();";
SqlCommand command = new SqlCommand(sqlStatement, connection);
command.Parameters.AddWithValue("@CustomerId", CustomerId);
command.Parameters.AddWithValue("@OrderDate", DateTime.Now);
connection.Open();
OrderId = int.Parse(command.ExecuteScalar().ToString());
connection.Close();
return OrderId;
}
private void AddItemToOrder(int OrderId, int BeerId, string Brand, string Name, double Price, int Quantity)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
string sqlStatement = "INSERT INTO OrderItem (OrderId, BeerId, Brand, Name, Price, Quantity) Values (@OrderId,@BeerId,@Brand,@Name,@Price,@Quantity);";
SqlCommand command = new SqlCommand(sqlStatement, connection);
command.Parameters.AddWithValue("@OrderId", OrderId);
command.Parameters.AddWithValue("@BeerId", BeerId);
command.Parameters.AddWithValue("@Brand", Brand);
command.Parameters.AddWithValue("@Name", Name);
command.Parameters.AddWithValue("@Price", Price);
command.Parameters.AddWithValue("@Quantity", Quantity);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
CREATE TABLE [dbo].[Customers]
(
[CustomerId] INT IDENTITY (1, 1) NOT NULL,
[Username] NVARCHAR(50) NOT NULL,
[Password] NVARCHAR(50) NOT NULL,
[Name] NVARCHAR(100) NOT NULL,
[Email] NVARCHAR(50) NOT NULL,
[Phone] NUMERIC(18) NOT NULL,
[Address1] NVARCHAR(25) NOT NULL,
[Address2] NVARCHAR(25) NULL,
[Address3] NVARCHAR(25) NOT NULL,
[Country] NVARCHAR(25) NOT NULL,
PRIMARY KEY CLUSTERED ([CustomerId] ASC)
);
这是一个大学项目。我希望我已经提供了足够的细节。
由于 安东尼。