ObjectDataSource and GridView

(ASP.NET)


Tools Used: Visual Studio 2013 Express, Sql Server 2012/2008 Express  and .NET Framework 4.5

The  object data source control is used to execute the query against the database and get the resultset and bind it to the linked data control(e.g gridview).

Unlike sql data source control, The object data source control won't render the sql query in the .aspx page because it will call the dal methods to perform it's operations.

The important properties of object data source control are:

  1. Select Method -> It contains the name of the select method, which will return the datasource to linked data control.
  2. InsertMethod -> It conatains the name of the insert method, which will insert new item to the database.
  3. UpdateMethod -> It contains the name of the update method, which will update the item in the database.
  4. DeleteMethod -> It contains the name of the delete method, which will delete the item in the database.
  5. TypeName -> It contains the name of the dal class and it's scope.

 The important methods of object data source control are:

  1. OnInserted & OnInserting -> Insert methods
  2. OnSelected & OnSelecting -> Select methods
  3. OnUpdated & OnUpdating ->  Update methods
  4. OnDeleted & OnDeleting  -> Delete methods

.aspx page

   <asp:GridView   AllowPaging="True" DataKeyNames="Id" AutoGenerateEditButton="True" 
            AutoGenerateDeleteButton="True" 
            AllowSorting="True" DataSourceID="objds_customers"  ID="grdvw_customers" runat="server">
        </asp:GridView>
     
        <asp:ObjectDataSource   OnUpdated="objds_customers_Updated" 
            OnDeleted="objds_customers_Deleted"  ID="objds_customers"
             UpdateMethod="UpdateCustomers" DeleteMethod="DeleteCustomer"  
            TypeName="ObjectDataSoruce.App_Code.CustomersDAL" SelectMethod="GetCustomers"
             runat="server">
        </asp:ObjectDataSource>
codebheind file/.cs file
namespace ObjectDataSoruce.App_Code
{
    public class CustomersDAL
    {

        public CustomersDAL()
        { }

      
       public DataTable GetCustomers()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["CustomersConn"].ToString()))
                {

                    conn.Open();
                    string sqlQuery = "SELECT Id, CustomerID, CompanyName, ContactName, City, PostalCode, Country FROM Customers";
                    SqlDataAdapter adptr = new SqlDataAdapter(sqlQuery, conn);
                    DataTable dt = new DataTable("Customers");
                    adptr.Fill(dt);
                    return dt;
                }
            }
           catch(Exception)
            { }

            return null;
        }


        public int UpdateCustomers(int Id,string CustomerID,string CompanyName,string ContactName,string City,string PostalCode,string Country)
       {
           try
           {
               using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["CustomersConn"].ToString()))
               {

                   conn.Open();
                   string sqlQuery = "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, City = @City, PostalCode = @PostalCode, Country = @Country WHERE Id = @Id";
                   SqlCommand cmd = new SqlCommand(sqlQuery, conn);

                   cmd.Parameters.AddWithValue("@Id", Id);
                   cmd.Parameters.AddWithValue("@CustomerID", CustomerID);

                   cmd.Parameters.AddWithValue("@CompanyName", CompanyName);
                   cmd.Parameters.AddWithValue("@ContactName", ContactName);

                   cmd.Parameters.AddWithValue("@City", City);
                   cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
                   cmd.Parameters.AddWithValue("@Country", Country);

                   int rowsAffected = 0;

                   rowsAffected = cmd.ExecuteNonQuery();

                   return rowsAffected;
               }
           }
            catch(Exception)
           { }

           return 0;
       }

        public int DeleteCustomer(int Id)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["CustomersConn"].ToString()))
                {

                    conn.Open();
                    string sqlQuery = "DELETE FROM Customers WHERE Id = @Id";
                    SqlCommand cmd = new SqlCommand(sqlQuery, conn);
                    cmd.Parameters.AddWithValue("@Id", Id);

                    int rowsAffected = 0;

                    rowsAffected = cmd.ExecuteNonQuery();

                    return rowsAffected;
                    
                }
            }
            catch(Exception)
            { }

            return 0;
        }
    }
}


Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
5/21/2014
5/21/2014
Download PDF
Download Project

Comments