I am having troubles populating my datagrid along with the controls in my wpf page, when the the page loads, and also how can I navigate from record to record when I click the next and previous buttons, I want the highlighter to move to another record on the datagrid and the controls to change values correspondingly to the datagrid highlighter. I am reading data from SQL server database
This is my code below.The datagrid name is DGAll. Somebody help me evaluate the code below.
private void StudentRegistrationForm_Loaded(object sender, RoutedEventArgs e)
{
StudentConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SchoolDB"].ConnectionString);
StudentCmd = new SqlCommand();
StudentCmd.Connection = StudentConn;
StudentCmd.CommandType = CommandType.StoredProcedure;
StudentCmd.CommandText = "spStudent_GetAll";
try
{
// Database connection, commands and reader to read and display data from database
StudentConn.Open();
studentReader = StudentCmd.ExecuteReader();
DGAll.ItemsSource = studentReader;
// Open connection and read data from database
if (studentReader.Read())
{
TxtbID.Text = studentReader["Id"].ToString();
TxtLastName.Text = (string)studentReader["LastName"];
TxtFirstName.Text = (string)studentReader["FirstName"];
TxtEmail.Text = (string)studentReader["Email"];
CboGender.Text = (string)studentReader["Gender"];
DtpDateOfBirth.SelectedDate = (DateTime)studentReader["DateofBirth"];
DtpDateRegistered.SelectedDate = (DateTime)studentReader["DateRegistered"];
TxtPhone.Text = (string)studentReader["MobileNumber"];
TxtComment.Text = (string)studentReader["Notes"];
CboReligion.Text = (string)studentReader["Religion"];
// Extract byte array from database and change it to image
imgprofilePicture.Source = GetBitmapImageFromBytes((byte[])studentReader["Image"]);
CboSpecialAttention.Text = (string)studentReader["SpecialAttention"];
//TxtGuardianID.DataContext = (int)studentReader["GuardianID"];
}
}
catch (Exception ex)
{
// If error occurs
if (ex is SqlException || ex is SystemException || ex is NullReferenceException || ex is InvalidOperationException)
{
MessageBox.Show(ex.Message, "Error Establishing Connection to Student Data Service", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
finally
{
//Close all database objects
studentReader.Close();
StudentCmd.Dispose();
StudentConn.Close();
StudentConn.Dispose();
}
// Set the app state to view mode
SetState("View");
}