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

问题描述:

我是C#的新手,我认为我对如何写数据库还没有很好的了解.我不确定如何将复选框写入数据库中的特定列.如果您看到我需要专门研究的领域,请告诉我.试图教自己如何做这样的事情,我很难知道我需要知道些什么.谢谢您的帮助.

I'm new to C# and I don't think I have good grasp on how to write to a database yet. I'm not sure how to write checkboxes to a specific column in a database. If you can see an area I need to specifically study, please let me know. It's hard to know what I need to know in trying to teach myself how to do things like this. Thank you for your help.

如果您需要其他任何信息,请告诉我.

If you need any further information, just let me know.

目标:我的目标是将Checkbox选项写入数据库中的表.每个复选框在表中都有一列,我只希望如果选中某项,则填充'Y',如果不选中,则不填充任何内容.列的标题与复选框标签相同.

Goal: My goal is to write the Checkbox selections to a table in a database. Each checkbox has a column in a table, and I just want a 'Y' to be populated if something is checked and nothing populated if it's not checked. The titles of the columns are the same as the checkbox labels.

问题:我不确定如何从复选框列表中将数据导入数据库.

Problem: I'm not sure how to get the data from the checkboxlist, into the database.

数据库结构:

网站草稿示例:

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 = "";
                }
            }
        }

    }
}

问题是您没有将数据写入数据库的代码.

The problem is that you don't have the code to write the data into the database.

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

You need to iterate through items in chkbxlst1 and set the proper parameters:

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

备注:

  1. 请勿在列中使用 Y/N .更改列以使用 BIT 类型.
  2. 我在 foreach 中使用了对 ListItem 的强制转换,因为我不确定CheckBoxList的 Items 属性的类型是什么./li>
  1. Do not use Y/N for your columns. Change the columns to use BIT type.
  2. I used a cast to ListItem in foreach because I'm not sure what is the type of Items property of your CheckBoxList.