Hi Guys,
Hope you can help me with this.
I have created a very simple program using WPF
in Microsoft Visual Studio 2013 Express, to Display , Update and delete data.
It works when compiled.
But never commit the changes made to the DB from my program.
Can someone help?????????
Here is the code
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
namespace myWPFusers
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public SqlConnection ConStringOpen()
{
SqlConnection con = new SqlConnection(@"Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\dbUsers.mdf;Integrated Security=True;User Instance=False;Context Connection=False ");
con.Open();
return con;
}
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ShowData();
}
public static int TryToParse(string value)
{
int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
//Console.WriteLine("Converted '{0}' to {1}.", value, number);
return number;
}
else
{
if (value == null) value = "";
//Console.WriteLine("Attempted conversion of '{0}' failed.", value);
return 0;
}
}
public void ShowData()
{
//SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbUsers.mdf;Integrated Security=True;User Instance=True");
//SqlConnection con = new SqlConnection(@"Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\dbUsers.mdf;Integrated Security=True;User Instance=False;Context Connection=False ");
//con.Open();
SqlConnection conn = ConStringOpen();
SqlCommand comm = new SqlCommand("Select * from UserData", conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
listView1.DataContext = dt.DefaultView;
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
int Id = TryToParse(textBox3.Text);
if (Id == 0 )
{
MessageBox.Show("Enter a Value to ID");
MessageBox.Show(textBox3.Text);
}
else{
MessageBox.Show("Has Value");
MessageBox.Show(textBox3.Text);
string Username = textBox1.Text;
string password = textBox2.Text;
// SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
//SqlConnection con = new SqlConnection(@"Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\dbUsers.mdf;Integrated Security=True;User Instance=False;Context Connection=False ");
//con.Open();
SqlConnection conn2 = ConStringOpen();
SqlCommand comm = new SqlCommand("insert into UserData(Id,Username,Password) values(@id,@username,@password)", conn2);
//SqlCommand comm = new SqlCommand("insert into UserData(Username,Password) values(@username,@password)", con);
comm.Parameters.AddWithValue("@id", Id);
comm.Parameters.AddWithValue("@username", textBox1.Text);
comm.Parameters.AddWithValue("@password", textBox2.Text);
comm.ExecuteNonQuery();
//comm.ExecuteNonQueryAsync();
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(comm);
da1.Update(dt1);
conn2.Close();
ShowData();
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
//MessageBox.Show("Cleaniing");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
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= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\dbUsers.mdf;Integrated Security=True;User Instance=False;Context Connection=False ");
//con.Open();
SqlConnection conn3 = ConStringOpen();
SqlCommand comm = new SqlCommand("delete from UserData where Id=@id", conn3);
comm.Parameters.AddWithValue("@id", id);
comm.ExecuteNonQuery();
DataTable dt2 = new DataTable();
SqlDataAdapter da2 = new SqlDataAdapter(comm);
da2.Update(dt2);
conn3.Close();
ShowData();
}
}
private void btnUpdate_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= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\dbUsers.mdf;Integrated Security=True;User Instance=False;Context Connection=False ");
con.Open();
SqlCommand comm = new SqlCommand("update UserData set username=@username,password=@password where Id=@id", con);
comm.Parameters.AddWithValue("@id", id);
comm.Parameters.AddWithValue("@username", textBox1.Text);
comm.Parameters.AddWithValue("@password", textBox2.Text);
comm.ExecuteNonQuery();
con.Close();
ShowData();
}
}
}
}