Assalam-u-Alaikum Usman,

Do you have checked the RowState in your DataTable? If the row you are
deleting was freshly added and have RowState = "DataRowState.Added"
then this row is removed from DataTable permanently (from memory), and
if the row is loaded and have RowState = "DataRowState.Unchanged" then
on executing "Questions.Rows.Remove(RowToDelete)", will change the
RowState = "DataRowState.Deleted" (NOT deleting from memory i.e.
DataTable) then DataAdapter will know that DeleteCommand should be
executed for this Row.

Another thing is that your Question Table is parent and Answer Table
is child so, you need to execute the Delete Command in reverse order
i.e. first Delete from Answer (child) and then from Question (parent).
See the Beth Massi's Article,

http://blogs.msdn.com/bethmassi/archive/2007/07/10/working-with-tableadapters-related-datatables-and-transactions.aspx

Regards,
Arsalan Tamiz

On Nov 10, 4:41 pm, Usman <[EMAIL PROTECTED]> wrote:
> 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

Reply via email to