我正在尝试将复选框列表写入数据库,我错过了什么?

时间:2017-12-13 15:14:40

标签: c# asp.net

我是C#的新手,我认为我还没有很好地掌握如何写入数据库。我不确定如何将复选框写入数据库中的特定列。如果您能看到我需要专门学习的区域,请告诉我。在试图教自己如何做这样的事情时,很难知道我需要知道什么。谢谢你的帮助。

如果您需要任何进一步的信息,请告诉我。

目标:我的目标是将Checkbox选项写入数据库中的表。每个复选框在表中都有一列,我只想在检查某些内容时填充“Y”,如果未选中则不填充任何内容。列的标题与复选框标签相同。

问题: 我不知道如何将复选框列表中的数据导入数据库。

数据库结构:

enter image description here

网站草稿示例:

enter image description here

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Revocations.Default_2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/StyleSheet1.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="t_row_header">
            <asp:Image ID="imgLogo" runat="server" CssClass="logo" ImageUrl="~/Images/logo.png" />
        </div>
        <h1>Transfer Revocations</h1>
        <div class="form-fields">

            <div class="form-group-left">
                <label>
                    <asp:RequiredFieldValidator ID="rqvldSchoolYear" runat="server" ControlToValidate="txtSchoolYear" CssClass="ErrorMessage" ErrorMessage="Please enter a school   year."   SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    School Year:
                    <asp:CompareValidator ID="cmpvldSchoolYear" runat="server" ControlToValidate="txtSchoolYear" CssClass="ErrorMessage" ErrorMessage="Please enter a school year  &gt;=    2017." Operator="GreaterThanEqual" SetFocusOnError="True" ValueToCompare="2017">*</asp:CompareValidator>
                </label>
                &nbsp;<asp:TextBox ID="txtSchoolYear" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="rqvldStudentID" runat="server" ControlToValidate="txtStudentID" CssClass="ErrorMessage" ErrorMessage="Please enter a student ID."     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Student ID:</label>&nbsp;<asp:TextBox ID="txtStudentID" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="rqvldFirstName" runat="server" ControlToValidate="txtFirstName" CssClass="ErrorMessage" ErrorMessage="Please enter a first name."     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    First Name:</label>&nbsp;<asp:TextBox ID="txtFirstName" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="rqvldLastName" runat="server" ControlToValidate="txtLastName" CssClass="ErrorMessage" ErrorMessage="Please enter a last name"     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Last Name:</label>&nbsp;<asp:TextBox ID="txtLastName" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="rqvldRevokedSchool" runat="server" ControlToValidate="txtRevokedSchool" CssClass="ErrorMessage" ErrorMessage="Please enter the     revoking school" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Revoked School:</label>&nbsp;<asp:TextBox ID="txtRevokedSchool" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="rqvldRevocationDate" runat="server" ControlToValidate="txtRevocationDate" CssClass="ErrorMessage" ErrorMessage="Please enter a     revocation date" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Revocation Date:</label>&nbsp;<asp:TextBox ID="txtRevocationDate" runat="server" TextMode="Date" CssClass="txtBox"></asp:TextBox>

            </div>
            <!-- form-group-left -->

            <div class="form-group-right">
                <label>
                    Reason:<asp:CheckBoxList ID="chkbxlst1" runat="server">
                        <asp:ListItem text="Academics"  Value="1"></asp:ListItem>
                        <asp:ListItem text="Attendance" Value="2"></asp:ListItem>
                        <asp:ListItem text="Behavior"  Value ="3"></asp:ListItem>
                        <asp:ListItem text="Cooperative Relationship" Value="4"></asp:ListItem>
                    </asp:CheckBoxList>
                    <br />
                </label>
                <label>
                    <asp:RequiredFieldValidator ID="rqvldNotes" runat="server" ControlToValidate="txtNotes" CssClass="ErrorMessage" ErrorMessage="Please enter a note."     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Notes:</label>&nbsp;&nbsp;<asp:TextBox ID="txtNotes" runat="server" height="218px" TextMode="MultiLine" CssClass="InsideShadow"></asp:TextBox>
                <br />
                <asp:Button ID="btnSubmit" runat="server" Text="SUBMIT" CssClass="btn" OnClick="btnSubmit_Click" />
            </div>

        </div>
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ErrorMessage" />


        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PPSS_RevocationConnection %>" DeleteCommand="SELECT 
       [SchoolYear]
      ,[Student_ID]
      ,[Last_Name]
      ,[First_Name]
      ,[Revoked_School]
      ,CAST([Revocation_Date] AS DATE) AS [Revocation_Date]
      ,[Notes]
      ,[Academics]
      ,[Attendance]
      ,[Behavior]
      ,[Cooperative Relationship]

  FROM [PPSS_Work].[Transfers].[Revocations]"
            SelectCommand="SELECT 
       [SchoolYear]
      ,[Student_ID]
      ,[Last_Name]
      ,[First_Name]
      ,[Revoked_School]
      ,CAST([Revocation_Date] AS DATE) AS [Revocation_Date]
      ,[Notes]
      ,[Academics]
      ,[Attendance]
      ,[Behavior]
      ,[Cooperative Relationship]

  FROM [PPSS_Work].[Transfers].[Revocations]"
            UpdateCommand="SELECT 
       [SchoolYear]
      ,[Student_ID]
      ,[Last_Name]
      ,[First_Name]
      ,[Revoked_School]
      ,CAST([Revocation_Date] AS DATE) AS [Revocation_Date]
      ,[Notes]
      ,[Academics]
      ,[Attendance]
      ,[Behavior]
      ,[Cooperative Relationship]

  FROM [PPSS_Work].[Transfers].[Revocations]"></asp:SqlDataSource>
    </form>
