Follwing is a code snippet i use to delete record from database.
Questions and their Respective Possible Answers are Stord in seperate
DataTables(Parent and Child Respectively). I am Using ADO.Net 2.0,
Untyped DataSet Class, and Disconnected Approach to Acces Database.
The Problem is that when i Want To Delete Record from Tables i
manually remove them from my DataTables and then push these changes to
Actual Database.Although updating database tables both in front end
and in actual dabase is performed successfully, but when i want to
delete the record it is deleted from my DataTable Objects(DataRow
Objects ) are removed but i am unable to reflect these changes to
database.
there is no Exception and no Runtime Error as well.
Here is the code snippet:
public void DeleteRecord()
{
if (DataObject.MyConnection.State ==
ConnectionState.Closed)
DataObject.MyConnection.Open();
Trans = DataObject.MyConnection.BeginTransaction();
System.Data.SqlClient.SqlCommand DeleteCmd =
DataObject.MyCommand;
DeleteCmd.Transaction = Trans;
try
{
DataRow RowToDelete=
T3FQuestions.Rows.Find(QuestionId);
Questions.Rows.Remove(RowToDelete);//Questions Is a DataTable
/**************************************************************/
//Deleting from Parent Table
DeleteCmd.Parameters.Clear();
DeleteCmd.CommandType = CommandType.Text;
DeleteCmd.CommandText = "Delete from Tbl3FQuestions
Where [EMAIL PROTECTED]";
DeleteCmd.Parameters.Add("@QId", SqlDbType.Int, 4,
"QuestionId");
DeleteCmd.Parameters["QId"].SourceVersion =
DataRowVersion.Original;
DataObject.GetSetMyAdapter.DeleteCommand = DeleteCmd;
DataObject.PushToDataBase("Questions");
/
******************************************************/
/
******************************************************/
//Answers is A DataTable
DataRow[] RowsToDelete = Answers.Select("QuestionId="
+ QuestionId.ToString(), string.Empty);
foreach (DataRow r in RowsToDelete)
{
Answers.Rows.Remove(r);
}
////////////////////////////////////////
DeleteCmd.Parameters.Clear();
DeleteCmd.CommandType = CommandType.Text;
DeleteCmd.CommandText = "Delete from Tbl3FAnswers
Where [EMAIL PROTECTED]";
DeleteCmd.Parameters.Add("@QId", SqlDbType.Int, 4,
"QuestionId");
DeleteCmd.Parameters["QId"].SourceVersion =
DataRowVersion.Original;
DataObject.GetSetMyAdapter.DeleteCommand =
DeleteCmd;
DataObject.PushToDataBase("Answers");
////////////////////////////////////////
DataObject.MyDataset.AcceptChanges();
Trans.Commit();
Last--;//Row Counter for Number of Records Type of
Integer
}
catch (Exception exp)
{
Trans.Rollback();
DataObject.MyDataset.RejectChanges();
throw exp;
}
}
Here is the PushToDatabase Function:
/// <summary>
/// Updates Actual Database with changes made to Loaded Data
(Dataset)
/// </summary>
/// <param name="TableName">Table Name to be Updated in
Database</param>
public void PushToDataBase(string TableName)
{
try
{
if (MyCon.State == ConnectionState.Closed)
MyCon.Open();
MyAdapter.Update(MyDs, TableName);
}
catch (Exception InnerExp)
{
throw (new Exception(InnerExp.Message, InnerExp));
}
}
So If Any Body Can Help, and BookMark my error, it would be extremely
appreciated