Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update/insert the status of one of the submitted suggestions in GridView by selecting a value from DropDownList?
    primarykey
    data
    text
    <p>I have two tables in my database as following:</p> <p>Suggestions Table: ID, Title, Description, StatusID... etc</p> <p>SuggestionsStatus Table: ID, Status</p> <p>I am using GridView to show the Suggestions and in the last column I put a DropDownList that for selecting the status for each Suggestion. Now, when I tried to update the Status of one of the submitted suggestions by selecting the status from the DropDownList, the value has been selected but it wasn't inserted to the Database (which means still I have NULL value in the StatusID column in the Suggestions Table) and I don't know why.</p> <p>My ASP.NET code:</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" width="950px" CssClass="mGrid" AlternatingRowStyle-CssClass="alt" RowStyle-HorizontalAlign="Center" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" &gt; &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt; &lt;HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /&gt; &lt;asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /&gt; &lt;asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /&gt; &lt;asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /&gt; &lt;asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" /&gt; &lt;asp:BoundField DataField="DivisionShortcut" HeaderText="Division" SortExpression="DivisionShortcut" /&gt; &lt;asp:TemplateField HeaderText="Status"&gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2" Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" DataTextField="Status" DataValueField="ID" AutoPostBack="true" OnDataBound="DropDownList_DataBound"&gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;" SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username, dbo.Divisions.DivisionShortcut FROM dbo.employee INNER JOIN dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode" FilterExpression="[DivisionShortcut] like '{0}%'"&gt; &lt;FilterParameters&gt; &lt;asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut" PropertyName="SelectedValue" Type="String" /&gt; &lt;/FilterParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;%--For the DropDownList--%&gt; &lt;asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;" SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"&gt;&lt;/asp:SqlDataSource&gt; </code></pre> <p>My code-behind:</p> <pre><code>protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddl = (DropDownList)sender; int suggestionStatus = int.Parse(ddl.SelectedValue); //For inserting the status in the database string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True"; string insertCommand = "INSERT INTO SafetySuggestionsLog (StatusID) values(@StatusID)"; using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(insertCommand, conn)) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@StatusID", suggestionStatus); cmd.ExecuteNonQuery(); } conn.Close(); } } </code></pre> <p>I modified the DropDownList_SelectedIndexChanged() as shown above but it gave me the following error:</p> <blockquote> <p>Cannot insert the value NULL into column 'Title', table 'psspdbTest.dbo.SafetySuggestionsLog'; column does not allow nulls. INSERT fails. The statement has been terminated.</p> </blockquote> <p><strong>So how I can fix this problem to be able to update the status of one of the submitted Suggestions in the (GridView) in the database?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload