GridView Delete() with ObjectDataSource
-
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.
-
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.
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
-
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
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?
-
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?
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