Update & Insert in ADO.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a datagrid binded to a dataset and once the user updates the grid my
dataset is reflected.

When i wanted to save the data to tables, however i needed to insert this
data into another table instead of the same table.

I am getting the error in the line
oDa.Update(oDs.Tables[0].Select("", "", DataViewRowState.CurrentRows));

"Update requires a valid UpdateCommand when passed DataRow collection with
modified rows.System.InvalidOperationException: Update requires a valid
UpdateCommand when passed DataRow collection with modified rows. at
System.Data.Common.DbDataAdapter"

----------------------------------------------------------------------------------------
below is the code

Code:
public void CreateFieldMapping(DataSet oDs)
{

string sMethodName = "[public void CreateFieldMapping(DataSet oDs)]";

//===============================================================================
//--- Establish local variable
//===============================================================================
string sProcName;
//string sConnString = Common.BuildConnectionString();
SqlDataAdapter oDa = new SqlDataAdapter();
SqlTransaction oTrn = null;
SqlConnection oCn =  GetConnection();
SqlCommand oInsCmd = null;

try

//===============================================================================
//--- Open the Connection and create the Transactio
//===============================================================================
oCn.Open();
oTrn = oCn.BeginTransaction();

//===============================================================================
//--- Set up the INSERT Comman
//===============================================================================
sProcName = "UEPSP_CONF_MAP_FIELDS_INSERT";
oInsCmd = new SqlCommand(sProcName, oCn, oTrn);
oInsCmd.CommandType = CommandType.StoredProcedure;
oInsCmd.Parameters.Add(new SqlParameter("@MAP_FIELD_CK", SqlDbType.Int,
4, "MAP_FIELD_CK"));
oInsCmd.Parameters.Add(new SqlParameter("@GROUP_CK", SqlDbType.Int,
4,"GROUP_CK"));
oInsCmd.Parameters.Add(new SqlParameter("@MAP_TYPE", SqlDbType.VarChar,
1, "MAP_TYPE"));
oInsCmd.Parameters.Add(new SqlParameter("@MAP_SEQ_NO", SqlDbType.Int, 4,
"MAP_SEQ_NO"));
oInsCmd.Parameters.Add(new SqlParameter("@EFF_DT", SqlDbType.DateTime,
10, "EFF_DT"));
oInsCmd.Parameters.Add(new SqlParameter("@TERM_DT", SqlDbType.DateTime,
10, "TERM_DT"));
oInsCmd.Parameters.Add(new SqlParameter("@DISPLAY_NAME",
SqlDbType.VarChar, 30, "DISPLAY_NAME"));
oInsCmd.Parameters.Add(new SqlParameter("@TABLE_NAME",
SqlDbType.VarChar, 30, "TABLE_NAME"));
oInsCmd.Parameters.Add(new SqlParameter("@FIELD_NAME",
SqlDbType.VarChar, 30, "FIELD_NAME"));
oInsCmd.Parameters.Add(new SqlParameter("@FIELD_TYPE",
SqlDbType.VarChar, 20, "FIELD_TYPE"));
oInsCmd.Parameters.Add(new SqlParameter("@MAPPED_FIELD_MIN",
SqlDbType.VarChar, 10, "MAPPED_FIELD_MIN"));
oInsCmd.Parameters.Add(new SqlParameter("@MAPPED_FIELD_MAX",
SqlDbType.VarChar, 10, "MAPPED_FIELD_MAX"));

//oInsCmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
oDa.InsertCommand = oInsCmd;

//===============================================================================
//--- Add the record(s
//===============================================================================
Common.ShowRowStates(oDs.Tables[0], "*** BEFORE FIELDS INSERTS ***");
oDa.Update(oDs.Tables[0].Select("", "", DataViewRowState.CurrentRows));
Common.ShowRowStates(oDs.Tables[0], "*** AFTER ALL ***");

oTrn.Commit();
oCn.Close();
}
catch (CustomException exCustom)

//===============================================================================
//--- Rollback the transactio
//===============================================================================
oTrn.Rollback();

//---------------------------------------------------------
//--- Push the custom Exception on the stack and re-throw it.
//---------------------------------------------------------
exCustom.CallStack.Push(this.m_sClassName, sMethodName);
throw(exCustom);

}
catch (Exception ex)
{
//===============================================================================
//--- Rollback the transaction
//===============================================================================
oTrn.Rollback();

//---------------------------------------------------------
//--- Create and throw a custom Exception.
//---------------------------------------------------------
throw(new CustomException("", this.m_sClassName, sMethodName, ex));
}
finally
{
oInsCmd.Dispose();
oDa.Dispose();
oTrn.Dispose();
oCn.Dispose();
}
oCn.Close();
//return oDs;

}

Is there a easy way to do this ? Or do we have to loop around the dataset
and insert one by one?
 
Back
Top