siddiq shameer

siddiq shameer

  • 2.1k
  • 44
  • 1k

listview database bind not reflects back

Jul 24 2016 4:19 AM
i have listview bounded to a database.i have textboxes to update data in table.
 
but when i enter data it updates on listview.
if i close and open the application sometimes data may be there and sometimes not,
when i checked my database table,the data not updated there
 
here's my xaml :
 
  1. <Window x:Class="DatabaseApplication.Window1"  
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.     Title="Window1" Height="554" Width="1134" Loaded="Window_Loaded" Background="White">  
  5.     <Grid  Background="White">  
  6.   
  7.         <ListView Margin="8,9,738,284" Name="listView1" ItemsSource="{Binding}" MinWidth="250" MinHeight="100" IsSynchronizedWithCurrentItem="True">  
  8.             <ListView.View>  
  9.                 <GridView>  
  10.                     <GridViewColumn Header="Item Number" DisplayMemberBinding="{Binding Path=ItemNumber}"></GridViewColumn>  
  11.                     <GridViewColumn Header="Item Name" DisplayMemberBinding="{Binding Path=ItemName}"></GridViewColumn>  
  12.                     <GridViewColumn Header="Item Price" DisplayMemberBinding="{Binding Path=ItemPrice}"></GridViewColumn>  
  13.                     <GridViewColumn Header="Margin" DisplayMemberBinding="{Binding Path=ItemMargin}"></GridViewColumn>  
  14.                     <GridViewColumn Header="Stock" DisplayMemberBinding="{Binding Path=ItemStock}"></GridViewColumn>  
  15.                 </GridView>  
  16.             </ListView.View>  
  17.         </ListView>  
  18.         <TextBox Margin="92,238,738,252" Name="textBox1"  Height="23" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=ItemName}" />  
  19.         <TextBox Height="23" Margin="92,0,738,224" Name="textBox2" VerticalAlignment="Bottom" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=ItemPrice}" />  
  20.         <TextBox Height="23" HorizontalAlignment="Left" Margin="92,295,0,0" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=ItemMargin}" Name="textBox3" VerticalAlignment="Top" Width="282" />  
  21.         <TextBox Height="23" HorizontalAlignment="Left" Margin="92,326,0,0" Name="textBox4" VerticalAlignment="Top" Width="282" DataContext="{Binding ElementName=listView1,Path=SelectedItem}" Text="{Binding Path=ItemStock}"/>  
  22.   
  23.   
  24.         <Label Margin="8,236,1026,254" Name="label1"  Height="23" >Item Name :</Label>  
  25.         <Label Height="23" Margin="8,0,1030,224" Name="label2" VerticalAlignment="Bottom">Item Price :</Label>  
  26.         <Label Content="Margin:" Height="25" HorizontalAlignment="Left" Margin="8,295,0,0" Name="label3" VerticalAlignment="Top" />  
  27.         <Label Content="Stock:" Height="28" HorizontalAlignment="Left" Margin="8,321,0,0" Name="label4" VerticalAlignment="Top" Width="61" />  
  28.   
  29.   
  30.   
  31.         <Button Height="26" Margin="92,0,956,134" Name="btnAdd" VerticalAlignment="Bottom" Click="btnAdd_Click">Add</Button>  
  32.         <Button Height="26" Margin="234,0,0,134" Name="btnUpdate" VerticalAlignment="Bottom" Click="btnUpdate_Click" HorizontalAlignment="Left" Width="67">Update</Button>  
  33.         <Button Height="26" Margin="162,0,0,134" Name="btnDelete" VerticalAlignment="Bottom" Click="btnDelete_Click"  HorizontalAlignment="Left" Width="66" >Delete</Button>  
  34.         <Button Height="26" Margin="307,0,738,134" Name="btnClear" VerticalAlignment="Bottom" Click="btnClear_Click">Clear</Button>  
  35.   
  36.     </Grid>  
  37.   
  38.   
  39. </Window> 

