MVC inserting values into a ViewModel after Model to ViewModel mapping











up vote
0
down vote

favorite












I am working with two different databases. I am passing the Model collection (SQL Server) to a ViewModel collection. The ViewModel has extra properties which I access out of a Visual Fox Pro database. I am able to map the existing properties, but the ViewModel does not save the data after passing the values to it.



The WoCust and the Lname fields return null, but the rest of the properties which come from the original Model pass to the properties in the ViewModel fine.



When I debug at the rdr for the OleDbCommand, it shows that the ViewModel is receiving a value for both rdr[WoCust] and rdr[Lname].



How do I make it so the ViewModel saves the new values?



WOSchedule.cs...



public partial class WOSchedule
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public Nullable<System.DateTime> WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public Nullable<System.DateTime> WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WoWcheduleVM.cs...



public partial class WoScheduleVM
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public DateTime? WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public DateTime? WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public string WoCust { get; set; } // extra property
public string Lname { get; set; } // extra property

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WOSchedulesController.cs



string cs = ConfigurationManager.ConnectionStrings["foxproTables"].ConnectionString;
OleDbConnection cn = new OleDbConnection(cs);
var wOSchedules = db.WOSchedules.Where(w => w.WoDone == null).Include(w => w.QuoteType);

var wOSchedulesVM = wOSchedules.Select(s => new ViewModels.WoScheduleVM()
{
Id = s.Id,
WoNo = s.WoNo,
QuoteTypeId = s.QuoteTypeId,
PriorityNo = s.PriorityNo,
Active = s.Active,
WoDate = s.WoDate,
QuoteID = s.QuoteID,
WoDone = s.WoDone,
WOScheduleListId = s.WOScheduleListId,
StorageLocation = s.StorageLocation
});

cn.Open();
foreach (var sch in wOSchedulesVM)
{
string conn = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = '" + sch.WoNo + "'";
OleDbCommand cmdWO = new OleDbCommand(conn, cn);
OleDbDataReader rdr = cmdWO.ExecuteReader();
while (rdr.Read())
{
sch.WoCust = ((string)rdr["wo_cust"]).Trim();
sch.Lname = ((string)rdr["lname"]).Trim();
}
}
cn.Close();

return View(wOSchedulesVM.OrderByDescending(d => d.WoDate).ToList());









share|improve this question
























  • Your code seems ok. When debugging inside while statement, quickwatch rdr["wo_cust"], check if is null or have value.
    – Gonzalo Lorieto
    Nov 7 at 19:39












  • I did do a watch/debug in that area, and It shows a value being returned from the reader (rdr). I put a breakpoint at the end of the while (}) and saw that the record for sch had a value. This is why I am so confused.
    – Beengie
    Nov 7 at 19:42










  • add rdr.Close() after the end of the while
    – Gonzalo Lorieto
    Nov 7 at 19:47










  • Other thoughts, query may be not returning results
    – Gonzalo Lorieto
    Nov 7 at 19:50















up vote
0
down vote

favorite












I am working with two different databases. I am passing the Model collection (SQL Server) to a ViewModel collection. The ViewModel has extra properties which I access out of a Visual Fox Pro database. I am able to map the existing properties, but the ViewModel does not save the data after passing the values to it.



The WoCust and the Lname fields return null, but the rest of the properties which come from the original Model pass to the properties in the ViewModel fine.



When I debug at the rdr for the OleDbCommand, it shows that the ViewModel is receiving a value for both rdr[WoCust] and rdr[Lname].



How do I make it so the ViewModel saves the new values?



WOSchedule.cs...



public partial class WOSchedule
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public Nullable<System.DateTime> WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public Nullable<System.DateTime> WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WoWcheduleVM.cs...



public partial class WoScheduleVM
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public DateTime? WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public DateTime? WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public string WoCust { get; set; } // extra property
public string Lname { get; set; } // extra property

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WOSchedulesController.cs



string cs = ConfigurationManager.ConnectionStrings["foxproTables"].ConnectionString;
OleDbConnection cn = new OleDbConnection(cs);
var wOSchedules = db.WOSchedules.Where(w => w.WoDone == null).Include(w => w.QuoteType);

