Reputation: 809
SQL Server stored procedure accepts the parameters, current company name, new company name and whether it already exists which has a default value. 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. Not sure how to fix this.
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
Upvotes: 0
Views: 256
Reputation: 31785
You are defining name
as a textbox in this line:
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:
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:
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.
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. In fact, I don't see any markup anywhere for a textbox in the first code sample you posted.
Upvotes: 1