[ Log In ]
Skip Navigation Links

Filling in the TextBoxes and Other Data Relevant Controls

When a new window is presented after selecting, say an item in a DataGrid, the fields on the screen will need to be populated with the selected record. This is with help from LINQ and the .dbml item in the project. In this example, four different types of data is used: char or text field, number field, checkbox and a date field. The ID number is also saved since it is not shown on the screen, but needs to be preserved so it can be carried on to updating the existing record. The conditions checking for null is to steer clear of any errors that would arrise if the record contained any null fields.

private void PopulateScreenFields(LINQTable TableObjectName)
{
if (TableObjectName.NumberField != null) TextBoxName.Text = TableObjectName.NumberField.ToString();
if (TableObjectName.DateReceived != null) DatePickerName.Text = TableObjectName.DateField.ToString();
if (TableObjectName.CharField != null) TextBoxName.Text = TableObjectName.CharField.Trim();
if (TableObjectName.BoolField == true) CheckBoxName.IsChecked = true;
UniqueIDNumber = TableObjectName.UniqueID;
}

So, now the fields on the screen are filled in with the information on the record, the data in the fields, after modifications are made, need to be transfered back into an object variable so it can be saved to the database. The formValid will check whether the record should be saved. If the formValid is not true, then it will restrict the saving of the information and allow the user to correct any incorrect field information. the float condition shows a statement that adds a 0 inthe field if it contains an empty string.

private LINQTable FillTableFieldsFromScreenfields(LINQTable TableObjectName)
{
FormValid = true;

TableObjectName.CharField = TextBoxName1.Text.Trim();

if (CheckBoxName1.IsChecked == true) TableObjectName.BoolField = true;

TableObjectName.Datefield = DatePickerName1.SelectedDate;

int IntNumber;
if (int.TryParse(TextBoxName2.Text, out IntNumber) == false)
{
MessageBox.Show("Please Enter a Valid ____ Number");
FormValid = false;
}
else
{
TableObjectName.NumberField = IntNumber;
}

float FloatNumber;
if (TextBoxName3.Text == "") TextBoxName3.Text = "0";
if (float.TryParse(TextBoxName3.Text, out FloatNumber) == false)
{
MessageBox.Show("Please enter a valid numeric value for the _____ field.");
FormValid = false;
}
else
{
TableObjectName.FloatField = FloatNumber;
}

TableObjectName.UniqueID = UniqueIDNumber;

return TableObjectName;
}