var wOSchedulesVM = wOSchedules.Select(s => new ViewModels.WoScheduleVM()
{
Id = s.Id,
WoNo = s.WoNo,
QuoteTypeId = s.QuoteTypeId,
PriorityNo = s.PriorityNo,
Active = s.Active,
WoDate = s.WoDate,
QuoteID = s.QuoteID,
WoDone = s.WoDone,
WOScheduleListId = s.WOScheduleListId,
StorageLocation = s.StorageLocation
});

cn.Open();
foreach (var sch in wOSchedulesVM)
{
string conn = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = '" + sch.WoNo + "'";
OleDbCommand cmdWO = new OleDbCommand(conn, cn);
OleDbDataReader rdr = cmdWO.ExecuteReader();
while (rdr.Read())
{
sch.WoCust = ((string)rdr["wo_cust"]).Trim();
sch.Lname = ((string)rdr["lname"]).Trim();
}
}
cn.Close();

return View(wOSchedulesVM.OrderByDescending(d => d.WoDate).ToList());









share|improve this question
























  • Your code seems ok. When debugging inside while statement, quickwatch rdr["wo_cust"], check if is null or have value.
    – Gonzalo Lorieto
    Nov 7 at 19:39












  • I did do a watch/debug in that area, and It shows a value being returned from the reader (rdr). I put a breakpoint at the end of the while (}) and saw that the record for sch had a value. This is why I am so confused.
    – Beengie
    Nov 7 at 19:42










  • add rdr.Close() after the end of the while
    – Gonzalo Lorieto
    Nov 7 at 19:47










  • Other thoughts, query may be not returning results
    – Gonzalo Lorieto
    Nov 7 at 19:50













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working with two different databases. I am passing the Model collection (SQL Server) to a ViewModel collection. The ViewModel has extra properties which I access out of a Visual Fox Pro database. I am able to map the existing properties, but the ViewModel does not save the data after passing the values to it.



The WoCust and the Lname fields return null, but the rest of the properties which come from the original Model pass to the properties in the ViewModel fine.



When I debug at the rdr for the OleDbCommand, it shows that the ViewModel is receiving a value for both rdr[WoCust] and rdr[Lname].



How do I make it so the ViewModel saves the new values?



WOSchedule.cs...



public partial class WOSchedule
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public Nullable<System.DateTime> WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public Nullable<System.DateTime> WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WoWcheduleVM.cs...



public partial class WoScheduleVM
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public DateTime? WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public DateTime? WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public string WoCust { get; set; } // extra property
public string Lname { get; set; } // extra property

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WOSchedulesController.cs



string cs = ConfigurationManager.ConnectionStrings["foxproTables"].ConnectionString;
OleDbConnection cn = new OleDbConnection(cs);
var wOSchedules = db.WOSchedules.Where(w => w.WoDone == null).Include(w => w.QuoteType);

var wOSchedulesVM = wOSchedules.Select(s => new ViewModels.WoScheduleVM()
{
Id = s.Id,
WoNo = s.WoNo,
QuoteTypeId = s.QuoteTypeId,
PriorityNo = s.PriorityNo,
Active = s.Active,
WoDate = s.WoDate,
QuoteID = s.QuoteID,
WoDone = s.WoDone,
WOScheduleListId = s.WOScheduleListId,
StorageLocation = s.StorageLocation
});

cn.Open();
foreach (var sch in wOSchedulesVM)
{
string conn = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = '" + sch.WoNo + "'";
OleDbCommand cmdWO = new OleDbCommand(conn, cn);
OleDbDataReader rdr = cmdWO.ExecuteReader();
while (rdr.Read())
{
sch.WoCust = ((string)rdr["wo_cust"]).Trim();
sch.Lname = ((string)rdr["lname"]).Trim();
}
}
cn.Close();

return View(wOSchedulesVM.OrderByDescending(d => d.WoDate).ToList());









share|improve this question















I am working with two different databases. I am passing the Model collection (SQL Server) to a ViewModel collection. The ViewModel has extra properties which I access out of a Visual Fox Pro database. I am able to map the existing properties, but the ViewModel does not save the data after passing the values to it.



The WoCust and the Lname fields return null, but the rest of the properties which come from the original Model pass to the properties in the ViewModel fine.



When I debug at the rdr for the OleDbCommand, it shows that the ViewModel is receiving a value for both rdr[WoCust] and rdr[Lname].



