【问题标题】:Cannot insert the value NULL into column 'SchoolID', table 'TDC.dbo.SWprjectPart' which is dependent dropdownlist无法将值 NULL 插入到列“SchoolID”、表“TDC.dbo.SWprjectPart”中,这是从属下拉列表
【发布时间】:2020-09-21 08:38:05
【问题描述】:

您好,我希望我的问题得到了解决...我有一个包含 2 个下拉列表的列表视图,它在显示数据中运行良好,但是当单击插入按钮时,我得到了无法插入空值的错误,因此它无法读取我的第二个下拉列表的选定值

这是我的html代码

  <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SwProjParticpant.aspx.cs" Inherits="WebApplication1.Account.SwProjParticpant" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TDCConnectionString2 %>" SelectCommand="Select distinct * from SupervisionSoftware INNER JOIN Schools ON SupervisionSoftware.SchoolID=Schools.SchoolID INNER JOIN Projects ON Projects.ProjectID=SupervisionSoftware.ProjectID"></asp:SqlDataSource>
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="PartID,ProjectID" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">
        <AlternatingItemTemplate>
            <tr style="background-color: #FAFAD2;color: #284775;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td>
                    <asp:Label ID="PartIDLabel" runat="server" Text='<%# Eval("PartID") %>' />
                </td>
                <td>
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                </td>
                <td>
                    <asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="SchoolIDLabel" runat="server" DataSourceID="SqlDataSource2" DataValueField="SchoolID" DataTextField="Name" Text='<%# Eval("SchoolID") %>' Enabled="false" />
                </td>
                <td>
                    <asp:Label ID="ClassLabel" runat="server" Text='<%# Eval("Class") %>' />
                </td>
                <td>
                    <asp:Label ID="SpecializationLabel" runat="server" Text='<%# Eval("Specialization") %>' />
                </td>
                <td>
                    <asp:Label ID="TeacherCodeLabel" runat="server" Text='<%# Eval("TeacherCode") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="background-color: #FFCC66;color: #000080;">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                </td>
                <td>
                    <asp:Label ID="PartIDLabel1" runat="server" Text='<%# Eval("PartID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="ProjectIDTextBox1" runat="server" DataSourceID="SqlDataSource2" DataValueField="ProjectID" DataTextField="ProjName" Text='<%# Bind("ProjectID") %>' AutoPostBack="true" OnSelectedIndexChanged="ProjectIDTextBox1_SelectedIndexChanged" />
                </td>
                <td>
                    <asp:DropDownList ID="SchoolIDTextBox" AutoPostBack="true" runat="server" />
                </td>
                <td>
                    <asp:TextBox ID="ClassTextBox" runat="server" Text='<%# Bind("Class") %>' />
                </td>
                <td>
                    <asp:TextBox ID="SpecializationTextBox" runat="server" Text='<%# Bind("Specialization") %>' />
                </td>
                <td>
                    <asp:TextBox ID="TeacherCodeTextBox" runat="server" Text='<%# Bind("TeacherCode") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                <tr>
                    <td>No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                </td>
                <td>
                    <asp:TextBox ID="PartIDTextBox" runat="server" Text='<%# Bind("PartID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="ProjectIDTextBox1" runat="server" DataSourceID="SqlDataSource2" DataValueField="ProjectID" DataTextField="ProjName" Text='<%# Bind("ProjectID") %>' AutoPostBack="true" OnSelectedIndexChanged="ProjectIDTextBox1_SelectedIndexChanged" />
                </td>
                <td>
                    <asp:DropDownList ID="SchoolIDTextBox" AutoPostBack="true" runat="server" />
                </td>
                <td>
                    <asp:TextBox ID="ClassTextBox" runat="server" Text='<%# Bind("Class") %>' />
                </td>
                <td>
                    <asp:TextBox ID="SpecializationTextBox" runat="server" Text='<%# Bind("Specialization") %>' />
                </td>
                <td>
                    <asp:TextBox ID="TeacherCodeTextBox" runat="server" Text='<%# Bind("TeacherCode") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr style="background-color: #FFFBD6;color: #333333;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td>
                    <asp:Label ID="PartIDLabel" runat="server" Text='<%# Eval("PartID") %>' />
                </td>
                <td>
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                </td>
                <td>
                    <asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="SchoolIDLabel" runat="server" DataSourceID="SqlDataSource2" DataValueField="SchoolID" DataTextField="Name" Text='<%# Eval("SchoolID") %>' Enabled="false" />
                </td>
                <td>
                    <asp:Label ID="ClassLabel" runat="server" Text='<%# Eval("Class") %>' />
                </td>
                <td>
                    <asp:Label ID="SpecializationLabel" runat="server" Text='<%# Eval("Specialization") %>' />
                </td>
                <td>
                    <asp:Label ID="TeacherCodeLabel" runat="server" Text='<%# Eval("TeacherCode") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                            <tr runat="server" style="background-color: #FFFBD6;color: #333333;">
                                <th runat="server"></th>
                                <th runat="server">PartID</th>
                                <th runat="server">Name</th>
                                <th runat="server">ProjectID</th>
                                <th runat="server">SchoolID</th>
                                <th runat="server">Class</th>
                                <th runat="server">Specialization</th>
                                <th runat="server">TeacherCode</th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
                        <asp:DataPager ID="DataPager1" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="background-color: #FFCC66;font-weight: bold;color: #000080;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td>
                    <asp:Label ID="PartIDLabel" runat="server" Text='<%# Eval("PartID") %>' />
                </td>
                <td>
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                </td>
                <td>
                    <asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="SchoolIDLabel" runat="server" DataSourceID="SqlDataSource2" DataValueField="SchoolID" DataTextField="Name" Text='<%# Eval("SchoolID") %>' Enabled="false" />
                </td>
                <td>
                    <asp:Label ID="ClassLabel" runat="server" Text='<%# Eval("Class") %>' />
                </td>
                <td>
                    <asp:Label ID="SpecializationLabel" runat="server" Text='<%# Eval("Specialization") %>' />
                </td>
                <td>
                    <asp:Label ID="TeacherCodeLabel" runat="server" Text='<%# Eval("TeacherCode") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:TDCConnectionString2 %>" DeleteCommand="DELETE FROM [SWprjectPart] WHERE [PartID] = @original_PartID AND [ProjectID] = @original_ProjectID AND [Name] = @original_Name AND [SchoolID] = @original_SchoolID AND [Class] = @original_Class AND [Specialization] = @original_Specialization AND [TeacherCode] = @original_TeacherCode" InsertCommand="INSERT INTO [SWprjectPart] ([PartID], [Name], [ProjectID], [SchoolID], [Class], [Specialization], [TeacherCode]) VALUES (@PartID, @Name, @ProjectID, @SchoolID, @Class, @Specialization, @TeacherCode)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [SWprjectPart]" UpdateCommand="UPDATE [SWprjectPart] SET [Name] = @Name, [SchoolID] = @SchoolID, [Class] = @Class, [Specialization] = @Specialization, [TeacherCode] = @TeacherCode WHERE [PartID] = @original_PartID AND [ProjectID] = @original_ProjectID AND [Name] = @original_Name AND [SchoolID] = @original_SchoolID AND [Class] = @original_Class AND [Specialization] = @original_Specialization AND [TeacherCode] = @original_TeacherCode">
        <DeleteParameters>
            <asp:Parameter Name="original_PartID" Type="Int32" />
            <asp:Parameter Name="original_ProjectID" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_SchoolID" Type="Int32" />
            <asp:Parameter Name="original_Class" Type="String" />
            <asp:Parameter Name="original_Specialization" Type="String" />
            <asp:Parameter Name="original_TeacherCode" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="PartID" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="ProjectID" Type="Int32" />
            <%--<asp:ControlParameter Name="SchoolID" ControlId="ListView1$SchoolIDTextBox" PropertyName="SelectedValue" Type="String" />--%>
            <asp:Parameter Name="SchoolID" Type="Int32" />
            <asp:Parameter Name="Class" Type="String" />
            <asp:Parameter Name="Specialization" Type="String" />
            <asp:Parameter Name="TeacherCode" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="SchoolID" Type="Int32" />
            <asp:Parameter Name="Class" Type="String" />
            <asp:Parameter Name="Specialization" Type="String" />
            <asp:Parameter Name="TeacherCode" Type="Int32" />
            <asp:Parameter Name="original_PartID" Type="Int32" />
            <asp:Parameter Name="original_ProjectID" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_SchoolID" Type="Int32" />
            <asp:Parameter Name="original_Class" Type="String" />
            <asp:Parameter Name="original_Specialization" Type="String" />
            <asp:Parameter Name="original_TeacherCode" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>

