Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. GridView Delete() with ObjectDataSource

GridView Delete() with ObjectDataSource

Scheduled Pinned Locked Moved ASP.NET
businesshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Taurian110
    wrote on last edited by
    #1

    I am using ObjectDataSource to display records on a Gridview. In my BLL I have three functions GetAll() [returns MyEntity], Save(MyEntity entity) and Delete(MyEntity entity) which make use of a business entity called MyEntity. The GetAll() and Save(MyEntity entity) works perfectly fine but the Delete(MyEntity entity) does not work at all. This is what my BLL Delete(MyEntity entity) looks like: [System.ComponentModel.DataObjectMethod(DataObjectMethodType.Delete,true)] public Boolean Delete(UserEntity tblUserXref) { UserXrefDAL dal = new UserXrefDAL(); return dal.Delete(tblUserXref); } And my DAL Delete(MyEntity entity) looks like this: public Boolean Delete(GlobalClassLibrary.Site.Entities.UserXrefEntity entity) { try { SqlDatabase dbHelper = new SqlDatabase(ConnectionString.GetConnectionString()); SqlCommand dbCommand = (SqlCommand)dbHelper.GetStoredProcCommand("tblUserXrefD"); dbHelper.AddInParameter(dbCommand, "@XrefId", SqlDbType.Int, entity.XrefId); dbHelper.ExecuteNonQuery(dbCommand); return true; } catch (Exception) { throw; } } Now, when I put a break point I found out that Delete button on GridView does call the BLL's Delete(MyEntity entity) method, but, all the values of my entity are null. Please help. Thanks.

    N 1 Reply Last reply
    0
    • T Taurian110

      I am using ObjectDataSource to display records on a Gridview. In my BLL I have three functions GetAll() [returns MyEntity], Save(MyEntity entity) and Delete(MyEntity entity) which make use of a business entity called MyEntity. The GetAll() and Save(MyEntity entity) works perfectly fine but the Delete(MyEntity entity) does not work at all. This is what my BLL Delete(MyEntity entity) looks like: [System.ComponentModel.DataObjectMethod(DataObjectMethodType.Delete,true)] public Boolean Delete(UserEntity tblUserXref) { UserXrefDAL dal = new UserXrefDAL(); return dal.Delete(tblUserXref); } And my DAL Delete(MyEntity entity) looks like this: public Boolean Delete(GlobalClassLibrary.Site.Entities.UserXrefEntity entity) { try { SqlDatabase dbHelper = new SqlDatabase(ConnectionString.GetConnectionString()); SqlCommand dbCommand = (SqlCommand)dbHelper.GetStoredProcCommand("tblUserXrefD"); dbHelper.AddInParameter(dbCommand, "@XrefId", SqlDbType.Int, entity.XrefId); dbHelper.ExecuteNonQuery(dbCommand); return true; } catch (Exception) { throw; } } Now, when I put a break point I found out that Delete button on GridView does call the BLL's Delete(MyEntity entity) method, but, all the values of my entity are null. Please help. Thanks.

      N Offline
      N Offline
      Naveed Kamboh
      wrote on last edited by
      #2

      Well you have written a bit complex code but thats your own way. Are you setting DataKeyNames property of DataGrid? that should be the name of your primarky ke of the table. Also you need to handle DataRowDeleting event of the Datagrid while you are deleting. It may help I think if not then excuse me :)

      Naveed Kamboh

      T 1 Reply Last reply
      0
      • N Naveed Kamboh

        Well you have written a bit complex code but thats your own way. Are you setting DataKeyNames property of DataGrid? that should be the name of your primarky ke of the table. Also you need to handle DataRowDeleting event of the Datagrid while you are deleting. It may help I think if not then excuse me :)

        Naveed Kamboh

        T Offline
        T Offline
        Taurian110
        wrote on last edited by
        #3

        Thanks a lot for your help, DataKeyNames did the trick:). Also on a seperate note: what would you suggesst i should do diffrently to make the code less complex?

        N 1 Reply Last reply
        0
        • T Taurian110

          Thanks a lot for your help, DataKeyNames did the trick:). Also on a seperate note: what would you suggesst i should do diffrently to make the code less complex?

          N Offline
          N Offline
          Naveed Kamboh
          wrote on last edited by
          #4

          Reallly i dont knonw what to tell you. May be you are writing v.good. but i use this simple code which is simple to me :). /// ///Delete the record for specified ID /// /// public void DeleteRecord(Int64 Newsid) { connection= dbConnect(); string sqlDelete = "Delete from tblTestimonials where Newsid=?Newsid"; MySqlCommand myCommand = new MySqlCommand(sqlDelete, connection); myCommand.Parameters.Add("?Newsid", Newsid); int rowsAffected = 0; try { rowsAffected = myCommand.ExecuteNonQuery(); } finally { connection.Close(); connection.Dispose(); } }

          Naveed Kamboh

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups