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
答案 0 :(得分:1)
您将name
定义为此行中的文本框:
TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;
然后,您尝试将参数的值设置为此行中的文本框:
cmd.Parameters.AddWithValue("@CurrentCompanyName", name);
当你应该做什么时,将参数值设置为文本框中的TEXT:
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,而不是文本框。事实上,我发布的第一个代码示例中的文本框没有任何标记。