here's my code:
 
  1. using System.Data.SqlClient;  
  2. using System.Data;  
  3.   
  4. namespace DatabaseApplication  
  5. {  
  6.     /// <summary>  
  7.     /// Interaction logic for Window1.xaml  
  8.     /// </summary>  
  9.     public partial class Window1 : Window  
  10.     {  
  11.         public Window1()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.   
  16.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  17.         {  
  18.             ShowData();  
  19.         }  
  20.         private void btnAdd_Click(object sender, RoutedEventArgs e)  
  21.         {  
  22.             string itemname = textBox1.Text;  
  23.             string itemprice = textBox2.Text;  
  24.             string itemmargin = textBox1.Text;  
  25.             string itemstock = textBox2.Text;  
  26.             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  27.             con.Open();  
  28.             SqlCommand comm = new SqlCommand("insert into stock(ItemName,ItemPrice,ItemMargin,ItemStock) values(@itemname,@itemprice,@itemmargin,@itemstock)", con);  
  29.             comm.Parameters.AddWithValue("@itemname", textBox1.Text);  
  30.             comm.Parameters.AddWithValue("@itemprice", textBox2.Text);  
  31.             comm.Parameters.AddWithValue("@itemmargin", textBox3.Text);  
  32.             comm.Parameters.AddWithValue("@itemstock", textBox4.Text);  
  33.             comm.ExecuteNonQuery();  
  34.             con.Close();  
  35.             ShowData();  
  36.         }  
  37.   
  38.         private void btnClear_Click(object sender, RoutedEventArgs e)  
  39.         {  
  40.             textBox1.Text = "";  
  41.             textBox2.Text = "";  
  42.             textBox3.Text = "";  
  43.             textBox4.Text = "";  
  44.         }  
  45.         public void ShowData()  
  46.         {  
  47.             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  48.             con.Open();  
  49.             SqlCommand comm = new SqlCommand("Select * from stock", con);  
  50.             DataTable dt = new DataTable();  
  51.             SqlDataAdapter da = new SqlDataAdapter(comm);  
  52.             da.Fill(dt);  
  53.             listView1.DataContext = dt.DefaultView;  
  54.         }  
  55.   
  56.         private void btnDelete_Click(object sender, RoutedEventArgs e)  
  57.         {  
  58.             if (listView1.SelectedItems.Count > 0)  
  59.             {  
  60.                 DataRowView drv = (DataRowView)listView1.SelectedItem;  
  61.                 string id = drv.Row[0].ToString();  
  62.                 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  63.                 con.Open();  
  64.                 SqlCommand comm = new SqlCommand("delete from stock where ItemNumber=@ItemNumber", con);  
  65.                 comm.Parameters.AddWithValue("@ItemNumber", id);  
  66.                 comm.ExecuteNonQuery();  
  67.                 ShowData();  
  68.             }  
  69.         }  
  70.   
  71.         private void btnUpdate_Click(object sender, RoutedEventArgs e)  
  72.         {  
  73.             if (listView1.SelectedItems.Count > 0)  
  74.             {  
  75.                 DataRowView drv = (DataRowView)listView1.SelectedItem;  
  76.                 string itemnumber = drv.Row[0].ToString();  
  77.                 SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  78.                 con.Open();  
  79.                 SqlCommand comm = new SqlCommand("update stock  set ItemName=@itemname,ItemPrice=@itemprice,ItemMargin=@itemmargin,ItemStock=@itemstock where ItemNumber=@itemnumber", con);  
  80.                 comm.Parameters.AddWithValue("@itemnumber",itemnumber );  
  81.                 comm.Parameters.AddWithValue("@itemname", textBox1.Text);  
  82.                 comm.Parameters.AddWithValue("@itemprice", textBox2.Text);  
  83.                 comm.Parameters.AddWithValue("@itemmargin",textBox3.Text);  
  84.                 comm.Parameters.AddWithValue("@itemstock",textBox4.Text);  
  85.                 comm.ExecuteNonQuery();  
  86.                 con.Close();  
  87.                 ShowData();  
  88.             }  
  89.               
  90.         }  
  91.   
  92.          
  93.     }  


 please give me a solution

Answers (2)