How do I make it so the ViewModel saves the new values?



WOSchedule.cs...



public partial class WOSchedule
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public Nullable<System.DateTime> WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public Nullable<System.DateTime> WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WoWcheduleVM.cs...



public partial class WoScheduleVM
{
public long Id { get; set; }
public string WoNo { get; set; }
public Nullable<long> QuoteTypeId { get; set; }
public Nullable<int> PriorityNo { get; set; }
public bool Active { get; set; }
public DateTime? WoDate { get; set; }
public Nullable<long> QuoteID { get; set; }
public DateTime? WoDone { get; set; }
public Nullable<long> WOScheduleListId { get; set; }
public string StorageLocation { get; set; }

public string WoCust { get; set; } // extra property
public string Lname { get; set; } // extra property

public virtual QuoteType QuoteType { get; set; }
public virtual Quote Quote { get; set; }
public virtual WOScheduleList WOScheduleList { get; set; }
}


WOSchedulesController.cs



string cs = ConfigurationManager.ConnectionStrings["foxproTables"].ConnectionString;
OleDbConnection cn = new OleDbConnection(cs);
var wOSchedules = db.WOSchedules.Where(w => w.WoDone == null).Include(w => w.QuoteType);

var wOSchedulesVM = wOSchedules.Select(s => new ViewModels.WoScheduleVM()
{
Id = s.Id,
WoNo = s.WoNo,
QuoteTypeId = s.QuoteTypeId,
PriorityNo = s.PriorityNo,
Active = s.Active,
WoDate = s.WoDate,
QuoteID = s.QuoteID,
WoDone = s.WoDone,
WOScheduleListId = s.WOScheduleListId,
StorageLocation = s.StorageLocation
});

cn.Open();
foreach (var sch in wOSchedulesVM)
{
string conn = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = '" + sch.WoNo + "'";
OleDbCommand cmdWO = new OleDbCommand(conn, cn);
OleDbDataReader rdr = cmdWO.ExecuteReader();
while (rdr.Read())
{
sch.WoCust = ((string)rdr["wo_cust"]).Trim();
sch.Lname = ((string)rdr["lname"]).Trim();
}
}
cn.Close();

return View(wOSchedulesVM.OrderByDescending(d => d.WoDate).ToList());






asp.net-mvc-5 oledb viewmodel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 19:24

























asked Nov 7 at 19:11









Beengie

7712825




7712825












  • Your code seems ok. When debugging inside while statement, quickwatch rdr["wo_cust"], check if is null or have value.
    – Gonzalo Lorieto
    Nov 7 at 19:39












  • I did do a watch/debug in that area, and It shows a value being returned from the reader (rdr). I put a breakpoint at the end of the while (}) and saw that the record for sch had a value. This is why I am so confused.
    – Beengie
    Nov 7 at 19:42










  • add rdr.Close() after the end of the while
    – Gonzalo Lorieto
    Nov 7 at 19:47










  • Other thoughts, query may be not returning results
    – Gonzalo Lorieto
    Nov 7 at 19:50


















  • Your code seems ok. When debugging inside while statement, quickwatch rdr["wo_cust"], check if is null or have value.
    – Gonzalo Lorieto
    Nov 7 at 19:39












  • I did do a watch/debug in that area, and It shows a value being returned from the reader (rdr). I put a breakpoint at the end of the while (}) and saw that the record for sch had a value. This is why I am so confused.
    – Beengie
    Nov 7 at 19:42










  • add rdr.Close() after the end of the while
    – Gonzalo Lorieto
    Nov 7 at 19:47










  • Other thoughts, query may be not returning results
    – Gonzalo Lorieto
    Nov 7 at 19:50
















Your code seems ok. When debugging inside while statement, quickwatch rdr["wo_cust"], check if is null or have value.
– Gonzalo Lorieto
Nov 7 at 19:39






Your code seems ok. When debugging inside while statement, quickwatch rdr["wo_cust"], check if is null or have value.
– Gonzalo Lorieto
Nov 7 at 19:39














I did do a watch/debug in that area, and It shows a value being returned from the reader (rdr). I put a breakpoint at the end of the while (}) and saw that the record for sch had a value. This is why I am so confused.
– Beengie
Nov 7 at 19:42




