简体   繁体   中英

How to parameterize the Update query in SqlDataSource

在此处输入图片说明

I am maintaining an old website, the code was pretty messy.

I want to use SqlDataSource to do the Updating, selecting, inserting functions. I really do not want to write the back-end code by myself. (I actually have already written all code, but the old code is too messy to test and does not have comments so it makes me very hard to detect where bugs are, so I have discarded the back-end code and am using SqlDataSource)

Instead, I'd like to use the UpdateCommand in SqlDataSource do to the update. Here is my Updating query:

UPDATE MedicareLocalAccounts SET Account=@Account and PWD=@PWD WHERE DivisionID=@DivisionID

The data source comes from 3 different tables.

 <asp:GridView ID="gv_MediCareLocals" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataKeyNames="LUDivisionsUID" ClientIDMode="Static" AutoGenerateEditButton="True"
            EnablePersistedSelection="True" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:HyperLinkField DataTextField="LUDivisionsUID" HeaderText="Division ID" ShowHeader="true"
                    SortExpression="LUDivisionsUID" />
                <asp:BoundField DataField="Division" HeaderText="Medicare Local" ReadOnly="true" SortExpression="Division"/>
                <asp:BoundField DataField="States" HeaderText="State" ReadOnly="true" SortExpression="States" />

                <asp:TemplateField HeaderText="Account">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Account") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <input type="text" clientidmode="Static" runat="server" id="tbAccount" value='<%# Eval("Account") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Password">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("PWD") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <input type="text" clientidmode="Static" runat="server" id="tbPassword" value='<%# Eval("PWD") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

So as you can see, I need to have the DivisionID parameterized as well, but the DivisionID field does not have an ID (not like the Account and Password). So How can I write the where clause in order to find the DivisionID in the current row?

Update: I have changed the DivisionID column to

  <asp:TemplateField HeaderText="DivisionID" SortExpression="LUDivisionsUID" >
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblDivisionID" Text='<%# Eval("LUDivisionsUID") %>'>' ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

But I got an error after I clicked the Update button.

Could not find control 'tbAccount' in ControlParameter 'Account'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Could not find control 'tbAccount' in ControlParameter 'Account'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: Could not find control 'tbAccount' in ControlParameter 'Account'.]
System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext context, Control control) +2155166
System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) +50
System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control control) +101
System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +36
System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +257 System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +222
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1210
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642898 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

As you can see from the code, I have already applied the property ClientIDMode="Static" on both GridView and columns. I use Chrome to see the generated ids for the Account column and found the id=tbAccount, but the name="ctl00$PageTitlePlaceHolder$gv_MediCareLocals$ctl02$tbAccount"

I don't think the Sql UpdateParameter is going to use the name property anyway.

If u create a stored procedure then it will be easy.For that purpose visit this Link

Hope it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM