TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
siddiq shameer
NA
44
1.4k
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 :
<
Window
x:Class
=
"DatabaseApplication.Window1"
xmlns
=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
=
"http://schemas.microsoft.com/winfx/2006/xaml"
Title
=
"Window1"
Height
=
"554"
Width
=
"1134"
Loaded
=
"Window_Loaded"
Background
=
"White"
>
<
Grid
Background
=
"White"
>
<
ListView
Margin
=
"8,9,738,284"
Name
=
"listView1"
ItemsSource
=
"{Binding}"
MinWidth
=
"250"
MinHeight
=
"100"
IsSynchronizedWithCurrentItem
=
"True"
>
<
ListView.View
>
<
GridView
>
<
GridViewColumn
Header
=
"Item Number"
DisplayMemberBinding
=
"{Binding Path=ItemNumber}"
>
</
GridViewColumn
>
<
GridViewColumn
Header
=
"Item Name"
DisplayMemberBinding
=
"{Binding Path=ItemName}"
>
</
GridViewColumn
>
<
GridViewColumn
Header
=
"Item Price"
DisplayMemberBinding
=
"{Binding Path=ItemPrice}"
>
</
GridViewColumn
>
<
GridViewColumn
Header
=
"Margin"
DisplayMemberBinding
=
"{Binding Path=ItemMargin}"
>
</
GridViewColumn
>
<
GridViewColumn
Header
=
"Stock"
DisplayMemberBinding
=
"{Binding Path=ItemStock}"
>
</
GridViewColumn
>
</
GridView
>
</
ListView.View
>
</
ListView
>
<
TextBox
Margin
=
"92,238,738,252"
Name
=
"textBox1"
Height
=
"23"
DataContext
=
"{Binding ElementName=listView1,Path=SelectedItem}"
Text
=
"{Binding Path=ItemName}"
/>
<
TextBox
Height
=
"23"
Margin
=
"92,0,738,224"
Name
=
"textBox2"
VerticalAlignment
=
"Bottom"
DataContext
=
"{Binding ElementName=listView1,Path=SelectedItem}"
Text
=
"{Binding Path=ItemPrice}"
/>
<
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"
/>
<
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}"
/>
<
Label
Margin
=
"8,236,1026,254"
Name
=
"label1"
Height
=
"23"
>
Item Name :
</
Label
>
<
Label
Height
=
"23"
Margin
=
"8,0,1030,224"
Name
=
"label2"
VerticalAlignment
=
"Bottom"
>
Item Price :
</
Label
>
<
Label
Content
=
"Margin:"
Height
=
"25"
HorizontalAlignment
=
"Left"
Margin
=
"8,295,0,0"
Name
=
"label3"
VerticalAlignment
=
"Top"
/>
<
Label
Content
=
"Stock:"
Height
=
"28"
HorizontalAlignment
=
"Left"
Margin
=
"8,321,0,0"
Name
=
"label4"
VerticalAlignment
=
"Top"
Width
=
"61"
/>
<
Button
Height
=
"26"
Margin
=
"92,0,956,134"
Name
=
"btnAdd"
VerticalAlignment
=
"Bottom"
Click
=
"btnAdd_Click"
>
Add
</
Button
>
<
Button
Height
=
"26"
Margin
=
"234,0,0,134"
Name
=
"btnUpdate"
VerticalAlignment
=
"Bottom"
Click
=
"btnUpdate_Click"
HorizontalAlignment
=
"Left"
Width
=
"67"
>
Update
</
Button
>
<
Button
Height
=
"26"
Margin
=
"162,0,0,134"
Name
=
"btnDelete"
VerticalAlignment
=
"Bottom"
Click
=
"btnDelete_Click"
HorizontalAlignment
=
"Left"
Width
=
"66"
>
Delete
</
Button
>
<
Button
Height
=
"26"
Margin
=
"307,0,738,134"
Name
=
"btnClear"
VerticalAlignment
=
"Bottom"
Click
=
"btnClear_Click"
>
Clear
</
Button
>
</
Grid
>
</
Window
>
here's my code:
using
System.Data.SqlClient;
using
System.Data;
namespace
DatabaseApplication
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public
partial
class
Window1 : Window
{
public
Window1()
{
InitializeComponent();
}
private
void
Window_Loaded(
object
sender, RoutedEventArgs e)
{
ShowData();
}
private
void
btnAdd_Click(
object
sender, RoutedEventArgs e)
{
string
itemname = textBox1.Text;
string
itemprice = textBox2.Text;
string
itemmargin = textBox1.Text;
string
itemstock = textBox2.Text;
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
);
con.Open();
SqlCommand comm =
new
SqlCommand(
"insert into stock(ItemName,ItemPrice,ItemMargin,ItemStock) values(@itemname,@itemprice,@itemmargin,@itemstock)"
, con);
comm.Parameters.AddWithValue(
"@itemname"
, textBox1.Text);
comm.Parameters.AddWithValue(
"@itemprice"
, textBox2.Text);
comm.Parameters.AddWithValue(
"@itemmargin"
, textBox3.Text);
comm.Parameters.AddWithValue(
"@itemstock"
, textBox4.Text);
comm.ExecuteNonQuery();
con.Close();
ShowData();
}
private
void
btnClear_Click(
object
sender, RoutedEventArgs e)
{
textBox1.Text =
""
;
textBox2.Text =
""
;
textBox3.Text =
""
;
textBox4.Text =
""
;
}
public
void
ShowData()
{
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
);
con.Open();
SqlCommand comm =
new
SqlCommand(
"Select * from stock"
, con);
DataTable dt =
new
DataTable();
SqlDataAdapter da =
new
SqlDataAdapter(comm);
da.Fill(dt);
listView1.DataContext = dt.DefaultView;
}
private
void
btnDelete_Click(
object
sender, RoutedEventArgs e)
{
if
(listView1.SelectedItems.Count > 0)
{
DataRowView drv = (DataRowView)listView1.SelectedItem;
string
id = drv.Row[0].ToString();
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
);
con.Open();
SqlCommand comm =
new
SqlCommand(
"delete from stock where ItemNumber=@ItemNumber"
, con);
comm.Parameters.AddWithValue(
"@ItemNumber"
, id);
comm.ExecuteNonQuery();
ShowData();
}
}
private
void
btnUpdate_Click(
object
sender, RoutedEventArgs e)
{
if
(listView1.SelectedItems.Count > 0)
{
DataRowView drv = (DataRowView)listView1.SelectedItem;
string
itemnumber = drv.Row[0].ToString();
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
);
con.Open();
SqlCommand comm =
new
SqlCommand(
"update stock set ItemName=@itemname,ItemPrice=@itemprice,ItemMargin=@itemmargin,ItemStock=@itemstock where ItemNumber=@itemnumber"
, con);
comm.Parameters.AddWithValue(
"@itemnumber"
,itemnumber );
comm.Parameters.AddWithValue(
"@itemname"
, textBox1.Text);
comm.Parameters.AddWithValue(
"@itemprice"
, textBox2.Text);
comm.Parameters.AddWithValue(
"@itemmargin"
,textBox3.Text);
comm.Parameters.AddWithValue(
"@itemstock"
,textBox4.Text);
comm.ExecuteNonQuery();
con.Close();
ShowData();
}
}
}
}
please give me a solution
Reply
Answers (
2
)
What is new feature of SQL Server 2014 and 2016?
refresh data problem