I did do a watch/debug in that area, and It shows a value being returned from the reader (rdr). I put a breakpoint at the end of the while (}) and saw that the record for sch had a value. This is why I am so confused.
– Beengie
Nov 7 at 19:42












add rdr.Close() after the end of the while
– Gonzalo Lorieto
Nov 7 at 19:47




add rdr.Close() after the end of the while
– Gonzalo Lorieto
Nov 7 at 19:47












Other thoughts, query may be not returning results
– Gonzalo Lorieto
Nov 7 at 19:50




Other thoughts, query may be not returning results
– Gonzalo Lorieto
Nov 7 at 19:50












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The problem is you're using foreach loop for iterating wOSchedulesVM collection, which renders the source collection immutable during iteration. The older documentation version explicitly explains that behavior:




The foreach statement is used to iterate through the collection to get
the information that you want, but can not be used to add or remove
items from the source collection
to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.




Therefore, you should use for loop to be able to modify property values inside that collection, as shown in example below:



using (var OleDbConnection cn = new OleDbConnection(cs))
{
cn.Open();
string cmd = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = @WoNo";

// not sure if it's 'Count' property or 'Count()' method, depending on collection type
for (int i = 0; i < wOSchedulesVM.Count; i++)
{
var sch = wOSchedulesVM[i];
using (OleDbCommand cmdWO = new OleDbCommand(conn, cn))
{
cmd.Parameters.AddWithValue("@WoNo", sch.WoNo)
OleDbDataReader rdr = cmdWO.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
sch.WoCust = (!rdr.IsDbNull(0)) ? rdr.GetString(0).Trim() : string.Empty;
sch.Lname = (!rdr.IsDbNull(1)) ? rdr.GetString(1).Trim() : string.Empty;
}
}
}
}
}


Note: This example includes 3 additional aspects, i.e. parameterized query, checking row existence with HasRows property and checking against DBNull.Value with IsDbNull().



