Prevent overlapping edits. : DropDownList « Data Binding « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MyPage" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Record Editor</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="lstProduct" 
                          runat="server" 
                          AutoPostBack="True" 
                          Width="300px" 
                          DataSourceID="sourceProducts" 
                          DataTextField="ProductName" 
                          DataValueField="ProductID">
        </asp:DropDownList>
        <br />
        <table>
            <tr>
                <td>
                    <asp:DetailsView ID="DetailsView1" 
                                     runat="server" 
                                     DataSourceID="sourceProductDetails"
                                     Height="50px" 
                                     Width="200px" 
                                     AutoGenerateEditButton="True">
                    </asp:DetailsView>
                </td>
                <td style="width: 190px">
                </td>
            </tr>
        </table>
        <asp:SqlDataSource ID="sourceProducts" 
                           runat="server"
                           ProviderName="System.Data.SqlClient"
                           ConnectionString="<%$ ConnectionStrings:Northwind %>"
                           SelectCommand="SELECT ProductName, ProductID FROM Products"/>
         <asp:SqlDataSource ID="sourceProductDetails" 
                            runat="server"
                            ProviderName="System.Data.SqlClient"
                            ConnectionString="<%$ ConnectionStrings:Northwind %>"
                            SelectCommand="SELECT ProductID, ProductName, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE ProductID=@ProductID"
                            UpdateCommand="UPDATE Products SET ProductName=@ProductName, UnitPrice=@UnitPrice, UnitsInStock=@UnitsInStock, UnitsOnOrder=@UnitsOnOrder, ReorderLevel=@ReorderLevel, Discontinued=@Discontinued WHERE ProductID=@ProductID AND ProductName=@original_ProductName AND UnitPrice=@original_UnitPrice AND UnitsInStock=@original_UnitsInStock AND UnitsOnOrder=@original_UnitsOnOrder AND ReorderLevel=@original_ReorderLevel AND Discontinued=@original_Discontinued"
                            OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues" OnUpdated="sourceProductDetails_Updated">
           <SelectParameters>
              <asp:ControlParameter ControlID="lstProduct" Name="ProductID" PropertyName="SelectedValue" />
           </SelectParameters>
        </asp:SqlDataSource>
        <asp:Label ID="lblInfo" runat="server" EnableViewState="False"></asp:Label>
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MyPage : System.Web.UI.Page
{
    protected void sourceProductDetails_Updated(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows == 0)
        {
            lblInfo.Text = "No update was performed. A concurrency error is likely, or the command is incorrectly written.";
        }
        else
        {
            lblInfo.Text = "Record successfully updated.";
        }
    }
}


File: Web.config


<?xml version="1.0"?>

<configuration>

  <connectionStrings>
    <add name="Northwind" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"/>
  </connectionStrings>
</configuration>








19.13.DropDownList
19.13.1.Binding to a Data Source
19.13.2.Determining the Selected List Item: SelectedIndex, SelectedItem, SelectedValue
19.13.3.Master/Details form with a list control
19.13.4.Enabling Automatic PostBacks
19.13.5.Table record editor by DropDownList and ListBox
19.13.6.Link DropDownList with SqlDataSource and do the editing
19.13.7.Prevent overlapping edits.
19.13.8.Binding ArrayList to asp:DropDownList
19.13.9.Data binding by filling list and drop-down list controls with the results of direct ADO.NET queries
19.13.10.Repeater and DropDownList