Я добавил событие rowDeleted в свой код, чтобы получить сообщение, если строка удалена с использованием Proparty ActedRows, но событие не запущено
1-Вот код, который используется:
( protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
Label1.Visible = true;
if(e.AffectedRows > 0)
{
Label1.Text = "the Row Deleted";
}
}
2 - это GrideView, который я использовал:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Education" HeaderText="Education" SortExpression="Education" />
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="" Font-Bold="true"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="" Font-Bold="true"></asp:Label>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetEmployees" TypeName="Employee_Mangment.Data.EmployeeDataAccess" UpdateMethod="UpdateEmployee" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="Original_{0}" DeleteMethod="DeleteEmployee">
<DeleteParameters>
<asp:Parameter Name="Original_ID" Type="Int32" />
<asp:Parameter Name="Original_Name" Type="String" />
<asp:Parameter Name="Original_Education" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Original_ID" Type="Int32" />
<asp:Parameter Name="Original_Name" Type="String" />
<asp:Parameter Name="Original_Education" Type="String" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Education" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
</div>
</form>
3 - это моя модель:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Education { get; set; }
}
4 - это мой dataaccessLayer :
public class EmployeeDataAccess
{
public static List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings["Sample"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand()
{
CommandText = "select ID,Name,EducationType from Employee join Education on EduID = EducationID",
Connection = con
};
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
Employee employee = new Employee()
{
ID = Convert.ToInt32(rdr["ID"]),
Name = rdr["Name"].ToString(),
Education = rdr["EducationType"].ToString(),
};
employees.Add(employee);
}
}
return employees;
}
}
public static void UpdateEmployee( int Original_ID, string Original_Name ,string Original_Education , string Name , string Education)
{
string cs = ConfigurationManager.ConnectionStrings["Sample"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand()
{
CommandText = "_try",
Connection = con
};
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameterOginalID = new SqlParameter("@Original_ID", Original_ID);
cmd.Parameters.Add(parameterOginalID);
SqlParameter parameterOginalName = new SqlParameter("@Original_Name", Original_Name);
cmd.Parameters.Add(parameterOginalName);
SqlParameter parameterOginalEducation = new SqlParameter("@Original_Education", Original_Education);
cmd.Parameters.Add(parameterOginalEducation);
SqlParameter ParameterName = new SqlParameter("@Name", Name);
cmd.Parameters.Add(ParameterName);
SqlParameter parameterEducation = new SqlParameter("@Education", Education);
cmd.Parameters.Add(parameterEducation);
cmd.ExecuteNonQuery();
}
}
public static void DeleteEmployee(int Original_ID, string Original_Name ,string Original_Education)
{
string cs = ConfigurationManager.ConnectionStrings["Sample"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand()
{
CommandText = "delete from Employee where ID = @Original_ID and Name = @Original_Name and EducationID = (select EduID from Education where EducationType = @Original_Education)",
Connection = con
};
SqlParameter OginalID = new SqlParameter("@Original_ID", Original_ID);
cmd.Parameters.Add(OginalID);
SqlParameter OginalName = new SqlParameter("@Original_Name", Original_Name);
cmd.Parameters.Add(OginalName);
SqlParameter OginalEducation = new SqlParameter("@Original_Education", Original_Education);
cmd.Parameters.Add(OginalEducation);
cmd.ExecuteNonQuery();
}
}
}
5 - вот _try Stored Prosedure:
CREATE proc [dbo].[_try] (@Original_ID int, @Name varchar(100),@Education varchar(100),@Original_Name varchar(100),@Original_Education varchar(100))
as
begin
update Employee set Name = @Name ,EducationID = (select EduID from Education where EducationType = @Education) where ID = @Original_ID and Name = @Original_Name and EducationID = (select EduID from Education where EducationType = @Original_Education)
End
мне нужно знать, в чем заключается проблема, из-за которой RowDeleted не срабатывает