Related issue: What is the best way to modify a list in a 'foreach' loop?






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196234%2fmvc-inserting-values-into-a-viewmodel-after-model-to-viewmodel-mapping%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The problem is you're using foreach loop for iterating wOSchedulesVM collection, which renders the source collection immutable during iteration. The older documentation version explicitly explains that behavior:




    The foreach statement is used to iterate through the collection to get
    the information that you want, but can not be used to add or remove
    items from the source collection
    to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.




    Therefore, you should use for loop to be able to modify property values inside that collection, as shown in example below:



    using (var OleDbConnection cn = new OleDbConnection(cs))
    {
    cn.Open();
    string cmd = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = @WoNo";

    // not sure if it's 'Count' property or 'Count()' method, depending on collection type
    for (int i = 0; i < wOSchedulesVM.Count; i++)
    {
    var sch = wOSchedulesVM[i];
    using (OleDbCommand cmdWO = new OleDbCommand(conn, cn))
    {
    cmd.Parameters.AddWithValue("@WoNo", sch.WoNo)
    OleDbDataReader rdr = cmdWO.ExecuteReader();
    if (rdr.HasRows)
    {
    while (rdr.Read())
    {
    sch.WoCust = (!rdr.IsDbNull(0)) ? rdr.GetString(0).Trim() : string.Empty;
    sch.Lname = (!rdr.IsDbNull(1)) ? rdr.GetString(1).Trim() : string.Empty;
    }
    }
    }
    }
    }


    Note: This example includes 3 additional aspects, i.e. parameterized query, checking row existence with HasRows property and checking against DBNull.Value with IsDbNull().



    Related issue: What is the best way to modify a list in a 'foreach' loop?






    share|improve this answer

























      up vote
      0
      down vote













      The problem is you're using foreach loop for iterating wOSchedulesVM collection, which renders the source collection immutable during iteration. The older documentation version explicitly explains that behavior:




      The foreach statement is used to iterate through the collection to get
      the information that you want, but can not be used to add or remove
      items from the source collection
      to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.




      Therefore, you should use for loop to be able to modify property values inside that collection, as shown in example below:



      using (var OleDbConnection cn = new OleDbConnection(cs))
      {
      cn.Open();
      string cmd = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = @WoNo";

      // not sure if it's 'Count' property or 'Count()' method, depending on collection type
      for (int i = 0; i < wOSchedulesVM.Count; i++)
      {
      var sch = wOSchedulesVM[i];
      using (OleDbCommand cmdWO = new OleDbCommand(conn, cn))
      {
      cmd.Parameters.AddWithValue("@WoNo", sch.WoNo)
      OleDbDataReader rdr = cmdWO.ExecuteReader();
      if (rdr.HasRows)
      {
      while (rdr.Read())
      {
      sch.WoCust = (!rdr.IsDbNull(0)) ? rdr.GetString(0).Trim() : string.Empty;
      sch.Lname = (!rdr.IsDbNull(1)) ? rdr.GetString(1).Trim() : string.Empty;
      }
      }
      }
      }
      }


      Note: This example includes 3 additional aspects, i.e. parameterized query, checking row existence with HasRows property and checking against DBNull.Value with IsDbNull().



      Related issue: What is the best way to modify a list in a 'foreach' loop?






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The problem is you're using foreach loop for iterating wOSchedulesVM collection, which renders the source collection immutable during iteration. The older documentation version explicitly explains that behavior:




        The foreach statement is used to iterate through the collection to get
        the information that you want, but can not be used to add or remove
        items from the source collection
        to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.




        Therefore, you should use for loop to be able to modify property values inside that collection, as shown in example below:



        using (var OleDbConnection cn = new OleDbConnection(cs))
        {
        cn.Open();
        string cmd = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = @WoNo";

        // not sure if it's 'Count' property or 'Count()' method, depending on collection type
        for (int i = 0; i < wOSchedulesVM.Count; i++)
        {
        var sch = wOSchedulesVM[i];
        using (OleDbCommand cmdWO = new OleDbCommand(conn, cn))
        {
        cmd.Parameters.AddWithValue("@WoNo", sch.WoNo)
        OleDbDataReader rdr = cmdWO.ExecuteReader();
        if (rdr.HasRows)
        {
        while (rdr.Read())
        {
        sch.WoCust = (!rdr.IsDbNull(0)) ? rdr.GetString(0).Trim() : string.Empty;
        sch.Lname = (!rdr.IsDbNull(1)) ? rdr.GetString(1).Trim() : string.Empty;
        }
        }
        }
        }
        }


        Note: This example includes 3 additional aspects, i.e. parameterized query, checking row existence with HasRows property and checking against DBNull.Value with IsDbNull().



        Related issue: What is the best way to modify a list in a 'foreach' loop?






        share|improve this answer












        The problem is you're using foreach loop for iterating wOSchedulesVM collection, which renders the source collection immutable during iteration. The older documentation version explicitly explains that behavior:




        The foreach statement is used to iterate through the collection to get
        the information that you want, but can not be used to add or remove
        items from the source collection
        to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.




        Therefore, you should use for loop to be able to modify property values inside that collection, as shown in example below:



        using (var OleDbConnection cn = new OleDbConnection(cs))
        {
        cn.Open();
        string cmd = @"SELECT wo_cust, lname FROM womast INNER JOIN custmast ON womast.wo_cust = custmast.cust_id WHERE wo_no = @WoNo";

        // not sure if it's 'Count' property or 'Count()' method, depending on collection type
        for (int i = 0; i < wOSchedulesVM.Count; i++)
        {
        var sch = wOSchedulesVM[i];
        using (OleDbCommand cmdWO = new OleDbCommand(conn, cn))
        {
        cmd.Parameters.AddWithValue("@WoNo", sch.WoNo)
        OleDbDataReader rdr = cmdWO.ExecuteReader();
        if (rdr.HasRows)
        {
        while (rdr.Read())
        {
        sch.WoCust = (!rdr.IsDbNull(0)) ? rdr.GetString(0).Trim() : string.Empty;
        sch.Lname = (!rdr.IsDbNull(1)) ? rdr.GetString(1).Trim() : string.Empty;
        }
        }
        }
        }
        }


        Note: This example includes 3 additional aspects, i.e. parameterized query, checking row existence with HasRows property and checking against DBNull.Value with IsDbNull().



        Related issue: What is the best way to modify a list in a 'foreach' loop?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 3:28









        Tetsuya Yamamoto

        13.4k41939




        13.4k41939






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196234%2fmvc-inserting-values-into-a-viewmodel-after-model-to-viewmodel-mapping%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini