0

Hi I hope got my problem solved ... I have a listview which contains 2 dropdownlists, it works well in show data but when insert button clicked I got error of can't insert null values so it couldn't read my selected value of the second dropdownlist

Here is my html code

  <%@ 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>

and here my code behind

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();

please could anyone help me ?

Shery
  • 1
  • 3

1 Answers1

0

Kindly share your error here it looks like you are not properly select values when going to insert. The best practice is whenever this type of issue rises use a debugger before you are going to select or insert value so you are sure about fetching values proper or not.

  • Server Error in '/' Application. Cannot insert the value NULL into column 'SchoolID', table 'TDC.dbo.SWprjectPart'; column does not allow nulls. INSERT fails.The statement has been terminated. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'SchoolID', table 'TDC.dbo.SWprjectPart'; column does not allow nulls. INSERT fails. – Shery Sep 21 '20 at 09:37
  • the error clearly saying you are trying to insert a NULL value in SchoolID which is not allowed by your SQL scheme because there you use some constraints so first go in SQL server and try to insert same data with a null value in schoolID if still, the error comes means you have to allowed from sql side to insert null in schoolID but here your actual problem is you are not the proper select value from the dropdown that's why this error is rising so use a debugger and find why null is selecting in dropdown. – owais shahab Sep 21 '20 at 09:58
  • Can you share your dropdown code for selecting a value from the dropdown. – owais shahab Sep 21 '20 at 10:00
  • I don't have any other code rather than published before in my question, I'm a beginner and tried many workarounds codes but got the same so could you please help me what should I do? – Shery Sep 21 '20 at 10:11
  • da.Fill(ds); // use break point here and check what you are filling here if there is no schoolID in your dataset that's the main issue then correct your query. – owais shahab Sep 21 '20 at 10:29
  • of course it pass null values while the dropdownlist is filled correctly ... I don't know what's going on – Shery Sep 21 '20 at 10:45
  • okay the problem here is your name is binding correctly but schoolID is not binding so you need to check all place you are properly using SchoolID because if any place there is a spelling issue this will not bind correctly. – owais shahab Sep 21 '20 at 11:15
  • select distinct * from SupervisionSoftware inner join Schools On SupervisionSoftware.SchoolID = Schools.SchoolID where ProjectID=" + ProjectID, do you checks this query giving you proper result in SQL server because if you are fetching null values of schoolID so you need to correct your query first. – owais shahab Sep 21 '20 at 11:18
  • it works good on sql and executed in right way, also on the web page it fills the 2nd dropdownlist with the correct data – Shery Sep 21 '20 at 11:32
  • is there any help please? – Shery Sep 22 '20 at 06:51
  • bro if this is a practice project you can share the repository with us because I cannot able to understand why this issue is happening until I check and debug your code. – owais shahab Sep 22 '20 at 08:44
  • could I send the project with sql script or what ? – Shery Sep 22 '20 at 09:38
  • you can share in zip file owaisshahab@gmail.com or better option is upload on github give me url here – owais shahab Sep 22 '20 at 10:22
  • https://github.com/suny100/WebApp.git hope it uploaded good – Shery Sep 22 '20 at 11:02