title: How to do validation for Combobox value to match the database value?
body: <p>I'm create an module which is a Editing Bill module which will enable the user to edit the available in the database. I'm facing one validation problem which is that when the user don't want to edit the bill &amp; click on the "Cancel" button will back to the listview to continue select another record. Here is my problem come as when the user didn't make any modification &amp; accidentally click on the "Status" combo from "UNPAID" to "PAID" then directly quite. When re select the record then the record status value will become "PAID" instead of "UNPAID" which is the actual value. </p>
  
  <p>How can I do so that when every time the user don't make any modification &amp; return back to the listview will set back the default value for the combobox.</p>
  
  <p>Thank you all for viewing this question &amp; hope to receive reply :)</p>
  
  <p>I will attach my code so that easily to refer:
  XAML File</p>
  
  <pre><code>&lt;Window x:Class="HouseWivesSavior.HomecareModule.EditBillRemainder"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="EditBillRemainder" Height="600" Width="800" Loaded="Window_Loaded"&gt;
      &lt;Window.Background&gt;
  
  
          &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
              &lt;GradientStop Color="#FFFFFFFF" Offset="0"/&gt;
              &lt;GradientStop Color="#FFFF00FF" Offset="1"/&gt;
          &lt;/LinearGradientBrush&gt;
  
  
      &lt;/Window.Background&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="MyDataTemplate"&gt;
              &lt;Border BorderBrush="#FF000000" BorderThickness="1,1,0,1" Margin="-6,-2,-6,-2"&gt;
                  &lt;StackPanel Margin="6,2,6,2"&gt;
                      &lt;TextBlock Text="{Binding Path=BillName}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
  
          &lt;DataTemplate x:Key="MyDataTemplate2"&gt;
              &lt;Border BorderBrush="#FF000000" BorderThickness="1,1,0,1" Margin="-6,-2,-6,-2"&gt;
                  &lt;StackPanel Margin="6,2,6,2"&gt;
                      &lt;TextBlock Text="{Binding Path=BillDescription}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
  
          &lt;DataTemplate x:Key="MyDataTemplate3"&gt;
              &lt;Border BorderBrush="#FF000000" BorderThickness="1,1,0,1" Margin="-6,-2,-6,-2"&gt;
                  &lt;StackPanel Margin="6,2,6,2"&gt;
                      &lt;TextBlock Text="{Binding Path=BillAmount}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
  
          &lt;DataTemplate x:Key="MyDataTemplate4"&gt;
              &lt;Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" Margin="-6,-2,-6,-2"&gt;
                  &lt;StackPanel Margin="6,2,6,2"&gt;
                      &lt;TextBlock Text="{Binding Path=BillDueDate, StringFormat='dd-MM-yyyy'}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
  
          &lt;Style x:Key="MyItemContainerStyle" TargetType="{x:Type ListViewItem}"&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt;
              &lt;Setter Property="VerticalContentAlignment" Value="Stretch" /&gt;
              &lt;!--&lt;EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" /&gt;--&gt;
          &lt;/Style&gt;
  
          &lt;!--DatePickerTextBoxFormat--&gt;
          &lt;Style TargetType="{x:Type DatePickerTextBox}"&gt;
              &lt;Setter Property="Control.Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate&gt;
                          &lt;TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd-MM-yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" /&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
  
  
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;ListView Name="lvwBooks" Background="White" ItemsSource="{Binding}" Margin="131,130,4,6" ItemContainerStyle="{DynamicResource MyItemContainerStyle}"&gt;
              &lt;ListView.View&gt;
                  &lt;GridView&gt;
                      &lt;GridViewColumn Header="Bill Name" Width="100"  CellTemplate="{DynamicResource MyDataTemplate}"/&gt;
                      &lt;GridViewColumn Header="Description" Width="250"  CellTemplate="{DynamicResource MyDataTemplate2}"/&gt;
                      &lt;GridViewColumn Header="Amount" Width="160"  CellTemplate="{DynamicResource MyDataTemplate3}"/&gt;
                      &lt;GridViewColumn Header="Due Date" Width="100"  CellTemplate="{DynamicResource MyDataTemplate4}"/&gt;
                      &lt;GridViewColumn Header="Edit"&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;UserControl&gt;
                                      &lt;Hyperlink Click="InputBox_Click"&gt;Edit
                                          &lt;Hyperlink.Style&gt;
                                              &lt;Style TargetType="{x:Type Hyperlink}"&gt;
                                                  &lt;Setter Property="Hyperlink.IsEnabled" Value="False" /&gt;
                                                  &lt;Style.Triggers&gt;
                                                      &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="True"&gt;
                                                          &lt;Setter Property="Hyperlink.IsEnabled" Value="True" /&gt;
                                                      &lt;/DataTrigger&gt;
                                                  &lt;/Style.Triggers&gt;
                                              &lt;/Style&gt;
                                          &lt;/Hyperlink.Style&gt;
                                      &lt;/Hyperlink&gt;
                                  &lt;/UserControl&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
                  &lt;/GridView&gt;
  
  
              &lt;/ListView.View&gt;
          &lt;/ListView&gt;
          &lt;!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! --&gt;
          &lt;Grid x:Name="InputBox" Visibility="Collapsed"&gt;
              &lt;Grid Background="Black" Opacity="0.5" /&gt;
              &lt;Border
              MinWidth="250"
              Background="Orange" 
              BorderBrush="Black" 
              BorderThickness="1" 
              CornerRadius="0,40,0,40" 
              HorizontalAlignment="Center" 
              VerticalAlignment="Center" Height="493" Margin="130,30,120,38" Width="528"&gt;
                  &lt;Canvas Height="446" Width="524"&gt;
  
                      &lt;GroupBox Header="General Information" Height="141" HorizontalAlignment="Left" Margin="193,52,0,0" Name="groupBox1" VerticalAlignment="Top" Width="495" Canvas.Left="-173" Canvas.Top="-67"&gt;
                          &lt;Canvas Background="WhiteSmoke" Opacity="1"&gt;
                              &lt;GridSplitter Background="#FFBCBCBC" HorizontalAlignment="Left" Margin="207,13,0,140" ResizeBehavior="PreviousAndNext" Width="2" Height="50" Canvas.Left="-3" Canvas.Top="48" /&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBlock1" Text="Bill Name" VerticalAlignment="Top" /&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="215,65,0,0" Name="textBlock4" Text="Due Date" VerticalAlignment="Top" Grid.ColumnSpan="2" /&gt;
                              &lt;GridSplitter HorizontalAlignment="Left" ResizeBehavior="PreviousAndNext"
                    Width="1" Background="#FFBCBCBC" Margin="206,63,0,9" /&gt;
                              &lt;DatePicker Height="25" HorizontalAlignment="Right" Margin="0,80,157,0" Name="BillDueDatedatePicker" VerticalAlignment="Top" Width="115" Grid.ColumnSpan="2" Canvas.Left="215" Canvas.Top="6" Text="{Binding Path=BillDueDate, StringFormat='dd-MM-yyyy'}" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}" SelectedDateFormat="Short"/&gt;
                              &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="7,29,0,0" Name="BillNameTxtBox" VerticalAlignment="Top" Width="458" Grid.ColumnSpan="2" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}" Text="{Binding Path=BillName}"/&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="7,63,0,0" Name="textBlock8" Text="Bill Type" VerticalAlignment="Top" /&gt;
                              &lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="8,81,0,0" Name="BillTypeCboBox" VerticalAlignment="Top" Width="193"  Canvas.Left="-5" Canvas.Top="8" Text="{Binding Path=BillTypes}" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}"&gt;
                                  &lt;ComboBoxItem Content="Water" /&gt;
                                  &lt;ComboBoxItem Content="Electricity" /&gt;
                                  &lt;ComboBoxItem Content="Gas" /&gt;
                                  &lt;ComboBoxItem Content="Internet/broadband"/&gt;
                                  &lt;ComboBoxItem Content="Others"/&gt;
                              &lt;/ComboBox&gt;
                              &lt;TextBox Canvas.Left="62" Canvas.Top="3" Height="23" Name="textBox1" Width="120" Visibility="Visible" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}" Text="{Binding Path=BillNo}"/&gt;
                          &lt;/Canvas&gt;
                      &lt;/GroupBox&gt;
  
                      &lt;GroupBox Header="Payment Information" Height="339" HorizontalAlignment="Left" Margin="193,202,0,0" Name="groupBox2" VerticalAlignment="Top" Width="495" Canvas.Left="-174" Canvas.Top="-81"&gt;
                          &lt;Canvas Background="WhiteSmoke" Height="303" Visibility="Visible"&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="7,17,0,0" Name="textBlock6" Text="Amount Due" VerticalAlignment="Top" /&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="7,75,0,0" Name="textBlock7" Text="Description" VerticalAlignment="Top" /&gt;
                              &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="81,14,0,0" Name="BillAmountTxtBox" VerticalAlignment="Top" Width="120" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}" Text="{Binding Path=BillAmount}"/&gt;
                              &lt;TextBox Height="148" HorizontalAlignment="Left" Margin="4,96,0,0" Name="BillDescriptionTxtBox" VerticalAlignment="Top" Width="471"  VerticalScrollBarVisibility="Visible" SpellCheck.IsEnabled="True" TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding Path=BillDescription}" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}"/&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="6,46,0,0" Name="textBlock9" Text="Status" VerticalAlignment="Top" /&gt;
                              &lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="81,46,0,0" Name="BillStatusCboBox" VerticalAlignment="Top" Width="120"   SelectionChanged="comboBox3_SelectionChanged" Text="{Binding Path=BillStatus}" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}" &gt;
                                  &lt;ComboBoxItem Content="UNPAID" /&gt;
                                  &lt;ComboBoxItem Content="PAID" /&gt;
                              &lt;/ComboBox&gt;
                              &lt;TextBlock Height="23" HorizontalAlignment="Left" Margin="215,14,0,0" Name="textBlock10" Text="Bill is Paid:" VerticalAlignment="Top" /&gt;
                              &lt;DatePicker Text="{Binding Path=BillPaidDate}" Height="25" HorizontalAlignment="Left" Margin="212,37,0,0"  Name="BillPaidDatedatePicker" VerticalAlignment="Top" Width="115" IsEnabled="false" DataContext="{Binding ElementName=lvwBooks, Path=SelectedItem}"/&gt;
                              &lt;GridSplitter Background="#FFBCBCBC" HorizontalAlignment="Left" Margin="205,14,0,228" ResizeBehavior="PreviousAndNext" Width="2" /&gt;
                              &lt;Separator Height="26" HorizontalAlignment="Left" Margin="7,250,0,0" Name="separator1" VerticalAlignment="Top" Width="470" /&gt;
                              &lt;Button Content="Done" Height="23" HorizontalAlignment="Left" Margin="322,267,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="YesButton_Click" /&gt;
                              &lt;Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="402,266,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="NoButton_Click" /&gt;
                              &lt;GridSplitter Background="#FFBCBCBC" HorizontalAlignment="Left" Margin="207,13,0,140" ResizeBehavior="PreviousAndNext" Width="0" /&gt;
                          &lt;/Canvas&gt;
                      &lt;/GroupBox&gt;
                  &lt;/Canvas&gt;
              &lt;/Border&gt;
          &lt;/Grid&gt;
       &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code Behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  
  namespace HouseWivesSavior.HomecareModule
  {
      /// &lt;summary&gt;
      /// Interaction logic for EditBillRemainder.xaml
      /// &lt;/summary&gt;
      public partial class EditBillRemainder : Window
      {
          public EditBillRemainder()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              ShowData();
          }
  
  
  
          private void InputBox_Click(object sender, RoutedEventArgs e)
          {
              // CoolButton Clicked! Let's show our InputBox.
              InputBox.Visibility = System.Windows.Visibility.Visible;
          }
  
  
  
          private void NoButton_Click(object sender, RoutedEventArgs e)
          {
              //ShowData();
              // NoButton Clicked! Let's hide our InputBox.
              InputBox.Visibility = System.Windows.Visibility.Collapsed;
          }
  
          private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              var item = sender as ListViewItem;
              if (item != null &amp;&amp; item.IsSelected)
              {
  
              }
          }
  
          //Design
          private void comboBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              ////The logic for this will be stanger but in order to cope iwht the SelectionChange behavior.
              //string s1 = BillStatusCboBox.SelectionBoxItem.ToString();
              //// MessageBox.Show(s1);
              //if (s1.Equals("UNPAID"))
              //{
              //    BillPaidDatedatePicker.IsEnabled = true;
              //    BillPaidDatedatePicker.Text = DateTime.Now.ToString("d/MM/yyyy");
              //}
  
              //if (s1.Equals("PAID"))
              //{
              //    BillPaidDatedatePicker.IsEnabled = false;
              //    //BillStatusCboBox.SelectedIndex = 0;
              //}
  
              ComboBoxItem currentItem = ((System.Windows.Controls.ComboBoxItem)BillStatusCboBox.SelectedItem);
  
              if (currentItem.Content.Equals("PAID"))
              {
                  BillPaidDatedatePicker.IsEnabled = true;
  
                  // BuilderupdateButton.Visibility = Visibility.Visible;
              }
              else {
                  BillPaidDatedatePicker.IsEnabled = false;
              }
  
  
  
          }
          //Database
          public void ShowData()
          {
              SqlConnection conn;
  
              string connStr = ConfigurationManager.ConnectionStrings["house"].ConnectionString;
              conn = new SqlConnection(connStr);
  
              //SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
              conn.Open();
              SqlCommand comm = new SqlCommand("Select * from bill ORDER BY BillDueDate", conn);
              DataTable dt = new DataTable();
              SqlDataAdapter da = new SqlDataAdapter(comm);
              da.Fill(dt);
              lvwBooks.DataContext = dt.DefaultView;
          }
  
          private void YesButton_Click(object sender, RoutedEventArgs e)
          {
              Update();
              // YesButton Clicked! Let's hide our InputBox and handle the input text.
              InputBox.Visibility = System.Windows.Visibility.Collapsed;
          }
  
          public void Update() { 
              SqlConnection conn;
              SqlCommand cmdInsert;
              String strUpdate;
  
  
              string connStr = ConfigurationManager.ConnectionStrings["house"].ConnectionString;
              conn = new SqlConnection(connStr);
  
  
              strUpdate = "UPDATE bill SET BillName=@BillName, BillTypes=@BillTypes, BillDueDate=@BillDueDate, BillAmount=@BillAmount, BillStatus=@BillStatus, BillPaidDate=@BillPaidDate, BillDescription=@BillDescription WHERE BillNo='" + textBox1.Text + "' ";
  
                  cmdInsert = new SqlCommand(strUpdate, conn);
  
                  cmdInsert.Parameters.AddWithValue("@BillName", BillNameTxtBox.Text);
                  cmdInsert.Parameters.AddWithValue("@BillTypes", BillTypeCboBox.Text);
                  cmdInsert.Parameters.AddWithValue("@BillDueDate", DateTime.Parse(BillDueDatedatePicker.Text));
                  cmdInsert.Parameters.AddWithValue("@BillAmount", BillAmountTxtBox.Text);
                  cmdInsert.Parameters.AddWithValue("@BillStatus", BillStatusCboBox.Text);
                  //MessageBox.Show(BillPaidDatedatePicker.Text);
                  if (BillPaidDatedatePicker.Text.Equals(""))
                  {
                      cmdInsert.Parameters.AddWithValue("@BillPaidDate", "");
                  }
                  else {
                      cmdInsert.Parameters.AddWithValue("@BillPaidDate", DateTime.Parse(BillPaidDatedatePicker.Text));
                  }
                 // cmdInsert.Parameters.AddWithValue("@BillPaidDate", DateTime.Parse(empty));
                  cmdInsert.Parameters.AddWithValue("@BillDescription", BillDescriptionTxtBox.Text);
  
                  try
                  {
                      conn.Open();
                      int result = cmdInsert.ExecuteNonQuery();
                      if (result == 1)
                      {
                          MessageBox.Show("Bill Edited!!!");
  
                      }
                  }
                  catch (SqlException ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
                  finally {
                      conn.Close();
                  }
              }
      }
  }
  </code></pre>