0% found this document useful (0 votes)
68 views

Deleting Data From Gridview Using Objectdatasource Control - Part 15

This document provides steps to delete data from a GridView control using an ObjectDataSource control in ASP.NET. It involves: 1. Creating a class to access employee data and business object 2. Adding methods to select all employees and delete by ID 3. Configuring the ObjectDataSource and GridView 4. Setting the GridView's DataKeyNames to use the employee ID The document then details each step, including creating the data access class, business object, data methods, and configuring the controls to enable deleting records from the GridView.

Uploaded by

shradha11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Deleting Data From Gridview Using Objectdatasource Control - Part 15

This document provides steps to delete data from a GridView control using an ObjectDataSource control in ASP.NET. It involves: 1. Creating a class to access employee data and business object 2. Adding methods to select all employees and delete by ID 3. Configuring the ObjectDataSource and GridView 4. Setting the GridView's DataKeyNames to use the employee ID The document then details each step, including creating the data access class, business object, data methods, and configuring the controls to enable deleting records from the GridView.

Uploaded by

shradha11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

8/28/2017 Sql server, .

net and c# video tutorial: Deleting data from gridview using objectdatasource control - Part 15

More Next Blog Create Blog Sign In

Sql server, .net and c# video tutorial


Free C#, .Net and Sql server video tutorial for beginners and intermediate programmers.

Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Deleting data from gridview using objectdatasource control - Part 15

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
Best software training and placements in
marathahalli, bangalore. For further
details please call 09945699393.

We will be using tblEmployee table for this demo. For SQL script to create and
CBSE Class 9 Maths
populate this table, please refer to, Part 13 - Deleting data from gridview using
Number Systems
sqldatasource control of asp.net gridview tutorial.

CPT Tutorial
Part 1 : Video | Text | Slides

Important Videos
The Gift of Education

Web application for your business

How to become .NET developer

Resources available to help you

Steps to delete from gridview using objectdatasource control the following are Dot Net Video Tutorials
the steps
Angular 2 Tutorial
1. Create EmployeeDataAccessLayer class
2. Create Employee business object in EmployeeDataAccessLayer.cs file Design Patterns
3. Add a static method to select all employees in EmployeeDataAccessLayer class
4. Add a static method to delete employee record using EmployeeId, in ASP.NET Web API
EmployeeDataAccessLayer class
5. Configure objectdatasource and gridview control. Bootstrap
6. Set "EmployeeId" as the value for DataKeyNames property of the gridview control.
AngularJS Tutorial
Now let us look at the steps in detail
jQuery Tutorial
Step 1: Create EmployeeDataAccessLayer class
Right click on the web application project and add a class file with name JavaScript with ASP.NET Tutorial
EmployeeDataAccessLayer.cs
JavaScript Tutorial
Step 2: Create Employee business object in EmployeeDataAccessLayer.cs file
public class Employee Charts Tutorial
{
LINQ
public int EmployeeId { get; set; }
public string Name { get; set; }
LINQ to SQL
public string Gender { get; set; }
public string City { get; set; } LINQ to XML
}
Entity Framework
Step 3: Add a static method to select all employees in EmployeeDataAccessLayer
http://csharp-video-tutorials.blogspot.in/2013/03/deleting-data-from-gridview-using.html 1/5
8/28/2017 Sql server, .net and c# video tutorial: Deleting data from gridview using objectdatasource control - Part 15
class WCF
public static List<Employee> GetAllEmployees()
{ ASP.NET Web Services
List<Employee> listEmployees = new List<Employee>();
Dot Net Basics

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; C#
using (SqlConnection con = new SqlConnection(CS))
{ SQL Server
SqlCommand cmd = new SqlCommand("Select * from tblEmployee", con);
con.Open(); ADO.NET
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) ASP.NET
{
GridView
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr["EmployeeId"]); ASP.NET MVC
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString(); Visual Studio Tips and Tricks
employee.City = rdr["City"].ToString();
Dot Net Interview Questions
listEmployees.Add(employee);
} Slides
}
Entity Framework

return listEmployees; WCF


}
ASP.NET Web Services
Step 4: Add a static method to delete employee record using EmployeeId, in
EmployeeDataAccessLayer class Dot Net Basics
public static void DeleteEmployee(int EmployeeId)
C#
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SQL Server
using (SqlConnection con = new SqlConnection(CS))
{ ADO.NET
SqlCommand cmd = new SqlCommand("Delete from tblEmployee where
EmployeeId = @EmployeeId", con); ASP.NET
SqlParameter param = new SqlParameter("@EmployeeId", EmployeeId);
cmd.Parameters.Add(param); GridView
con.Open();
ASP.NET MVC
cmd.ExecuteNonQuery();
}
Visual Studio Tips and Tricks
}

Step 5: Configure objectdatasource and gridview control. Java Video Tutorials


Compile the project. If the project is not compiled, EmployeeDataAccessLayer class Part 1 : Video | Text | Slides
may not show up in the wizard when configuring objectdatasource control.
1. Right click on "ObjectDataSource1" control and select "Show Smart Tag" Part 2 : Video | Text | Slides
2. Now click on "Configure Data Source" link
Part 3 : Video | Text | Slides
3. Select "EmployeeDataAccessLayer" class from "Choose your business object
dropdownlist" and click next
4. On "Define Data Methods" screen, select "GetAllEmployees" method Interview Questions
5. Now click on "DELETE" tab and select "DeleteEmployee" method and click Finish C#

Now, associate "ObjectDataSource1" with "GridView1" SQL Server

Step 6: Set "EmployeeId" as the value for DataKeyNames property of the gridview Written Test
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">

http://csharp-video-tutorials.blogspot.in/2013/03/deleting-data-from-gridview-using.html 2/5

You might also like