简体   繁体   English

如何在SqlDataSource中参数化Update查询

[英]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. 我想使用SqlDataSource进行更新,选择,插入功能。 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) (我实际上已经编写了所有代码,但是旧代码太乱了,无法测试并且没有注释,因此这使我很难检测到错误所在,因此我放弃了后端代码并使用SqlDataSource)

Instead, I'd like to use the UpdateCommand in SqlDataSource do to the update. 相反,我想使用SqlDataSource中的UpdateCommand来进行更新。 Here is my Updating query: 这是我的更新查询:

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

The data source comes from 3 different tables. 数据源来自3个不同的表。

 <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). 如您所见,我还需要对DivisionID进行参数化,但是DivisionID字段没有ID(与帐户和密码不同)。 So How can I write the where clause in order to find the DivisionID in the current row? 那么,如何编写where子句才能在当前行中找到DivisionID?

Update: I have changed the DivisionID column to 更新:我已将DivisionID列更改为

  <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'. 在ControlParameter'Account'中找不到控件'tbAccount'。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 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'. 异常详细信息:System.InvalidOperationException:在ControlParameter“帐户”中找不到控件“ tbAccount”。

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 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'.] [InvalidOperationException:在ControlParameter'Account'中找不到控件'tbAccount'。]
System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext context, Control control) +2155166 System.Web.UI.WebControls.ControlParameter.Evaluate(HttpContext上下文,控制控件)+2155166
System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) +50 System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext上下文,控制控件)+50
System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control control) +101 System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext上下文,控制控件)+101
System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +36 System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext上下文,控制控件)+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.WebControls.SqlDataSourceView.InitializeParameters(DbCommand命令,ParameterCollection参数,IDictionary排除列表)+257 System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary密钥,IDictionary值,IDictionary oldValues)+222
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87 System.Web.UI.DataSourceView.Update(IDictionary键,IDictionary值,IDictionary oldValues,DataSourceViewOperationCallback回调)+87
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1210 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow行,Int32 rowIndex,布尔原因验证)+1210
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e,布尔causeValidation,字符串验证组)+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.GridView.OnBubbleEvent(对象来源,EventArgs e)+89 System.Web.UI.Control.RaiseBubbleEvent(对象来源,EventArgs参数)+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.GridViewRow.OnBubbleEvent(对象源,EventArgs e)+88 System.Web.UI.Control.RaiseBubbleEvent(对象源,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.LinkBut​​ton.OnCommand(CommandEventArgs e)+121 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument)+156
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 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 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. 从代码中可以看到,我已经在GridView和column上应用了属性ClientIDMode =“ Static”。 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" 我使用Chrome浏览器为“帐户”列查看生成的ID,并找到了ID = tbAccount,但名称为“ ctl00 $ PageTitlePlaceHolder $ gv_MediCareLocals $ ctl02 $ tbAccount”

I don't think the Sql UpdateParameter is going to use the name property anyway. 我不认为Sql UpdateParameter无论如何都不会使用name属性。

If u create a stored procedure then it will be easy.For that purpose visit this Link 如果您创建一个存储过程将很容易。为此,请访问此链接

Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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