Conkbabes Conkbabes - 7 months ago 83
SQL Question

Update button in datagrid, entity framework WPF Application

I'm attempting to create an update button, that you can press to automatically update a record within a DB.

the update button opens a new dialog, which then allows user to enter the updated details in text boxes and hit submit. This should then change the record within the DB and refresh the datagrid.

The code wont update the record within the DB.

Update button:

private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
using (db1104983Entities1 context = new db1104983Entities1())
{
Button button = sender as Button; // find which button triggered the event
Student s2 = (Student)button.DataContext;

Window1 dialog = new Window1(s2.MatricNo, s2.FirstName);
Nullable<bool> result = dialog.ShowDialog();

if (dialog.DialogResult == true)
{
string matricNo = dialog.editMatric.Text;
if (matricNo != string.Empty)
s2.MatricNo = matricNo;

string firstName = dialog.editFirst.Text;
if (firstName != string.Empty)
s2.FirstName = firstName;

MessageBox.Show(firstName+"s, record succesfully updated!");
context.SaveChanges();
customDG.ItemsSource = context.Students.ToList();
}
}
}


Window:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

public Window1(string MatricNo, string FirstName)
{
InitializeComponent();
// initialise dialog Textbox
editMatric.Text = MatricNo;
// initialise dialog TextBox
editFirst.Text = FirstName;
}

// event handler if user hits submit button
private void OnSubmit(object sender, RoutedEventArgs e)
{
// Set DialogResult to true
// Property DialogResult is tested by calling code
// to determine whether user cancelled dialog
// or submitted changes
DialogResult = true;
// Close the dialog
Close();
}
}


Finally here is my entire code

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnAddNewStudent(object sender, RoutedEventArgs e)
{

using (db1104983Entities1 context = new db1104983Entities1()) //Contained within using so it automatically disposes when it is out of scope
{
Student s1 = new Student //creates new Student Object


{

//Binds textboxes values to values within the DB

MatricNo = txtM.Text,
FirstName = txt1.Text,
LastName = txt2.Text,
Component1 = txtcom1.Text,
Component2 = txtcom2.Text,
Component3 = txtcom3.Text,

};

MessageBox.Show("Student Added Succesfully"); // Advises user the record has succesfully been added
context.Students.Add(s1); //Adds Student object to DB
context.SaveChanges(); // Commits change to dDB


//Clears all textboxes once record has been added
txtM.Clear();
txt1.Clear();
txt2.Clear();
txtcom1.Clear();
txtcom2.Clear();
txtcom3.Clear();
customDG.ItemsSource = context.Students.ToList(); //Updates the table



}
}

private void OnPurgeDB(object sender, RoutedEventArgs e)
{
using (db1104983Entities1 context = new db1104983Entities1()) //Contained within using so it automatically disposes when it is out of scope
{


// Messagebox confirms user input and only continues if yes is pressed

MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure you want to delete the entire contents of the Database?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
{
context.Database.ExecuteSqlCommand("TRUNCATE TABLE [Student]"); //Deletes contents of DB
MessageBox.Show("Database has been succesfully purged!"); //Confirms that all records have been deleted
customDG.ItemsSource = context.Students.ToList(); // Updates the table after purge

}


}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
using (db1104983Entities1 context = new db1104983Entities1())
{
customDG.ItemsSource = context.Students.ToList();

}
}


private void DeleteRecord_Click(object sender, RoutedEventArgs e)
{
using (db1104983Entities1 context = new db1104983Entities1())
{

var tempval2 = context.Students.Where(w => w.MatricNo == txtdelete.Text).FirstOrDefault();
context.Students.Remove(tempval2); //Removes the record which matches the value in the textbox
context.SaveChanges(); // Commits change to the DB
customDG.ItemsSource = context.Students.ToList(); //Refreshes the datagrid
MessageBox.Show("Student with Matriculation Number " + txtdelete.Text + " has been deleted!"); // Advises user which record has been deleted
txtdelete.Clear(); // Clears textbox once deleted

}
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 win = new Window1();
win.ShowDialog();
}

private void btnDelete_Click(object sender, RoutedEventArgs e)
{

}

private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
using (db1104983Entities1 context = new db1104983Entities1())
{
// find which button triggered the event
Button button = sender as Button;

Student s2 = (Student)button.DataContext;

Window1 dialog = new Window1(s2.MatricNo, s2.FirstName);

Nullable<bool> result = dialog.ShowDialog();

if (dialog.DialogResult == true)
{
string matricNo = dialog.editMatric.Text;
if (matricNo != string.Empty)
s2.MatricNo = matricNo;

string firstName = dialog.editFirst.Text;
if (firstName != string.Empty)
s2.FirstName = firstName;

MessageBox.Show(firstName+"s, record succesfully updated!");
context.SaveChanges();
customDG.ItemsSource = context.Students.ToList();


}
}


}
}
}

Answer

While it would be better if you could refactor this in a proper MVVM style, your main problem is that your Student s2 is not attached to the context so there are no changes to be saved. Attach it to the context :

Student s2 = (Student)button.DataContext;
context.Students.Attach(s2);