gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:10 26 4
gpt4 key购买 nike

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

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

目标:我的目标是将复选框选择写入数据库中的表。每个复选框在表中都有一列,我只想在选中某些内容时填充“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 = "";
}
}
}

}
}

最佳答案

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

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

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

备注:

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

关于c# - 我正在尝试将复选框列表写入数据库,我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com