Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have a table named industries In this my fields are
workfor_id,workfor_usr_id,workfor_industry_id
with the same values of workfor_id, I have different workfor_industry_id's.I want to update multiple records based on workfor_id.
can u tell me how to implement this.
Posted
Comments
Tejas Vaishnav 11-Oct-12 2:57am    
what you tried so far...
charan1431 11-Oct-12 3:05am    
foreach (var k in us){
var indList = dbContext.industries.Where(i => i.workfor_id == k.id).Select(i => i).ToArray();
string ind2 = k.industry;
var industryParts = ind2.Split(',');
var o = (industryParts.Length);
for (c = 0; c < o; c++){
industries ind = new industries();
ind.workfor_id = Convert.ToInt16(k.id);
ind.workfor_industry_id = Convert.ToInt16(k.industryid);
} }
with this i am getting multiple records depending upon workfor_id.
Greysontyrus 11-Oct-12 4:21am    
It looks like you're taking a csv list in 'industryParts' and are creating new 'industries' instances for each item in the csv. First, without anything else within that 'for' loop, you are losing every instance of 'ind' apart from the last. Second, each instance 'ind' is identical.
Q: where are you expecting this update to take place? Is this the whole code?
charan1431 11-Oct-12 4:34am    
actually with the above code,new record is adding with out updating the existing record.so I removed code for creating new instance.
I want to update the records in indList with different workfor_industry_id values.
Greysontyrus 12-Oct-12 5:42am    
Please use the reply button on the comment. That way the conversation is easy to track and I get a note letting me know that you've replied ^_^
To update a record you have to change the record you got back from the data context, not a new instance. I think what you need is:
foreach(industries ind in indList){
ind.workfor_id = Convert.ToInt16(k.id);
ind.workfor_industry_id = Convert.ToInt16(k.industryid);
}
dbContext.SubmitChanges();

or am I getting the wrong end of the stick?

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