</body>
</html>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;


namespace Revocations
{
    public partial class Default_2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Establish SQL Connection

            SqlConnection sqlCon = new SqlConnection("Data Source = acctsql; Initial Catalog = PPSS_Work; Integrated Security = True");
            {
                // Write INSERT SQL statement
                SqlCommand sqlCom = new SqlCommand("INSERT INTO [PPSS_Work].[Transfers].[t_Revocations](SchoolYear, Student_ID, Last_Name, First_Name,    Revoked_School,Revocation_Date,  Academics, Attendance, Behavior, Cooperative Relationship, Notes) VALUES(@SchoolYear, @Student_ID, @First_Name, @Last_Name,    @Revoked_School,  @Revocation_Date,@Academics, @Attendance, @Behavior, @Cooperative Relationship, @Notes)", sqlCon);

                sqlCom.Parameters.AddWithValue("@SchoolYear", txtSchoolYear.Text);
                sqlCom.Parameters.AddWithValue("@Student_ID", txtStudentID.Text);
                sqlCom.Parameters.AddWithValue("@First_Name", txtFirstName.Text);
                sqlCom.Parameters.AddWithValue("@Last_Name", txtLastName.Text);
                sqlCom.Parameters.AddWithValue("@Revoked_School", txtRevokedSchool.Text);
                sqlCom.Parameters.AddWithValue("@Revocation_Date", txtRevocationDate.Text);
                sqlCom.Parameters.AddWithValue("@Notes", txtNotes.Text);


                sqlCom.Parameters.AddWithValue("@Academics", txtNotes.Text);
                sqlCom.Parameters.AddWithValue("@Attendance", txtNotes.Text);
                sqlCom.Parameters.AddWithValue("@Behavior", txtNotes.Text);
                sqlCom.Parameters.AddWithValue("@[Cooperative Relationship]", txtNotes.Text);

                sqlCon.Open();
                sqlCom.ExecuteNonQuery();
                sqlCon.Close();

                if (IsPostBack)
                {
                    txtSchoolYear.Text = "";
                    txtStudentID.Text = "";
                    txtFirstName.Text = "";
                    txtLastName.Text = "";
                    txtRevokedSchool.Text = "";
                    txtRevocationDate.Text = "";
                    chkbxlst1.Text = "";
                    txtNotes.Text = "";
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:3)

问题在于您没有将数据写入数据库的代码。

您需要遍历chkbxlst1中的项目并设置正确的参数:

foreach(ListItem item in chkbxlst1.Items)
{
    if(item.Checked)
        sqlCom.Parameters.AddWithValue("@"+item.Text, "Y");
}

说明:

  1. 不要将Y/N用于列。更改列以使用BIT类型。
  2. 我在ListItem中使用了强制转换为foreach,因为我不确定CheckBoxList的Items属性的类型是什么。