Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private int _saveRecord()
   {
       string _tableName = DBStructure.TABLE_SITE_POWER_PLANT_MASTER;
       int _retVal = -1;
       int _errorCode = (int)ErrorCodes.NoError;
       try
       {
           if (PowerPlantTable.Rows.Count > 0)
           {
               foreach (DataRow _dr in PowerPlantTable.Rows)
               {

                   RecordValues[] _newRec = new RecordValues[6];

                   _newRec[0].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_POWER_PLANT_ID;
                   _newRec[0].FieldType = Fieldtypes.Integer;
                   _newRec[0].FieldValue = _dr["Power Plant"].ToString();

                   _newRec[1].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_METER_SLAVE_ID;
                   _newRec[1].FieldType = Fieldtypes.Integer;
                   _newRec[1].FieldValue = _dr["DC Meter"].ToString();

                   _newRec[2].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_CHANNEL_ID;
                   _newRec[2].FieldType = Fieldtypes.Integer;
                   _newRec[2].FieldValue = _dr["Channel ID"].ToString();

                   _newRec[3].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_CREATED_BY;
                   _newRec[3].FieldType = Fieldtypes.VarChar;
                   _newRec[3].FieldValue = UserName.ToString();

                   _newRec[4].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_CREATED_DATE;
                   _newRec[4].FieldType = Fieldtypes.Int;
                   _newRec[4].FieldValue = UpdatedTime.ToString();

                   _newRec[5].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_SITE_ID;
                   _newRec[5].FieldType = Fieldtypes.Integer;
                   _newRec[5].FieldValue = SiteID.ToString();

                   if (_dr["SiteID"].ToString().Equals("0"))
                   {
                       _retVal = _dalAccess.Insert(false, _tableName, _newRec, out _errorCode);

                       if (_errorCode == (int)ErrorCodes.NoError)
                       {
                           StatusDescription = GetGlobalResourceObject(globalResourceFileName, "InsertedSucessfully").ToString();
                       }
                       else
                           StatusDescription = _errorLogger.GetErrorMessage(_errorCode);
                   }
                   else
                   {
                       cndCheck[] _whClause = new cndCheck[1];
                       _whClause[0].FieldName = DBStructure.SITE_POWER_PLANT_MASTER_SITE_ID;
                       _whClause[0].FieldType = Fieldtypes.Integer;
                       _whClause[0].FirstFieldValue = _dr["SiteID"].ToString();
                       _whClause[0].ArithmeticOp = ArithmeticOperator.EQUALS;

                       _retVal = _dalAccess.Update(false, _tableName, _newRec, _whClause, out _errorCode);
                       if (_errorCode == (int)ErrorCodes.NoError)
                       {
                           StatusDescription = GetGlobalResourceObject(globalResourceFileName, "UpdatedSuccessfully").ToString();
                       }
                       else
                           StatusDescription = _errorLogger.GetErrorMessage(_errorCode);
                   }
               }
           }
           else
           {
               StatusDescription = GetGlobalResourceObject(ResorceFileName, "PowerPlantNotExists").ToString();
           }
           return _retVal;
       }
       catch (Exception ex)
       {
           _errorLogger.PrintUnknownError(ex.Message.ToString(), GetModuleName(), _getPageName(), "_saveRecord()");
           return -1;
       }
   }







C#
private void _updateToDataTable(int _rowIndex)
   {
       try
       {
           bool blCheck = _checkEditData(_rowIndex);
           if (blCheck)
           {
           PowerPlantTable.Rows[_rowIndex].BeginEdit();
           DataRow dr = PowerPlantTable.Rows[_rowIndex];

           dr["Power Plant"] = cmbPowerPlant.SelectedItem.Text;
           dr["DC Meter"] = cmbDcMeter.SelectedItem.Text;
           dr["Channel ID"] = cmbChannel.SelectedItem.Text;

           ViewState.Remove("rowIndex");
           PowerPlantTable.AcceptChanges();

           wbtnSave.Text = GetGlobalResourceObject(globalResourceFileName, "lnkAdd").ToString();
           wbtnSave.ToolTip = GetGlobalResourceObject(globalResourceFileName, "lnkAddToolTip").ToString();
           _setPageAccessibilty();
           _clearControls();
       }
           else
           {
               StatusDescription = GetGlobalResourceObject(ResorceFileName, "PPExists").ToString();
           }
       }

       catch (Exception Ex)
       {
           _errorLogger.PrintUnknownError(Ex.Message, GetModuleName(), _getPageName(), "_updateToDataTable()");
       }
   }




wen i add records and come out of the list page n again go back and edit the saved record
after saving the record will be discarded from the grid and records are saving multiple times in database overriding the existing records
where am i going wrong?
please someone help me wid this piece of code
Posted
Updated 4-Feb-14 18:25pm
v2
Comments
Prasad Avunoori 5-Feb-14 2:29am    
Hi, Are you using browser back button and clicking on Save Button?
Member 10395687 5-Feb-14 3:42am    
no i ve add clear cancel and save button
i ve edit option wen i click on the record
once it is in edit mode add button only ll become update button.
after i update and then click on save the record ll override the other record and only updated record will be displayed wen i next visit the same grid.
and in database also the updated record ll override other record

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900