-1

i creat an accounting application in mvc3. i have journal voucher form in it.in this form master and detail data is on single view.i use html table and text boxes in html table for create my grid.I saved my data into sql server 2008 r2 database in 2 tables with help of json and wcf services on single button click.(means on save button click all the master and detail data is inserted in database.there is no edit button on each grid line for editing data into database. Now I want to perform my form's edit functionality on single button click into database. for this purpose i have an idea that i use sqltransaction object and delete data from database temporarily and then insert new data into database and then do the sqltran.commit or sqltrans.rollback on sucess/fail base of transaction.
for this purpose i create a function in DAL in which it firstly bind sqltransaction with command and then do delete functionality with dataadapter and stored procedure.
but my stored procedure is not called and data is not delete i monitor it in sql server profiler.
I paste my code here plzzz any one tell me where i do mistake.

public bool JVInsertDAL1(JVEntity Obj, bool FMode, bool EMode, bool DMode)
    {

        bool FinRes = false;
        DataSet ObjDs = new DataSet();
        //function call to fill tables scheme in dataset

        ObjDs = FillDSDAL(ObjDs, "", "");
        if (FMode == false && DMode == true)
        {

            ObjDs = FillDSDAL(ObjDs, Obj.VNo, Obj.Type);
        }
                //==============================

        ///*********MASTER DATA INSERTION************************
        if (EMode != true || DMode != false)
        {
            if (sqlConnection.State == ConnectionState.Closed)
            {
                sqlConnection.Open();
            }
            sqlTransaction = sqlConnection.BeginTransaction();
        }
      try
        {

            if (FMode == false && DMode == true)
            {

SqlCommand cmdDeleteLedg = new SqlCommand("SpGLdelete", sqlConnection, sqlTransaction);
cmdDeleteLedg.CommandType = CommandType.StoredProcedure;

SqlCommand cmdDeleteHead = new SqlCommand("SpGlHeadDelete", sqlConnection, sqlTransaction);
cmdDeleteHead.CommandType = CommandType.StoredProcedure;

cmdDeleteHead.Parameters.Add("@VNo", SqlDbType.Char, 10, "VNo").Value = Obj.VNo;
cmdDeleteHead.Parameters.Add("@Type", SqlDbType.Char, 2, "Type").Value=Obj.Type;

cmdDeleteLedg.Parameters.Add("@VNo", SqlDbType.Char, 10, "VNo").Value = Obj.VNo;
cmdDeleteLedg.Parameters.Add("@Type", SqlDbType.Char, 2, "Type").Value = Obj.Type;
 //-----------------PROCEDURE FOR DELETE RECORD DURING EDIT MODE-------------           
                ObjDadp.DeleteCommand = cmdDeleteLedg;
                ObjDadp.Update(ObjDs, "Gledg");

                ObjDadp.DeleteCommand = cmdDeleteHead;
                ObjDadp.Update(ObjDs, "Glhead");

                ObjDs.AcceptChanges();
                ObjDs.Clear();
            }
            if (FMode == true)
            {

SqlCommand sqlCommand = new SqlCommand("SPJVHeadInsert", sqlConnection, sqlTransaction);
sqlCommand.CommandType = CommandType.StoredProcedure;

SqlCommand sqlCommand1 = new SqlCommand("SPJVLedgInsert", sqlConnection, sqlTransaction);
sqlCommand1.CommandType = CommandType.StoredProcedure;

sqlCommand.Parameters.Add("@Type", SqlDbType.Char, 2, "Type");
sqlCommand.Parameters.Add("@VNo", SqlDbType.Char, 10, "VNo");
sqlCommand.Parameters.Add("@VDate", SqlDbType.DateTime, 10, "VDate");

sqlCommand1.Parameters.Add("@Type", SqlDbType.Char, 2, "Type");
sqlCommand1.Parameters.Add("@VNo", SqlDbType.Char, 10, "VNo");
sqlCommand1.Parameters.Add("@VDate", SqlDbType.DateTime, 10, "VDate");
sqlCommand1.Parameters.Add("@Code", SqlDbType.Char, 10, "Code");
         ObjRow = ObjDs.Tables["GlHead"].NewRow();
                    ObjRow["Type"] = Obj.Type;
                    ObjRow["VNo"] = Obj.VNo;
                    ObjRow["VDate"] = Obj.VDate;
                    ObjRow["RefRemarks"] = Obj.RefRemarks;
                    ObjDs.Tables["GlHead"].Rows.Add(ObjRow); 

                    foreach (var trans in Obj.GLedg)
                    {
                        ObjRow = ObjDs.Tables["Gledg"].NewRow();
                        ObjRow["Type"] = trans.Type;
                        ObjRow["VNo"] = trans.VNo;
                        ObjRow["VDate"] = trans.VDate;
                        ObjRow["Code"] = trans.Code;
                        ObjDs.Tables["Gledg"].Rows.Add(ObjRow); 
                    }

                //----------------------------------------------------
            ObjDadp.InsertCommand = sqlCommand;
            ObjDadp.Update(ObjDs, "GlHead");

            ObjDadp.InsertCommand = sqlCommand1;
            ObjDadp.Update(ObjDs, "GLedg");
                //----------------------------------------------------

            }//END OF FMODE=TRUE CONDITION
            //------------------------------------------------------ 

            //****************************************               
            if (DMode != true || EMode != true)
          //  if (EMode != true || DMode != false)
            {
                sqlTransaction.Commit();
            }
            //************************************
        }//end of try
        catch (SqlException sqlError)
        {
            sqlTransaction.Rollback();

        }
        catch (Exception ex)
        {
            sqlTransaction.Rollback();
        }
        finally
        {
            if (EMode != true || DMode != true)
            {
                if (sqlConnection.State == ConnectionState.Open)
 {
 sqlConnection.Close();
 }
 }
 }            
 //******end of detail insertion***************************************************
 if (FMode == false && DMode == true)
 {
 if (ObjDs.HasErrors== false)
 {
 FinRes = true;
 }
 else
 {
 FinRes = false;
 }
 }
 if (FMode == true)
 {
 if (ObjDs.HasErrors == false)
{
FinRes = true;
}
else
{
FinRes = false;
}
}            
return FinRes;
}

In above code i pass FMode: true, EMode: false, DMode: false for save data and it insert data into database.
FMode: false, EMode: true, DMode: true when delete data from database in editing mode.
and finally FMode: true, EMode: true, DMode: false for insert data after deletion from database in editing mode.

Any suggestions for solve my problem?

4

1 回答 1

0

从上面的代码看来,您并没有更改表格GlheadGledg.

当上述表的行都处于 DataRowState.Unchanged 状态时,调用 SqlDataAdapter 的Update方法不会对数据库执行任何操作。

当应用程序调用 Update 方法时,DbDataAdapter 会检查 RowState 属性,并根据 DataSet 中配置的索引的顺序为每一行迭代地执行所需的 INSERT、UPDATE 或 DELETE 语句。

您需要从数据表中删除该行,然后调用 SqlDataAdapter 的 Update 方法

于 2013-07-25T09:21:27.393 回答