这里是我的代码

protected void ProjectIDTextBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strConnection = null;
                    strConnection = "Data Source=DESKTOP-HJRRBDG;Initial Catalog=TDC;Integrated Security=True";
        
        DropDownList ddlListFind = (DropDownList)sender;
        ListViewItem item1 = (ListViewItem)ddlListFind.NamingContainer;

        DropDownList getDDLList = (DropDownList)item1.FindControl("ProjectIDTextBox1");

        DropDownList schoolbox = (DropDownList)item1.FindControl("SchoolIDTextBox");

        int ProjectID = Convert.ToInt32(getDDLList.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select distinct  * from SupervisionSoftware inner join Schools On SupervisionSoftware.SchoolID = Schools.SchoolID where ProjectID=" + ProjectID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
schoolbox.DataSource = ds;
schoolbox.DataTextField = "Name";
schoolbox.DataValueField = "SchoolID";
schoolbox.DataBind();
con.Close();

谁能帮帮我?

【问题讨论】:

    标签: c# sql asp.net sqlparameter


    【解决方案1】:

    请在此处分享您的错误,看起来您在插入时没有正确选择值。 最佳做法是每当出现此类问题时,在您选择或插入值之前使用调试器,这样您就可以确定是否正确获取值。

    【讨论】:

    • “/”应用程序中的服务器错误。无法将值 NULL 插入到列“SchoolID”、表“TDC.dbo.SWprjectPart”中;列不允许空值。 INSERT 失败。语句已终止。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.Data.SqlClient.SqlException:无法将值 NULL 插入列“SchoolID”、表“TDC.dbo.SWprjectPart”;列不允许空值。插入失败。
    • 错误清楚地表明您正在尝试在 SchoolID 中插入一个 NULL 值,这是您的 SQL 方案不允许的,因为您使用了一些约束,所以首先进入 SQL 服务器并尝试使用schoolID 中的 null 值如果仍然存在,则错误出现意味着您必须允许从 sql 端在 schoolID 中插入 null 但这里您的实际问题是您不是下拉列表中的正确选择值,这就是此错误上升的原因,因此请使用调试器和找出为什么 null 在下拉列表中选择。
    • 您能否分享您的下拉代码以从下拉列表中选择一个值。
    • 我没有任何其他代码,而不是之前在我的问题中发布过,我是一个初学者并尝试了许多解决方法代码但得到了相同的,所以请你帮我我该怎么办?
    • da.Fill(ds); // 在此处使用断点并检查您在此处填写的内容,如果您的数据集中没有 schoolID 这是主要问题,然后更正您的查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多