Suggested Videos
Part 12 - Using stored procedures with objectdatasource control
Part 13 - Deleting data from gridview using sqldatasource control
Part 14 - ConflictDetection property of SqlDataSource control
We will be using tblEmployee table for this demo. For SQL script to create and populate this table, please refer to, Part 13 - Deleting data from gridview using sqldatasource control of asp.net gridview tutorial.
Steps to delete from gridview using objectdatasource control the following are the steps
1. Create EmployeeDataAccessLayer class
2. Create Employee business object in EmployeeDataAccessLayer.cs file
3. Add a static method to select all employees in EmployeeDataAccessLayer class
4. Add a static method to delete employee record using EmployeeId, in EmployeeDataAccessLayer class
5. Configure objectdatasource and gridview control.
6. Set "EmployeeId" as the value for DataKeyNames property of the gridview control.
Now let us look at the steps in detail
Step 1: Create EmployeeDataAccessLayer class
Right click on the web application project and add a class file with name EmployeeDataAccessLayer.cs
Step 2: Create Employee business object in EmployeeDataAccessLayer.cs file
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
Step 3: Add a static method to select all employees in EmployeeDataAccessLayer class
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("Select * from tblEmployee", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr["EmployeeId"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();
listEmployees.Add(employee);
}
}
return listEmployees;
}
Step 4: Add a static method to delete employee record using EmployeeId, in EmployeeDataAccessLayer class
public static void DeleteEmployee(int EmployeeId)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("Delete from tblEmployee where EmployeeId = @EmployeeId", con);
SqlParameter param = new SqlParameter("@EmployeeId", EmployeeId);
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
}
}
Step 5: Configure objectdatasource and gridview control.
Compile the project. If the project is not compiled, EmployeeDataAccessLayer class may not show up in the wizard when configuring objectdatasource control.
1. Right click on "ObjectDataSource1" control and select "Show Smart Tag"
2. Now click on "Configure Data Source" link
3. Select "EmployeeDataAccessLayer" class from "Choose your business object dropdownlist" and click next
4. On "Define Data Methods" screen, select "GetAllEmployees" method
5. Now click on "DELETE" tab and select "DeleteEmployee" method and click Finish
Now, associate "ObjectDataSource1" with "GridView1"
Step 6: Set "EmployeeId" as the value for DataKeyNames property of the gridview control.
This can be done in HTML or code. Since, EmployeeId is the primary key, we need to set it as the value for "DataKeyNames" property of the gridview control, otherwise deleting does not work.
Setting DataKeyNames property of the gridview control in HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmployeeId" DataSourceID="ObjectDataSource1">
Setting DataKeyNames property of the gridview control in code
GridView1.DataKeyNames = new string[1] { "EmployeeId" };
Part 12 - Using stored procedures with objectdatasource control
Part 13 - Deleting data from gridview using sqldatasource control
Part 14 - ConflictDetection property of SqlDataSource control
We will be using tblEmployee table for this demo. For SQL script to create and populate this table, please refer to, Part 13 - Deleting data from gridview using sqldatasource control of asp.net gridview tutorial.
Steps to delete from gridview using objectdatasource control the following are the steps
1. Create EmployeeDataAccessLayer class
2. Create Employee business object in EmployeeDataAccessLayer.cs file
3. Add a static method to select all employees in EmployeeDataAccessLayer class
4. Add a static method to delete employee record using EmployeeId, in EmployeeDataAccessLayer class
5. Configure objectdatasource and gridview control.
6. Set "EmployeeId" as the value for DataKeyNames property of the gridview control.
Now let us look at the steps in detail
Step 1: Create EmployeeDataAccessLayer class
Right click on the web application project and add a class file with name EmployeeDataAccessLayer.cs
Step 2: Create Employee business object in EmployeeDataAccessLayer.cs file
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
Step 3: Add a static method to select all employees in EmployeeDataAccessLayer class
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("Select * from tblEmployee", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr["EmployeeId"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();
listEmployees.Add(employee);
}
}
return listEmployees;
}
Step 4: Add a static method to delete employee record using EmployeeId, in EmployeeDataAccessLayer class
public static void DeleteEmployee(int EmployeeId)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("Delete from tblEmployee where EmployeeId = @EmployeeId", con);
SqlParameter param = new SqlParameter("@EmployeeId", EmployeeId);
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
}
}
Step 5: Configure objectdatasource and gridview control.
Compile the project. If the project is not compiled, EmployeeDataAccessLayer class may not show up in the wizard when configuring objectdatasource control.
1. Right click on "ObjectDataSource1" control and select "Show Smart Tag"
2. Now click on "Configure Data Source" link
3. Select "EmployeeDataAccessLayer" class from "Choose your business object dropdownlist" and click next
4. On "Define Data Methods" screen, select "GetAllEmployees" method
5. Now click on "DELETE" tab and select "DeleteEmployee" method and click Finish
Now, associate "ObjectDataSource1" with "GridView1"
Step 6: Set "EmployeeId" as the value for DataKeyNames property of the gridview control.
This can be done in HTML or code. Since, EmployeeId is the primary key, we need to set it as the value for "DataKeyNames" property of the gridview control, otherwise deleting does not work.
Setting DataKeyNames property of the gridview control in HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmployeeId" DataSourceID="ObjectDataSource1">
Setting DataKeyNames property of the gridview control in code
GridView1.DataKeyNames = new string[1] { "EmployeeId" };
Hi Venkat sir,
ReplyDeleteI am getting this error . I have used completly your code only i have renamed the properties and prefixed it with letter p. i googled a lot for this information and seen that many are having the similar issue but nobody properly knows solution can you please help me
Server Error in '/' Application.
ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'DeleteEmployee' that has parameters: EmployeeId, pEmpid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'DeleteEmployee' that has parameters: EmployeeId, pEmpid.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'DeleteEmployee' that has parameters: EmployeeId, pEmpid.]
System.Web.UI.WebControls.ObjectDataSourceView.GetResolvedMethodData(Type type, String methodName, IDictionary allParameters, DataSourceOperation operation) +1533607
System.Web.UI.WebControls.ObjectDataSourceView.ExecuteDelete(IDictionary keys, IDictionary oldValues) +1065
System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +84
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +930
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +974
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +205
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9671830
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237
GridView1.DataKeyNames = new string[1] { "EmployeeId" }; its not working properly
ReplyDeleteI also got same error: Server Error in '/' Application.
ReplyDeleteObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'DeleteEmployee' that has parameters: EmployeeId, pEmpid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
I tried to do the following but it did not work either.
public static object GetDataValue(object value)
{
if(value == null)
{
return DBNull.Value;
}
return value;
}
and then use
cmd.Parameters.AddWithValue("@EmployeeId", GetDataValue(EmployeeId));
OldValuesParameterFormatString="original_{0}" just delete this attribute from objectdatasource code...it works fine
ReplyDelete