在asp.net GridView的更新与SqlDataSource的 [英] Updating gridview with SqlDataSource in asp.net

查看:194
本文介绍了在asp.net GridView的更新与SqlDataSource的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用更新在GridView的记录的SqlDataSource ,这里是我在做什么。结果
下面是我的GridView标记

i want to update the records from the gridview using SqlDataSource, here is what i am doing.
below is my gridview markup

<asp:GridView ID="grdManageFaculties" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        DataSourceID="LocalServerDataSource" AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
        Width="100%" OnRowUpdating="grdManageFaculties_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="MANAGE">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
                    <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update"></asp:LinkButton>
                    <asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="NAME">
                <ItemTemplate>
                    <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblEditUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EMAIL">
                <ItemTemplate>
                    <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditEmail" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="MOBILE">
                <ItemTemplate>
                    <asp:Label ID="lblMobileNumber" runat="server" Text='<%# Eval("Mobile") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditMobileNumber" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LOCKED">
                <ItemTemplate>
                    <asp:CheckBox ID="chkIsLocked" runat="server" Enabled="false" Checked='<%# Eval("Locked") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="chkEditIsLocked" runat="server" Checked='<%# Bind("Locked") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CREATED">
                <ItemTemplate>
                    <asp:Label ID="lblCreated" runat="server" Text='<%# Eval("Created") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblEditCreated" runat="server" Text='<%# Eval("Created") %>'></asp:Label>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

下面是我在SqlDataSource

below is my markup for the SqlDataSource

<asp:SqlDataSource ID="LocalServerDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>"
        SelectCommand="users_GetAllUsers" SelectCommandType="StoredProcedure" UpdateCommand="users_UpdateFaculty" UpdateCommandType="StoredProcedure">
        <UpdateParameters>
            <asp:Parameter Name="EMAIL" Type="String" />
            <asp:Parameter Name="ISLOCKEDOUT" Type="Boolean" />
            <asp:Parameter Name="MOBILENUMBER" Type="Int64" />
            <asp:Parameter Name="USERNAME" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

下面是我的code-背后的Row_Updating功能

below is my code-behind for the Row_Updating function

   protected void grdManageFaculties_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            TextBox email = grdManageFaculties.Rows[e.RowIndex].FindControl("txtEditEmail") as TextBox;
            Label username = grdManageFaculties.Rows[e.RowIndex].FindControl("lblEditUserName") as Label;
            CheckBox locked = grdManageFaculties.Rows[e.RowIndex].FindControl("chkEditIsLocked") as CheckBox;
            TextBox mobilenumber = grdManageFaculties.Rows[e.RowIndex].FindControl("txtEditMobileNumber") as TextBox;

            LocalServerDataSource.UpdateParameters["EMAIL"].DefaultValue = email.Text;
            LocalServerDataSource.UpdateParameters["ISLOCKEDOUT"].DefaultValue = locked.Checked.ToString();
            LocalServerDataSource.UpdateParameters["MOBILENUMBER"].DefaultValue = mobilenumber.Text;
            LocalServerDataSource.UpdateParameters["USERNAME"].DefaultValue = username.Text;
            LocalServerDataSource.Update();
        }
        catch { }
    }

下面

我的存储过程更新

below is my Stored Procedure for Update

ALTER PROCEDURE users_UpdateFaculty
    @EMAIL NVARCHAR(100),
    @ISLOCKEDOUT BIT,
    @MOBILENUMBER BIGINT,
    @USERNAME nvarchar(100)
AS
BEGIN
    UPDATE aspnet_Users SET MOBILENUMBER=@MOBILENUMBER where USERNAME=@USERNAME
    UPDATE ASPNET_MEMBERSHIP SET EMAIL = @EMAIL, LOWEREDEMAIL = LOWER(@EMAIL), ISLOCKEDOUT=@ISLOCKEDOUT WHERE USERID = (SELECT USERID FROM ASPNET_USERS WHERE USERNAME=@USERNAME)
END

我在数据库中的记录是得到更新,但是当我在更新按钮点击,我得到以下错误:

my records in database is getting updated, but when i click on update button, i gets the below error:

Procedure or function users_UpdateFaculty has too many arguments specified.

谁能帮我什么可以,我用正确的所有参数会造成这个问题。

Can anyone help me what could be causing this issue, i am using all the parameters properly.

推荐答案

找到了解决办法:
选择列和参数更新应该匹配才能使用SqlDataSource的更新,这意味着如果你选择(查询或过程)将返回在GridView的3个字段,然后所有的人都应该是用于更新的参数,你可以错过了在数据库中的实际updations如果不是必需的,但&LT; UpdateParameters&GT; 应具备的所有字段:比方说,如果下面是我选择查询

Found the solution: The Select Columns and the Update Parameters should match in order to update using SqlDataSource, that means if you select(query or procedure) is returning 3 fields in gridview, then all of them should be the parameter for the update, you can miss out the actual updations in database if not required, but the <UpdateParameters> should have all the fields: say for example if below is my select query

SELECT USERNAME, MOBILENUMBER, EMAIL FROM USERS

则更新参数应

<UpdateParameters>
   <asp:Parameter Name="UserName" Type="String" />
   <asp:Parameter Name="MobileNumber" Type="Int64" />
   <asp:Parameter Name="Email" Type="String" />
<UpdateParameters>

您不能跳过任何参数,即使你不打算更新场
希望这会帮助别人,因为我浪费了很多时间来研究这一点。

you cannot skip any of the parameters, even though you do not intend to update that field Hope this will help others, as i wasted lots of time researching on this.

这篇关于在asp.net GridView的更新与SqlDataSource的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