Jahangir Khan

Jahangir Khan

  • NA
  • 27
  • 5.9k

Same Data recorded if entered many times !

May 15 2016 5:14 AM
Now this program works great. BUT data gets entered twice without any problem. same ID same name, address , and amount & year. 
 Any suggestion to fix this ? 
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Data.SqlClient;  
  4.   
  5. namespace dss  
  6. {  
  7.     public partial class Form1 : Form  
  8.   
  9.     {  
  10.         SqlConnection con = new SqlConnection("Data Source=JAKHAN-PC\\sqlexpress;Initial Catalog=JG_Test;Integrated Security=True");  
  11.         public Form1()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.   
  16.         private void btnSearch_Click(object sender, EventArgs e)  
  17.         {  
  18.             SqlDataReader reader;  
  19.             SqlCommand cmd = new SqlCommand();  
  20.             try  
  21.             {  
  22.                 string sql = "SELECT * FROM Members WHERE MemberId = '" + tbID.Text + "' ";  
  23.                 cmd.Connection = con;  
  24.                 cmd.CommandText = sql;  
  25.                 con.Open();  
  26.                 reader = cmd.ExecuteReader();  
  27.                 while (reader.Read())  
  28.                 {  
  29.                     tbID.Text = reader["MemberId"].ToString();  
  30.                     tbName.Text = reader["Name"].ToString();  
  31.                     tbMobile.Text = reader["Mobile"].ToString();  
  32.                     tbEmail.Text = reader["Email"].ToString();  
  33.                     tbAddress.Text = reader["Address"].ToString();  
  34.                 }  
  35.                 con.Close();  
  36.                 sql = "SELECT * FROM Payments WHERE MemberId  = '" + tbID.Text + "' ";  
  37.                 cmd.Connection = con;  
  38.                 cmd.CommandText = sql;  
  39.                 con.Open();  
  40.                 reader = cmd.ExecuteReader();  
  41.                 while (reader.Read())  
  42.                 {  
  43.                     tbID.Text = reader["MemberId"].ToString();  
  44.                     tbYear.Text = reader["Year"].ToString();  
  45.                     tbAmount.Text = reader["Amount"].ToString();  
  46.   
  47.                 }  
  48.                 con.Close();  
  49.                 if (string.IsNullOrWhiteSpace(tbID.Text))  
  50.                 {  
  51.                     MessageBox.Show("ID Required");  
  52.                 }  
  53.             }  
  54.             catch (Exception ex)  
  55.             {  
  56.                 MessageBox.Show(ex.Message.ToString());  
  57.             }  
  58.   
  59.         }  
  60.   
  61.         private void btnAdd_Click(object sender, EventArgs e)  
  62.         {  
  63.             {  
  64.                 con.Open();  
  65.                 string Sql = "INSERT INTO Members ( MemberId, Name, Address, Mobile, Email ) VALUES " + " (@Id, @name, @address, @mobile, @email)";  
  66.                 using (SqlCommand cmd = new SqlCommand(Sql, con))  
  67.                 {  
  68.                     cmd.CommandText = Sql;  
  69.                     cmd.Parameters.AddWithValue("@Id", tbID.Text);  
  70.                     cmd.Parameters.AddWithValue("@name", tbName.Text);  
  71.                     cmd.Parameters.AddWithValue("@address", tbAddress.Text);  
  72.                     cmd.Parameters.AddWithValue("@mobile", tbMobile.Text);  
  73.                     cmd.Parameters.AddWithValue("@email", tbEmail.Text);  
  74.                     cmd.ExecuteNonQuery();  
  75.   
  76.                     Sql = "INSERT INTO Payments (MemberId, [Year], [Amount] ) VALUES " + "(@id, @year, @amount)";  
  77.                     cmd.Parameters.Clear();  
  78.                     cmd.CommandText = Sql;  
  79.                     cmd.Parameters.AddWithValue("@Id", tbID.Text);  
  80.                     cmd.Parameters.AddWithValue("@year", tbYear.Text);  
  81.                     cmd.Parameters.AddWithValue("@amount", tbAmount.Text);  
  82.                     cmd.ExecuteNonQuery();  
  83.   
  84.                     MessageBox.Show("Data Added");  
  85.                     tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  86.                     con.Close();  
  87.                 }  
  88.             }  
  89.         }  
  90.   
  91.         private void btnUpdate_Click(object sender, EventArgs e)  
  92.         {  
  93.             try  
  94.             {  
  95.                 SqlCommand cmd = new SqlCommand();  
  96.                 string Sql = "UPDATE Members SET MemberId = '" + tbID.Text + "', Name = '" + tbName.Text + "', Address = '" + tbAddress.Text + "', Mobile = '" + tbMobile.Text + "', Email = '" + tbEmail.Text + "' WHERE MemberId = '" + tbID.Text + "' ";  
  97.                 cmd.CommandText = Sql;  
  98.                 cmd.Connection = con;  
  99.                 con.Open();  
  100.                 cmd.ExecuteNonQuery();  
  101.                 con.Close();  
  102.   
  103.                 Sql = "UPDATE Payments SET MemberId = '" + tbID.Text + "', Year = '" + tbYear.Text + "', Amount = '" + tbAmount.Text + "' WHERE MemberId = '" + tbID.Text + "' ";  
  104.                 cmd.CommandText = Sql;  
  105.                 cmd.Connection = con;  
  106.                 con.Open();  
  107.                 cmd.ExecuteNonQuery();  
  108.                 con.Close();  
  109.                 MessageBox.Show("Data Updated");  
  110.                 tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  111.                   
  112.             }  
  113.             catch (Exception error)  
  114.             {  
  115.                 MessageBox.Show(error.ToString());  
  116.             }  
  117.         }  
  118.   
  119.         private void btnDelete_Click(object sender, EventArgs e)  
  120.         {  
  121.             try  
  122.             {  
  123.                 SqlCommand cmd = new SqlCommand();  
  124.                 string Sql = "DELETE FROM Members WHERE MemberId = '" + tbID.Text + "' ";  
  125.                 cmd.CommandText = Sql;  
  126.                 cmd.Connection = con;  
  127.                 con.Open();  
  128.                 cmd.ExecuteNonQuery();  
  129.                 con.Close();  
  130.   
  131.                 Sql = "DELETE  FROM Payments WHERE MemberId = '" + tbID.Text + "' ";  
  132.                 cmd.CommandText = Sql;  
  133.                 cmd.Connection = con;  
  134.                 con.Open();  
  135.                 cmd.ExecuteNonQuery();  
  136.                 tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  137.                 MessageBox.Show("Data Deleted");  
  138.                   
  139.                 con.Close();  
  140.             }  
  141.             catch (Exception error)  
  142.             {  
  143.                 MessageBox.Show(error.ToString());  
  144.             }  
  145.   
  146.         }  
  147.   
  148.         private void btnReset_Click(object sender, EventArgs e)  
  149.         {  
  150.              tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  151.         }  
  152.   
  153.         private void btnExit_Click(object sender, EventArgs e)  
  154.         {  
  155.             Application.Exit();  
  156.         }  
  157.     }  
  158. }  
 

Answers (8)