locked
DetailsView Update Issue!! RRS feed

  • Question

  • User1112626631 posted

    Hi all,

    I have a simple aspx form contains 1 simple SqlDataSource bounded to simple Table and 1 DetailsView bounded to the SqlDataSource, i have converted 1 field of the DetailsView to TemplateField as a DropDownList with ListItems.

    After i have inserted some records to the table i ran my aspx form, the DetailsView appeared with Edit, Delete Links, till now no problems.

    But if i click Edit link and changed the value of the TemplateField and clicked update old value gone and the new value does not applied to the datasource!! (i have no code behinde).

    How to apply changes made by DropDownList to the datasource?

    Screenshot1: Show the current value.

    http://img71.imageshack.us/img71/7217/46160407xy8.jpg

    Screenshot2: Shows TemplateField DropDown values.

    http://img71.imageshack.us/img71/5897/63150374tw3.jpg

    Screenshot3: Shows that the value has gone!!

    http://img49.imageshack.us/img49/3011/51324406os5.jpg

    Regards

    Monday, June 2, 2008 1:01 PM

All replies

  • User-1995538749 posted

    You need to make sure you Bind the SelectedValue of the DropDownList to the proper database field. Here's an example. Note the DropDownList within the EditTemplate.

    ASPX 

    <%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
    	codefile="CRUDWithSqlDataSourceAndForeignKeys.aspx.cs" inherits="DetailsView_CRUDWithSqlDataSourceAndForeignKeys"
    	title="Untitled Page" %>
    
    <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    	<asp:label id="lblMessage" runat="server" enableviewstate="false" />
    	<asp:detailsview id="dvProducts" runat="server" allowpaging="True" autogeneratedeletebutton="True"
    		autogenerateeditbutton="True" autogenerateinsertbutton="True" autogeneraterows="False"
    		datakeynames="ProductID" datasourceid="sdsProducts" style="margin-top: 20px;" 
    		onitemdeleted="dvProducts_ItemDeleted" onitemupdated="dvProducts_ItemUpdated" 
    		onpageindexchanging="dvProducts_PageIndexChanging">
    		<fields>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    			<asp:templatefield headertext="SupplierID" sortexpression="SupplierID">
    				<itemtemplate>
    					<%# Eval("CompanyName") %>
    				</itemtemplate>
    				<edititemtemplate>
    					<asp:dropdownlist id="ddlSuppliers" runat="server" appenddatabounditems="true" datasourceid="sdsSuppliers"
    						datatextfield="CompanyName" datavaluefield="SupplierID" selectedvalue='<%# Bind("SupplierID") %>'>
    						<asp:listitem text="" value="" />
    					</asp:dropdownlist>
    					<asp:sqldatasource id="sdsSuppliers" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    						selectcommand="SELECT * FROM [Suppliers] ORDER BY [CompanyName]"></asp:sqldatasource>
    				</edititemtemplate>
    				<insertitemtemplate>
    					<asp:dropdownlist id="ddlSuppliers" runat="server" appenddatabounditems="true" datasourceid="sdsSuppliers"
    						datatextfield="CompanyName" datavaluefield="SupplierID" selectedvalue='<%# Bind("SupplierID") %>'>
    						<asp:listitem text="" value="" />
    					</asp:dropdownlist>
    					<asp:sqldatasource id="sdsSuppliers" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    						selectcommand="SELECT * FROM [Suppliers] ORDER BY [CompanyName]">
    					</asp:sqldatasource>
    				</insertitemtemplate>
    			</asp:templatefield>
    			<asp:templatefield headertext="CategoryID" sortexpression="CategoryID">
    				<itemtemplate>
    					<%# Eval("CategoryName") %>
    				</itemtemplate>
    				<edititemtemplate>
    					<asp:dropdownlist id="ddlCategories" runat="server" appenddatabounditems="true" datasourceid="sdsCategories"
    						datatextfield="CategoryName" datavaluefield="CategoryID" 
    						selectedvalue='<%# Bind("CategoryID") %>'>
    						<asp:listitem text="" value="" />
    					</asp:dropdownlist>
    					<asp:sqldatasource id="sdsCategories" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    						selectcommand="SELECT * FROM [Categories] ORDER BY [CategoryName]">
    					</asp:sqldatasource>
    				</edititemtemplate>
    				<insertitemtemplate>
    					<asp:dropdownlist id="ddlCategories" runat="server" appenddatabounditems="True" datasourceid="sdsCategories"
    						datatextfield="CategoryName" datavaluefield="CategoryID" selectedvalue='<%# Bind("CategoryID") %>'>
    						<asp:listitem text="" value="" />
    					</asp:dropdownlist>
    					<asp:sqldatasource id="sdsCategories" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    						selectcommand="SELECT * FROM [Categories] ORDER BY [CategoryName]">
    					</asp:sqldatasource>
    				</insertitemtemplate>
    			</asp:templatefield>
    			<asp:checkboxfield datafield="Discontinued" headertext="Discontinued" sortexpression="Discontinued" />
    		</fields>
    	</asp:detailsview>
    	<asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
    		deletecommand="DELETE FROM Products WHERE (ProductID = @ProductID)" insertcommand="INSERT INTO Products (ProductName, SupplierID, CategoryID, Discontinued) VALUES (@ProductName,@SupplierID,@CategoryID,@Discontinued); SELECT @ProductID = SCOPE_IDENTITY();"
    		selectcommand="SELECT Products.ProductID, Products.ProductName, Products.SupplierID, Products.CategoryID, Categories.CategoryName, Suppliers.CompanyName, Products.Discontinued FROM Products LEFT OUTER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID LEFT OUTER JOIN Categories ON Products.CategoryID = Categories.CategoryID ORDER BY Products.ProductName"
    		updatecommand="UPDATE Products SET ProductName = @ProductName, SupplierID = @SupplierID, CategoryID = @CategoryID, Discontinued = @Discontinued WHERE (ProductID = @ProductID)"
    		oninserted="sdsProducts_Inserted">
    		<updateparameters>
    			<asp:parameter name="ProductName" />
    			<asp:parameter name="SupplierID" />
    			<asp:parameter name="CategoryID" />
    			<asp:parameter name="Discontinued" />
    			<asp:parameter name="ProductID" />
    		</updateparameters>
    		<insertparameters>
    			<asp:parameter name="ProductName" />
    			<asp:parameter name="SupplierID" />
    			<asp:parameter name="CategoryID" />
    			<asp:parameter name="Discontinued" />
    			<asp:parameter direction="Output" name="ProductID" type="Int32" />
    		</insertparameters>
    		<deleteparameters>
    			<asp:parameter name="ProductID" />
    		</deleteparameters>
    	</asp:sqldatasource>
    </asp:content>
    

    CODE-BEHIND 

    using System;
    using System.Web.UI.WebControls;
    
    public partial class DetailsView_CRUDWithSqlDataSourceAndForeignKeys : System.Web.UI.Page
    {
    	protected void dvProducts_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    	{
    		dvProducts.ChangeMode(DetailsViewMode.ReadOnly);
    	}
    
    	protected void dvProducts_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    	{
    		if ((e.Exception == null) && e.AffectedRows.Equals(1))
    		{
    			lblMessage.Text = String.Format(
    				"Product '{0}' successfully updated.",
    				e.Keys["ProductID"]);
    		}
    		else
    		{
    			lblMessage.Text = "Unable to successfully update product.";
    			e.ExceptionHandled = true;
    		}
    	}
    
    	protected void dvProducts_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    	{
    		if ((e.Exception == null) && e.AffectedRows.Equals(1))
    		{
    			lblMessage.Text = String.Format(
    				"Product '{0}' successfully deleted.",
    				e.Keys["ProductID"]);
    		}
    		else
    		{
    			lblMessage.Text = "Unable to successfully delete product.";
    			e.ExceptionHandled = true;
    		}
    	}
    
    	protected void sdsProducts_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    	{
    		if ((e.Exception == null) && e.AffectedRows.Equals(1))
    		{
    			lblMessage.Text = String.Format(
    				"Product '{0}' successfully added.",
    				e.Command.Parameters["@ProductID"].Value);
    		}
    		else
    		{
    			lblMessage.Text = "Unable to successfully add product.";
    			e.ExceptionHandled = true;
    		}
    	}
    }
    Monday, June 2, 2008 1:21 PM
  • User1112626631 posted

    Strange..!! no "SelectedValue" found by intellisence and it cause error if i wrote it with my own!!

    http://img249.imageshack.us/img249/3668/dvuijy3.jpg

    I`m runing ASP.NET 3.5

    Monday, June 2, 2008 2:06 PM
  • User-1995538749 posted

    You're correct, the SeelctedValue doesn't appear as a property of the DropDownList within the DropDownList control, but believe me in that this works. What is causing the error?

    Monday, June 2, 2008 2:10 PM
  • User1112626631 posted

    Error appears like that when debuging disabled by IE 7

     http://img170.imageshack.us/img170/2337/ederrcf5.jpg

    And it is when debuging enabled

     http://img159.imageshack.us/img159/333/ederr2ot6.jpg

    I`m sure this field already exist, dropdown appears with values when in edit!!

    Monday, June 2, 2008 2:29 PM
  • User-1995538749 posted

    Is the "CategoryName" field from my example? If so, I need to see the code for your DropDownList. It appears as if you have the DataValueField or DataTextField properties of your DropDownList messed up. The error is telling you that the "CategoryName" field does not exist within the data that is being bound to your DropDownList.

    Monday, June 2, 2008 2:40 PM
  • User1112626631 posted

    No i have changed the field name according to mine.

    I had uploaded the project and sent you link by mail.

     Thanks so much for help :)

    Monday, June 2, 2008 3:01 PM
  • User-1598387078 posted

    Guys any update on this..Even i am stuck with same problem.. [:(] 

    Thursday, July 24, 2008 5:20 PM