Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a DataGrid with three fields and also a Checkbox. On the check of the Checkbox, i want to update the DataBase. I have tried doing it. But the values are not reflecting in the database. The table to which am updating the data has no primary key. Will it also matter??

Here is the Code.
C#
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    using (DataClasses1DataContext context = new DataClasses1DataContext())
    {

        DataGridCellInfo defect = DataGrid.SelectedCells[0];

        string value = ((TextBlock)defect.Column.GetCellContent(defect.Item)).Text;

        int DefectID = Convert.ToInt32(value);

        var updateStatus = (from maintain in context.MaintenanceDefects
                            where maintain.DefectID == DefectID
                                select maintain).ToList();

         foreach (var pvalue in updateStatus)
        {
            System.Console.WriteLine(updateStatus[0].DefectStatus);
            System.Console.WriteLine(updateStatus[0].DefectID);
            System.Console.WriteLine(updateStatus[0].MaintenanceID);

            updateStatus[0].DefectStatus = "Fixed";

            context.SubmitChanges();

        }
    }
}


Kindly suggest the solution.

Thanks & regards
raj_arenem
Posted
Updated 28-Mar-13 0:09am
v4
Comments
Jegan Thiyagesan 28-Mar-13 6:06am    
Why are you going through the foreach loop but selecting only the first item in the updateStatus collection?
raj_arenem 28-Mar-13 6:08am    
Thank you for your reply.
I tried the other way also, but the result is the same.

1 solution

I had a quick look. I think the problem is in the foreach loop:
C#
foreach (var pvalue in updateStatus)
 {
 System.Console.WriteLine(updateStatus[0].DefectStatus);
 System.Console.WriteLine(updateStatus[0].DefectID);
 System.Console.WriteLine(updateStatus[0].MaintenanceID);

 updateStatus[0].DefectStatus = "Fixed";

 context.SubmitChanges();

 }


Its not updating DefectStatus because the foreach loop isn't triggering. Ensure there's data in the list variable named updateStatus before the list executes. Try something like:

C#
if (updateStatus.Count == 0) //Might be: (updateStatus.Count() == 0)
{
  //Throw no data exception and handle it
}

for (int i = 0; i < updateStatus.Count; i++)
{
   //Do work here and submit
}
 
Share this answer
 
v2
Comments
raj_arenem 28-Mar-13 6:05am    
Thank you for your reply.
I checked that, it has data. The thing is it is not getting updated to the database. i would like to mention this, the table to which am updating has no primary key in it. Does this also matter?

Thanks & regards
raj_arenem
J van Rooyen 28-Mar-13 6:42am    
It shouldn't, as long as there are unique identifiers.

Because LINQ needs the Object Model to be selected from the DB, and not cast or created on the fly. In other words, you need to select the object first, like so:

//Use the following code and replace all code in foreach loop
System.Console.WriteLine(updateStatus[0].DefectStatus);
System.Console.WriteLine(updateStatus[0].DefectID);
System.Console.WriteLine(updateStatus[0].MaintenanceID);

//Select a single (and DB bound/linked item)
MaintenanceDefect updateItem = context.MaintenanceDefects.Single(o => o.DefectID == pvalue.DefectID); //May need to parse or cast a few Int's there, depending on your design

//Update a field/property value
updateItem.DefectStatus = "Fixed";

//Update all objects bound/linked to the context
context.SubmitChanges();

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