Problem updating DetailsView when added DropDownList
-
Hi I am struggling with updating data from a web page. On the page I have a DetailsView control which is bound to a table in the SQL Server database. I have enabled Editing. One of the fields in the table is a foreign key to a lookup table (for the Referring Hospital) in the database. I would like to have a DropdownList in the DetailsView so that the user can choose the item. I have changed the field to a TemplateField and while the field (“HospID”) is still a TextBox the update to database works. For example when I change the LastName and click ‘Update’, the update reflects in the database. Here is the markup:
<asp:TemplateField HeaderText="HospID" SortExpression="HospID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />However when I replace the TextBox in the EditTemplate with a DropdownList and try to update, the changes do not reflect in the database. Even if I have just changed the LastName. The markup for the DropdownList is as follows:
<asp:TemplateField HeaderText="HospID" SortExpression="HospID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource_RefHosp" DataTextField="HospShort"
DataValueField="HospID">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>Even if I just use a basic DropdownList with no databinding the update does not work.
<asp:DropDownList ID="DropDownList_RefHosp" runat="server"
DataTextField="HospShort" DataValueField= -
Hi I am struggling with updating data from a web page. On the page I have a DetailsView control which is bound to a table in the SQL Server database. I have enabled Editing. One of the fields in the table is a foreign key to a lookup table (for the Referring Hospital) in the database. I would like to have a DropdownList in the DetailsView so that the user can choose the item. I have changed the field to a TemplateField and while the field (“HospID”) is still a TextBox the update to database works. For example when I change the LastName and click ‘Update’, the update reflects in the database. Here is the markup:
<asp:TemplateField HeaderText="HospID" SortExpression="HospID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />However when I replace the TextBox in the EditTemplate with a DropdownList and try to update, the changes do not reflect in the database. Even if I have just changed the LastName. The markup for the DropdownList is as follows:
<asp:TemplateField HeaderText="HospID" SortExpression="HospID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource_RefHosp" DataTextField="HospShort"
DataValueField="HospID">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HospID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HospID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>Even if I just use a basic DropdownList with no databinding the update does not work.
<asp:DropDownList ID="DropDownList_RefHosp" runat="server"
DataTextField="HospShort" DataValueField=You are connected the Dropdownlist by automatically by Database connection wizard. When program starts running the values in Database which you specified is loaded Dropdownlist. Again it will not goto database to retive the values for dropdownlist. So you can't get updated values. Instaed of that you will write the code in separate method RetriveData() to retive the data from database and add the SqlDataReader values to Dropdownlist. Call thet method when page load and update button click methods . Take another connection object to for RetriveData() method Open the connection when RetriveData() is called and Close at the method end. U will get updated list evrytime. Hope you can do the coding simple try it..