GridView更新按钮调用SQL Server存储过程时@CurrentCompanyName为null的问题排查
It sounds like your main issue is that the original company name isn’t being passed to your stored procedure when you hit Update—let’s walk through the most common fixes for this in ASPX:
1. Make Sure GridView Tracks the Current Company Name with DataKeys
First, your GridView needs to know which field represents the original value to use for updates. Set the DataKeyNames property to your company name column (this is critical for preserving the original value across postbacks):
<asp:GridView ID="gvCompanies" runat="server" AutoGenerateEditButton="True" DataKeyNames="CompanyName" <!-- Replace with your actual column name --> OnRowUpdating="gvCompanies_RowUpdating"> <!-- Your Columns and EditItemTemplate here --> </asp:GridView>
2. Correctly Pass the Current Name to the Stored Procedure
How you do this depends on whether you’re using a SqlDataSource or direct code-behind calls:
Option A: Using SqlDataSource
If you’re binding via SqlDataSource, map the @CurrentCompanyName parameter to the GridView’s DataKey instead of the edited text box. Use a DataKeyParameter to automatically pull the original value:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YourConnString %>" UpdateCommand="sp_UpdateCompanyName" UpdateCommandType="StoredProcedure"> <UpdateParameters> <!-- Pull original name from GridView DataKeys --> <asp:DataKeyParameter Name="CurrentCompanyName" DataKeyName="CompanyName" Type="String" /> <!-- Get new name from the edited text box --> <asp:ControlParameter Name="NewCompanyName" ControlID="gvCompanies" PropertyName="SelectedRow.Cells[1].Text" <!-- Adjust to your column index --> Type="String" /> <!-- Default value for IsExists --> <asp:Parameter Name="IsExists" Type="Boolean" DefaultValue="False" /> </UpdateParameters> </asp:SqlDataSource>
Option B: Direct Code-Behind Call
If you’re handling the update in code-behind (e.g., in the RowUpdating event), retrieve the original name from the GridView’s DataKeys collection—don’t get it from the edited text box (that’s the new name!):
protected void gvCompanies_RowUpdating(object sender, GridViewUpdateEventArgs e) { // Get original current company name from DataKeys string currentCompanyName = gvCompanies.DataKeys[e.RowIndex].Value.ToString(); // Get new company name from the edited text box in the row TextBox txtNewName = (TextBox)gvCompanies.Rows[e.RowIndex].FindControl("txtCompanyName"); string newCompanyName = txtNewName.Text; // Execute stored procedure using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnString"].ConnectionString)) { SqlCommand cmd = new SqlCommand("sp_UpdateCompanyName", conn); cmd.CommandType = CommandType.StoredProcedure; // Add parameters—make sure CurrentCompanyName is set here! cmd.Parameters.AddWithValue("@CurrentCompanyName", currentCompanyName); cmd.Parameters.AddWithValue("@NewCompanyName", newCompanyName); cmd.Parameters.AddWithValue("@IsExists", false); // Use your default value conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } // Exit edit mode and rebind GridView to show changes gvCompanies.EditIndex = -1; BindGridView(); // Your method to reload data }
3. Verify Your Stored Procedure
Double-check that your stored procedure uses @CurrentCompanyName in the WHERE clause to target the correct record:
CREATE PROCEDURE sp_UpdateCompanyName @CurrentCompanyName VARCHAR(100), @NewCompanyName VARCHAR(100), @IsExists BIT = 0 AS BEGIN SET NOCOUNT ON; -- Optional: Check if new name exists if using @IsExists IF @IsExists = 1 BEGIN IF EXISTS(SELECT 1 FROM Companies WHERE CompanyName = @NewCompanyName) BEGIN -- Handle duplicate logic here (e.g., throw error) RETURN; END END -- Update the record using the current name to find it UPDATE Companies SET CompanyName = @NewCompanyName WHERE CompanyName = @CurrentCompanyName; END
4. Check ViewState
Ensure EnableViewState="true" is set on your GridView (it’s enabled by default, but if someone turned it off, DataKeys won’t persist across postbacks, leading to null values):
<asp:GridView ID="gvCompanies" runat="server" EnableViewState="True" ...>
These steps should fix the null @CurrentCompanyName issue and get your update working correctly.
内容的提问来源于stack exchange,提问作者RA19

