我已成功将数据插入[Survey]表。
但我还想同时将数据(金额)附加到[投票]表格中,这会增加用户点击提交时所选择的披萨的数据(金额)+1。
最后如果可以完成这一步,我将需要构建一个饼图,Chart.js从我已经创建的数据库中检索,但是我在脚本中手动设置。滚动到最后,查看chart.js饼图。
目前投票= 0。 [Vote] table Data
这是我的Sql查询。(我创建一个名为&#34的dbml项目; myconnection")
CREATE TABLE [dbo].[Survey] (
[SurveyId] INT IDENTITY (1, 1) NOT NULL,
[MemberName] VARCHAR (100) NULL,
[PizzaVote] VARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([SurveyId] ASC)
);
CREATE TABLE [dbo].[Vote] (
[VoteId] INT IDENTITY (1, 1) NOT NULL,
[PizzaName] VARCHAR (100) NULL,
[amount] INT NULL,
PRIMARY KEY CLUSTERED ([VoteId] ASC)
);
这是我的Survey.aspx正面代码。(survey.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Survey.aspx.cs" Inherits="MYDemo.Survey" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="auto-style1" colspan="2">
<h1>Survey</h1>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td class="auto-style2">Name</td>
<td>
<asp:TextBox ID="txtSurName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSurName" ErrorMessage="Please enter your name!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Preffered Pizza?</td>
<td>
<asp:DropDownList ID="ddlPrePizza" runat="server">
<asp:ListItem Value="0">Select One</asp:ListItem>
<asp:ListItem Value="1">Island Supreme</asp:ListItem>
<asp:ListItem Value="2">Hawaiian Supreme</asp:ListItem>
<asp:ListItem Value="3">Blazing Seafood</asp:ListItem>
<asp:ListItem Value="4">Chicken Pepperoni</asp:ListItem>
<asp:ListItem Value="5">Deluxe Cheese</asp:ListItem>
<asp:ListItem Value="6">Island Tuna</asp:ListItem>
<asp:ListItem Value="7">Haiwaiian Chicken</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddlPrePizza" ErrorMessage="Please select one pizza!" InitialValue="0"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
这是我背后的代码。(survey.aspx.cs)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MYDemo
{
public partial class Survey : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String CS = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd2 = new SqlCommand("insert into Survey (MemberName,PizzaVote) values(@MemberName, @PizzaVote)", con);
cmd2.Parameters.AddWithValue("@MemberName", txtSurName.Text);
cmd2.Parameters.AddWithValue("@PizzaVote", ddlPrePizza.SelectedItem.Text);
con.Open();
cmd2.ExecuteNonQuery();
con.Close();
txtSurName.Text = string.Empty;
ddlPrePizza.SelectedIndex = 0;
}
}
}
}
这是我用Chart.js创建的饼图。
<%--<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChartJS.aspx.cs" Inherits="LoginAndRegister.ChartJS" %>--%>
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="ChartJS.aspx.cs" Inherits="LoginAndRegister.ChartJS" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="server">
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%--<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="js/script.js"></script>--%>
<link href="StyleSheet1.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
<script src="js/Chart.min.js"></script>
<title></title>
<style type="text/css">
.auto-style1 {
text-decoration: underline;
font-weight: bold;
}
</style>
</head>
<body>
<%--<form id="form1" runat="server">--%>
<div class="wrapper">
<h1>Online Survey on <span class="auto-style1"><strong>10000</strong></span> customer on their Prefer Pizza in Best Pizza</h1>
<br />
<br />
<br />
<br />
<%--<canvas id="myChart" width="300" height="300"></canvas>--%>
<canvas id="myChart"></canvas>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["ISLAND SUPREME", "HAWAIIAN SUPREME", "BLAZING SEAFOOD", "CHICKEN PEPPERONI", "DELUXE CHEESE", "ISLAND TUNA", "HAWAIIAN CHICKEN"],
datasets: [{
backgroundColor: [
"#2ecc71",
"#3498db",
"#95a5a6",
"#9b59b6",
"#f1c40f",
"#e74c3c",
"#34495e"
],
data: [1000, 1309, 2003, 1117, 1028, 1024, 2007]
}]
}
});
</script>
</div>
<%--</form>--%>
</body>
</html>
</asp:Content>