I have a loop going through the records in a XamDataGrid and updating the database using a SqlDataAdapter. The problem is that its only updating the first record. I read that the Records object only contains the root collection of records. How can i update the selected active record?
daSelectUsers = new SqlDataAdapter("SELECT Name, Age, conn); DataSet DS = new DataSet("DataSet"); conn.Open(); foreach (DataRecord record in recipeSegmentsXamDataGrid.Records) { daUsersCommand = new SqlCommand("UPDATE Users SET Name = '" + record.Cells["Name"].Value + "'," + "Age = '" + record.Cells["Age"].Value + "'," + "WHERE ID = '" + ID + "' ", conn); daSelectUsers.UpdateCommand = daUsersCommand; SqlDataAdapter sda = new SqlDataAdapter(daUsersCommand); daSelectUsers.Fill(DS, "Users"); sda.Update(DS, "Users"); daUsersCommand.ExecuteNonQuery(); conn.Close(); }
Hello,
Thank you for posting in our forums!
With the code you provided, it looks like you are looping through each DataRecord, but you close the connection after the first one and never open it again.
Does this work if you keep the connection open?
Looking forward to hearing from you.
That worked, thanks for spotting that!!
I'm using the same code layout with a INSERT and have my XamDataGrid set to AllowAddNew="True". Instead of looping through each DataRecord I need to insert the selected active data record. How can I accomplish this?