In this article I will demonstrate the few uses of the ObjectDataSource. This is a two part article series. In the first part I will demonstrate some of the cool uses of the ObjectDataSource control. In the next article I will discuss Ugly part of the ObjectDataSource control. The article is also accompanied with code (C# only) which can be downloaded.

Introduction:

In this article I will demonstrate the few uses of the ObjectDataSource. This is a two part article series. In the first part I will demonstrate some of the cool uses of the ObjectDataSource control. In the next article I will discuss Ugly part of the ObjectDataSource control. The article is also accompanied with code (C# only) which can be downloaded.

What is ObjectDataSource Control?

ObjectDataSource control allows you to bind your objects to a control on the form. These controls can be any web server controls. Mainly ObjectDataSource control is used to bind to the GridView and DetailsView control.

Displaying Data:

One of the good feature about ObjectDataSource control is that you can quickly bind a control to it without writing any code for the binding operation. Let's see a quick example. Suppose I have a Customer class which contains a method GetAllCustomers() which, returns all the customers to the presentation layer.

public static List<Customer> GetAllCustomers()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlCommand myCommand = new SqlCommand("SELECT LastName,FirstName FROM Customers", myConnection);

List<Customer> customerList = new List<Customer>();

SqlDataReader reader = null;

myConnection.Open();

reader = myCommand.ExecuteReader();

while (reader.Read())

{

Customer customer = new Customer((string)reader["FirstName"], (string)reader["LastName"]);

customerList.Add(customer);

}

return customerList;

}

 

Now if you wish to bind the customerList to the presentation layer you can simply use:

<asp:ObjectDataSource ID="CustomerData" SelectMethod="GetAllCustomers"

TypeName = "AzamSharp.Testing.BusinessLogic.Customer" runat="server">

And the GridView HTML Code will look something like this:

<asp:GridView ID="GridView1" DataSourceID="CustomerData" EmptyDataText="No data available" runat="server" BackColor="White" BorderColor="#3366CC"

BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Bold="False" Font-Size="11pt"

Font-Underline="False" AutoGenerateColumns="False" ShowFooter="True">

<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />

<RowStyle BackColor="White" ForeColor="#003399" />

<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />

<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />

<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />

<Columns>

<asp:BoundField DataField="LastName" HeaderText="LastName" />

<asp:BoundField DataField="FirstName" HeaderText="FirstName" />

</Columns>

</asp:GridView>

Now, when you display your GridView control your GridView will be populated.

Inserting Data using ObjectDataSource Control:

Inserting data using the ObjectDataSource control is also pretty simple. All you need to do is to use the correct insert parameter type and set the InsertMethod name and that's it. Here is a simple example which takes input using a TextBox control and inserts it into the database.

<asp:ObjectDataSource ID="CustomerData" SelectMethod="GetAllCustomers"

InsertMethod="AddNewCustomer" TypeName = "AzamSharp.Testing.BusinessLogic.Customer" runat="server">

<InsertParameters>

<asp:ControlParameter ControlID="txtFirstName" Name="firstName" PropertyName = "Text" Type="String" />

<asp:ControlParameter ControlID = "txtLastName" Name="lastName" PropertyName="Text" Type="String" />

</InsertParameters>

 

In the button click event you need to call ObjectDataSource insert method which will call the appropriate custom insert method which is specified by you. In the above code ControlParameter represents the control from which the arguments is coming which in this case is the TextBox control.

protected void Button1_Click(object sender, EventArgs e)

{CustomerData.Insert(); }

or if you like you can do all the stuff dynamically like this:

private void InsertDynamically()

{

string firstName = txtFirstName.Text;

string lastName = txtLastName.Text;

CustomerData.InsertMethod = "AddNewCustomer";

CustomerData.InsertParameters.Add("FirstName", firstName);

CustomerData.InsertParameters.Add("lastName", lastName);

}

When you call CustomerData.Insert() where CustomerData is the name of the ObjectDataSource control it will call the method that you have provided in the InsertMethod property which in this case is "AddNewCustomer". The TypeName property describe where is the method location which is the name of the class.

// This accepts firstName and lastName as String values

public static void AddNewCustomer(string firstName, string lastName)

{

string query = "INSERT INTO Customers(FirstName,LastName) VALUES(@FirstName, @LastName)";

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlCommand myCommand = new SqlCommand(query, myConnection);

myCommand.Parameters.AddWithValue("@FirstName", firstName);

myCommand.Parameters.AddWithValue("@LastName", lastName);

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

}

Since, the GridView control is already bind to the ObjectDataSource control using the DataSourceID property you will see that as soon as you insert new data it will be displayed in the GridView control.

Deleting Data using ObjectDataSource control:

Deleting is also much like inserting. Here is the code that deletes the customer based on its customer ID.

public static void DeleteCustomer(int customerID)

{

string query = @"DELETE FROM Customers WHERE CustomerID = @CustomerID";

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlCommand myCommand = new SqlCommand(query, myConnection);

myCommand.Parameters.AddWithValue("@CustomerID", customerID);

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

}

And the button click code for the delete button:

protected void Button2_Click(object sender, EventArgs e)

{

string customerID = (txtCustomerID.Text);

CustomerData.DeleteMethod = "DeleteCustomer";

CustomerData.DeleteParameters.Add("customerID", TypeCode.Int32, customerID);

CustomerData.Delete();

}

I will leave updating as an exercise for the User.

I hope you liked the article, happy coding!