Reputation: 4864
We are trying to update data to the database from an ASP.Net DetailsView with a value from an ASP:DropDownList but when the user clicks the Update button nothing happens. Only the Cancel button will work.
The DropDownList will show the data from the database that was loaded into it but we can't do an Update.
I'm assuming I'm missing something from the markup coding.
Here's the markup for the DropDownList:
<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownListClass"
Runat="server"
DataSourceID="SqlDataSourceClasses"
DataTextField = "ClassName"
DataValueField="ID"
AutoPostBack="True"
AppendDataBoundItems="true">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEditDropDownListClass" runat="server"
ControlToValidate="DropDownListClass"
ErrorMessage="Please select a class." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="LiteralClass" runat="server"
Text='<%# FormatAsMixedCase(Eval("ClassName").ToString())%>' />
</ItemTemplate>
<ItemStyle ForeColor="Blue" />
</asp:TemplateField>
This is the markup of the DataSource which is a simple lookup table:
<asp:SqlDataSource
ID="SqlDataSourceClasses"
runat="server"
ConnectionString="<%$ ConnectionStrings:Islamic Knowledge Academy Staff System %>"
SelectCommand="SELECT [ID], [ClassName], [Grade] FROM [Classes]">
</asp:SqlDataSource>
Upvotes: 0
Views: 390
Reputation: 870
Emad-ud-den,
I think you need a click handler for your submit button so that when the client posts back to the server - it knows to update the database. Something along the lines of:
Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cnAD As New System.Data.SqlClient.SqlConnection = yourdatasource.getConnection("SqlDataSourceClasses", cnAD)
Dim cmd As New SqlCommand
cmd.Connection = cnAd
cmd.CommandType.Text
cmd.CommandText = "update yourtable set yourval=@val where yourparams"
cmd.Paramaters.AddWithValue("@val", DropDownListClass.SelectedValue)
cmd.ExecuteNonQuery()
End Sub
You could even add a try-catch to get a better idea as to where your code is going awry. Please note that what I've written will not work as is, but that it is intended to point you in the right direction.
Upvotes: 1