简体   繁体   English

GridView更新按钮执行更新SQL Server存储过程

[英]GridView Update button execute update SQL Server stored procedure

在此处输入图片说明

SQL Server stored procedure accepts the parameters, current company name, new company name and whether it already exists which has a default value. SQL Server存储过程接受参数,当前公司名称,新公司名称以及是否已存在(具有默认值)。 When the edit button is clicked on the front end UI, the grid view allows me to edit the company name. 在前端用户界面上单击“编辑”按钮时,网格视图允许我编辑公司名称。 This shows an 'Update' button - when this is clicked - the code parses however nothing updates and company name does not update either. 这将显示一个“更新”按钮-单击此按钮时,代码将进行解析,但是没有更新,公司名称也没有更新。

Breakpoint is set and stepped through and @CurrentCompanyName is returned as null. 设置断点并逐步执行,并且@CurrentCompanyName返回为null。 Not sure how to fix this. 不确定如何解决此问题。

Aspx: ASPX:

<asp:GridView ID="CompanyTable" runat="server" 
     OnRowEditing="CompanyTable_RowEditing" 
     OnRowCancelingEdit="CompanyTable_RowCancelingEdit" 
     OnRowUpdating="CompanyTable_RowUpdating" 
     OnPageIndexChanging="CompanyTable_PageIndexChanging" 
     PageSize="20" Font-Underline="False" AllowPaging="True">
    <HeaderStyle Width="150px" />
    <Columns>
        <asp:TemplateField>
            <HeaderStyle Width="200px" />
            <ControlStyle CssClass="ButtonDesigntwo" />
            <ItemTemplate>
                <asp:LinkButton ID="Edit" ButtonType="Button" runat="server" CommandName="Edit" Text="Edit" />
                <asp:LinkButton ID="Delete" ButtonType="Button" runat="server" CommandName="Delete" Text="Delete" />
            </ItemTemplate>
            <EditItemTemplate>  
                <asp:Button ID="Update" ButtonType="Button" runat="server" Text="Update" CommandName="Update"/>  
                <asp:Button ID="Cancel" ButtonType="Button" runat="server" Text="Cancel" CommandName="Cancel"/>  
            </EditItemTemplate>   
        </asp:TemplateField>
    </Columns>
    <HeaderStyle CssClass="tableHeaderStyle" />
    <PagerSettings Mode="NumericFirstLast" />
    <PagerStyle CssClass="pager" BorderColor="Black" ForeColor="White" Font-Underline="False" />
    <RowStyle CssClass="tableRowStyle" />
</asp:GridView>

Method code: 方法代码:

 protected void CompanyTable_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
 {
        string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;

        SqlConnection cn = new SqlConnection(connectionString);

        using (SqlCommand cmd = new SqlCommand("[updateCompanyName]", cn))
        {
            TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@CurrentCompanyName", name);
            cmd.Parameters.AddWithValue("@NewCompanyName", CompanyInputTextBox.Text).Direction = ParameterDirection.Input;

            SqlParameter objisExists = new SqlParameter("@isExists", SqlDbType.Int);
            objisExists.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(objisExists);

            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();

            int isExists = Convert.ToInt32(cmd.Parameters["@isExists"].Value.ToString());

            if (isExists == 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "111", "AddCompanyUpdating();", true);
            }
            else if (isExists == 1)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "111", "CompanyNotUpdatedValidation();", true);
            }
        }

        // Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
        CompanyTable.EditIndex = -1;

        // Call ShowData method for displaying updated data  
        BindData();
    }

Stored procedure: 存储过程:

ALTER PROCEDURE [dbo].[updateCompanyName] 
    @CurrentCompanyName VARCHAR(50),
    @NewCompanyName VARCHAR(50),
    @IsExists INT = 0 OUT 
AS 
BEGIN
    DECLARE @CompanyID INT

    SELECT @CompanyID = CompanyID 
    FROM company 
    WHERE companyname = @CurrentCompanyName

    BEGIN
        IF EXISTS (SELECT CompanyName 
                   FROM company 
                   WHERE companyname = @NewCompanyName )
        BEGIN
            SET @IsExists = 1
        END
        ELSE
        BEGIN   
            UPDATE COMPANY
            SET CompanyName = @NewCompanyName 
            WHERE companyid = @CompanyID

            SET @IsExists = 0
        END
    END

    PRINT @isexists 
END

You are defining name as a textbox in this line: 您正在此行中将name定义为文本框:

TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;

Then you try to set the value of your parameter to the textbox in this line: 然后,尝试在此行中将参数的值设置为文本框:

cmd.Parameters.AddWithValue("@CurrentCompanyName", name);

When what you SHOULD be doing, is setting the parameter value to the TEXT that is IN the textbox: 当您应该执行的操作时,将参数值设置为文本框中的TEXT:

cmd.Parameters.AddWithValue("@CurrentCompanyName", name.Text);

EDIT: 编辑:

Since name itself is NULL when you are hovering over it, that means that you have not correctly defined it in this line: 由于将鼠标悬停在name本身上时, name本身为NULL,这意味着您未在此行中正确定义它:

TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;

Stop the code in the debugger and open a quick view into CompanyTable, and see if you can figure out the correct way to define the textbox you are looking for. 停止调试器中的代码,并打开CompanyTable的快速视图,看看是否可以找到定义所需文本框的正确方法。

EDIT 2: In defining your TextBox, you are doing FindControl("CompanyTable") , but according to your markup, "CompanyTable" is the ID of your GridView, not a textbox. 编辑2:在定义TextBox时,您正在执行FindControl("CompanyTable") ,但是根据您的标记,“ CompanyTable”是GridView的ID,而不是文本框。 In fact, I don't see any markup anywhere for a textbox in the first code sample you posted. 实际上,在您发布的第一个代码示例中,在文本框的任何地方都看不到任何标记。

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

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