无法将值 NULL 插入到"SchoolID"列中,表 'TDC.dbo.SWprjectPart' 是依赖下拉列表



嗨,我希望我的问题解决了。。。我有一个包含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();

有人能帮我吗?

请在这里分享您的错误——在插入时似乎没有正确选择值。最佳做法是,每当出现这种类型的问题时,在选择或插入值之前使用调试器,以便确定是否正确获取值。

相关内容

最新更新