【问题标题】:GridView Update button execute update SQL Server stored procedureGridView 更新按钮执行更新 SQL Server 存储过程
【发布时间】:2018-02-14 19:33:04
【问题描述】:

SQL Server 存储过程接受参数,当前公司名称,新公司名称,是否已经存在,有默认值。当在前端 UI 上单击编辑按钮时,网格视图允许我编辑公司名称。这将显示一个“更新”按钮 - 单击此按钮时 - 代码会解析,但没有任何更新,公司名称也不会更新。

断点已设置并单步执行,@CurrentCompanyName 返回为 null。不知道如何解决这个问题。

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>

方法代码:

 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();
    }

存储过程:

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

【问题讨论】:

    标签: c# sql-server gridview


    【解决方案1】:

    您将name 定义为这一行中的文本框:

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

    然后您尝试将参数的值设置为该行中的文本框:

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

    当您应该做的是将参数值设置为文本框中的文本时:

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

    编辑:

    由于name 在您将鼠标悬停在其上时其本身为NULL,这意味着您没有在此行中正确定义它:

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

    在调试器中停止代码并打开 CompanyTable 的快速视图,看看您是否能找出定义您要查找的文本框的正确方法。

    编辑 2:在定义您的 TextBox 时,您正在做 FindControl("CompanyTable"),但根据您的标记,“CompanyTable”是您的 GridView 的 ID,而不是文本框。事实上,在您发布的第一个代码示例中,我没有看到任何文本框的任何标记。

    【讨论】:

    • 说明:在执行当前网络请求的过程中发生了一个未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。第 148 行:cmd.Parameters.AddWithValue("@CurrentCompanyName", name.Text);
    • 尝试了你的建议并得到了上述未处理的异常
    • Tab 已经解释了您需要做些什么来解决这个问题。这是您的代码中的一个错误,需要您设置断点并找出未正确设置的内容。我们无法远程执行此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2014-03-21
    • 1970-01-01
    • 2012-10-13
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多