Reputation: 151
I am trying to delete records from grid on checkbox selection. But It's not deleting records. Where I am going wrong. Someone please help me to find it out.
Here is my code-
Default.aspx--
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" BackColor="#999966" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" BackColor="#999966" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Name"
DataSourceID="SqlDataSource1"
style="padding: 0px; margin: 0px; border: thin solid #FF9933; width: 500px;"
CellPadding="0" EmptyDataText="No records found" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle BackColor="Moccasin" Width="500px" Font-Bold="True"
ForeColor="Black" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HRMSConnectionString %>"
SelectCommand="SELECT [Name] FROM [Languages]">
</asp:SqlDataSource>
Default.aspx.cs--
string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlCommand command;
protected void Page_Load(object sender, EventArgs e)
{
tblAdd.Visible = false;
Label1.Visible = false;
GridView1.DataBind();
if (!Page.IsPostBack)
{
fillLanguageGrid();
}
}
public void fillLanguageGrid()
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
GridView1.Visible = true;
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chk");
if (chkdelete.Checked)
{
string name= Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Values["Name"].ToString());
deleteRecordByName(name);
}
}
fillLanguageGrid();
}
public void deleteRecordByName(string Name)
{
using (SqlConnection sqlConnection = new SqlConnection(strcon))
using (SqlCommand cmd = new SqlCommand("[dbo].[hrm_Langauges]", sqlConnection))
{
// define this to be a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));
// define the parameter and set its value
cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)).Value = Name;
//open connection, execute DELETE query, close connection
sqlConnection.Open();
command.Parameters["@status"].Value = "Delete";
cmd.ExecuteNonQuery();
sqlConnection.Dispose();
}
Here is my stored procedure
ALTER PROCEDURE [dbo].[hrm_Langauges]
(
@Name varchar(120),
@CreatedOn datetime,
@UpdatedOn datetime=0,
@CreatedBy bigint=0,
@UpdatedBy bigint=0,
@IsDeleted bit=0,
@status as varchar(50) = ''
)
AS
BEGIN
BEGIN TRANSACTION
DECLARE @ID int;
SELECT @ID = coalesce((select max(ID) + 1 from Languages), 1)
COMMIT
if(@status = 'Display')
BEGIN
SELECT * FROM [dbo].[Languages];
END
else if(@status = 'Add')
BEGIN
IF EXISTS(SELECT Name FROM [dbo].[Languages] WHERE Name = @Name)
Begin
Return 0
End
Else
INSERT INTO [dbo].[Languages](Name, CreatedOn,UpdatedOn,CreatedBy,UpdatedBy,IsDeleted) VALUES(@Name,@CreatedOn,@UpdatedOn,@CreatedBy,@UpdatedBy,@IsDeleted)
END
else if(@status = 'Update')
BEGIN
UPDATE [dbo].[Languages] Set Name=@Name,UpdatedOn=@UpdatedOn WHERE ID=@ID
END
else if(@status = 'Delete')
BEGIN
DELETE FROM [dbo].[Languages] WHERE NAME=@NAME
END
END
Here is snap shot of grid-
Upvotes: 0
Views: 68
Reputation:
In ASP.NET, when you raise an event on a server side control, the Page_Load event is always executed before the code in the control event.
In your case, your user checks the checkboxes, then presses the Delete button. This raises the Page_Load event followed by the btnDelete_Click event. The Page_Load event reloads the checkboxes effectively destroying the selection of checkboxes checked by the user, then the button event code is called. But at this point the selected checkboxes are the unchecked, so your code runs correctly, but doesn't change anything.
Change the Page_Load event adding
Upvotes: 1