none
修改SqlDataSource的selectcommand导致更新失灵,求解 RRS feed

  • 问题

  • 大家好,现在疑难是点击更新后,修改后的值无法被更新到数据库

    具体见下图,请高手们指点一下

    下面是代码

    前台

    <%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="test._Default" %>
    
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h2>
            欢迎使用 ASP.NET!
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        </h2>
        
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                DataSourceID="SqlDataSource2" DataTextField="1" DataValueField="1" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>" 
                SelectCommand="SELECT * FROM [Table_2]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                AutoGenerateColumns="False" CellPadding="4" DataKeyNames="4" 
                DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="1" HeaderText="1" SortExpression="1" />
            <asp:BoundField DataField="2" HeaderText="2" SortExpression="2" />
            <asp:BoundField DataField="3" HeaderText="3" SortExpression="3" />
            <asp:BoundField DataField="4" HeaderText="4" ReadOnly="True" 
                SortExpression="4" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConflictDetection="CompareAllValues" 
                ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>" 
                DeleteCommand="DELETE FROM [Table_2] WHERE [4] = @original_column1 AND (([1] = @original_column2) OR ([1] IS NULL AND @original_column2 IS NULL)) AND (([2] = @original_column3) OR ([2] IS NULL AND @original_column3 IS NULL)) AND (([3] = @original_column4) OR ([3] IS NULL AND @original_column4 IS NULL))" 
                InsertCommand="INSERT INTO [Table_2] ([1], [2], [3], [4]) VALUES (@column1, @column2, @column3, @column4)" 
                OldValuesParameterFormatString="original_{0}" onload="SqlDataSource1_Load" 
                SelectCommand="SELECT * FROM [Table_2]" 
                UpdateCommand="UPDATE [Table_2] SET [1] = @column1, [2] = @column2, [3] = @column3 WHERE [4] = @original_column1 AND (([1] = @original_column2) OR ([1] IS NULL AND @original_column2 IS NULL)) AND (([2] = @original_column3) OR ([2] IS NULL AND @original_column3 IS NULL)) AND (([3] = @original_column4) OR ([3] IS NULL AND @original_column4 IS NULL))">
                <DeleteParameters>
                    <asp:Parameter Name="original_column1" Type="Int32" />
                    <asp:Parameter Name="original_column2" Type="String" />
                    <asp:Parameter Name="original_column3" Type="String" />
                    <asp:Parameter Name="original_column4" Type="String" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="column1" Type="String" />
                    <asp:Parameter Name="column2" Type="String" />
                    <asp:Parameter Name="column3" Type="String" />
                    <asp:Parameter Name="column4" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="column1" Type="String" />
                    <asp:Parameter Name="column2" Type="String" />
                    <asp:Parameter Name="column3" Type="String" />
                    <asp:Parameter Name="original_column1" Type="Int32" />
                    <asp:Parameter Name="original_column2" Type="String" />
                    <asp:Parameter Name="original_column3" Type="String" />
                    <asp:Parameter Name="original_column4" Type="String" />
                </UpdateParameters>
            </asp:SqlDataSource>
        </ContentTemplate>
        </asp:UpdatePanel>
        <p>
            若要了解关于 ASP.NET 的详细信息,请访问 <a href="http://www.asp.net/cn" title="ASP.NET 网站">www.asp.net/cn</a>。
        </p>
        <p>
            您还可以找到 <a href="http://go.microsoft.com/fwlink/?LinkID=152368"
                title="MSDN ASP.NET 文档">MSDN 上有关 ASP.NET 的文档</a>。
        </p>
    </asp:Content>
    

    后台

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace test
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
              
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                SqlDataSource1.SelectCommand = "SELECT * FROM [Table_2] where [1]='" + DropDownList1.SelectedItem.ToString() + "'";
                Session["SelectCommand"] = SqlDataSource1.SelectCommand.ToString();
            }
    
            protected void SqlDataSource1_Load(object sender, EventArgs e)
            {
                if (Session["SelectCommand"] != null)
                {
                    SqlDataSource1.SelectCommand = Session["SelectCommand"].ToString();
                }
            }
        }
    }
    

    2013年1月12日 12:41

全部回复