2
Answers

Update function problem !

Jahangir Khan

Jahangir Khan

8y
336
1
When I call some values and update them,  then all the values which already existed (already present)  in the database change to this update. 
 
I have 2 tables "Members" and "Payments" and I have 8 different members and there details in the database, I call John Doe's data and change address and update it.
It successfully updates it. 
 
When I check the database all the 8 different members all converted to John Doe and the values I changed.
 
I am quite confused ! 
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Data.SqlClient;  
  4. using System.Data;  
  5.   
  6. namespace dss  
  7. {  
  8.     public partial class Form1 : Form  
  9.   
  10.     {  
  11.         SqlConnection con = new SqlConnection("Data Source=JAKHAN-PC\\sqlexpress;Initial Catalog=JG_Test;Integrated Security=True");  
  12.         string connString = "Data Source=JAKHAN-PC\\sqlexpress;Initial Catalog=JG_Test;Integrated Security=True";  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void btnSearch_Click(object sender, EventArgs e)  
  19.         {  
  20.             SqlDataReader reader;  
  21.             SqlCommand cmd = new SqlCommand();  
  22.             try  
  23.             {  
  24.                 string sql = "SELECT * FROM Members WHERE MemberId = '"+ tbID.Text +"' ";  
  25.                 cmd.Connection = con;  
  26.                 cmd.CommandText = sql;  
  27.                 con.Open();  
  28.                 reader = cmd.ExecuteReader();  
  29.                 while (reader.Read())  
  30.                 {  
  31.                     tbID.Text = reader["MemberId"].ToString();  
  32.                     tbName.Text = reader["Name"].ToString();  
  33.                     tbMobile.Text = reader["Mobile"].ToString();  
  34.                     tbEmail.Text = reader["Email"].ToString();  
  35.                     tbAddress.Text = reader["Address"].ToString();  
  36.                 }  
  37.                 con.Close();  
  38.                 sql= "SELECT * FROM Payments WHERE MemberId  = '" + tbID.Text + "' ";  
  39.                 cmd.Connection = con;  
  40.                 cmd.CommandText = sql;  
  41.                 con.Open();  
  42.                 reader = cmd.ExecuteReader();  
  43.                 while (reader.Read())  
  44.                 {  
  45.                     tbID.Text = reader["MemberId"].ToString();  
  46.                     tbYear.Text = reader["Year"].ToString();  
  47.                     tbAmount.Text = reader["Amount"].ToString();  
  48.   
  49.                 }  
  50.                 con.Close();  
  51.             }  
  52.             catch (Exception ex)  
  53.             {  
  54.                 MessageBox.Show(ex.Message.ToString());  
  55.             }  
  56.         }  
  57.   
  58.         private void btnAdd_Click(object sender, EventArgs e)  
  59.         {  
  60.             using (SqlConnection con = new SqlConnection(connString))  
  61.             {  
  62.                 con.Open();  
  63.                 string Sql = "INSERT INTO Members ( MemberId, Name, Address, Mobile, Email ) VALUES " + " (@Id, @name, @address, @mobile, @email)";  
  64.                 using (SqlCommand cmd = new SqlCommand(Sql, con))  
  65.                 {  
  66.                     cmd.CommandText = Sql;  
  67.                     cmd.Parameters.AddWithValue("@Id", tbID.Text);  
  68.                     cmd.Parameters.AddWithValue("@name", tbName.Text);  
  69.                     cmd.Parameters.AddWithValue("@address", tbAddress.Text);  
  70.                     cmd.Parameters.AddWithValue("@mobile", tbMobile.Text);  
  71.                     cmd.Parameters.AddWithValue("@email", tbEmail.Text);  
  72.                     cmd.ExecuteNonQuery();  
  73.   
  74.                     Sql = "INSERT INTO Payments (MemberId, [Year], [Amount] ) VALUES " + "(@id, @year, @amount)";  
  75.                     cmd.Parameters.Clear();  
  76.                     cmd.CommandText = Sql;   
  77.                     cmd.Parameters.AddWithValue("@Id", tbID.Text);  
  78.                     cmd.Parameters.AddWithValue("@year", tbYear.Text);  
  79.                     cmd.Parameters.AddWithValue("@amount", tbAmount.Text);  
  80.                     cmd.ExecuteNonQuery();  
  81.   
  82.                     MessageBox.Show("Data Added");  
  83.                     tbID.Clear();tbName.Clear();tbAddress.Clear();tbMobile.Clear();tbEmail.Clear();tbYear.Clear();tbAmount.Clear();  
  84.                     con.Close();  
  85.                 }  
  86.             }  
  87.         }  
  88.   
  89.         private void btnUpdate_Click(object sender, EventArgs e)  
  90.         {  
  91.             try  
  92.             {  
  93.                 SqlCommand cmd = new SqlCommand();  
  94.                 string Sql = "UPDATE Members SET MemberId = '" + tbID.Text +"', Name = '" + tbName.Text + "', Address = '" + tbAddress.Text + "', Mobile = '" + tbMobile.Text + "', Email = '" + tbEmail.Text + "'";  
  95.                 cmd.CommandText = Sql;  
  96.                 cmd.Connection = con;  
  97.                 con.Open();  
  98.                 cmd.ExecuteNonQuery();  
  99.                 con.Close();  
  100.   
  101.                 Sql = "UPDATE Payments SET MemberId = '" + tbID.Text + "', Year = '" + tbYear.Text + "', Amount = '" + tbAmount.Text + "'";  
  102.                 cmd.CommandText = Sql;  
  103.                 cmd.Connection = con;  
  104.                 con.Open();  
  105.                 cmd.ExecuteNonQuery();  
  106.   
  107.                 MessageBox.Show("Data Updated");  
  108.                 tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  109.                 con.Close();  
  110.             }  
  111.             catch (Exception error)  
  112.             {  
  113.                 MessageBox.Show(error.ToString());  
  114.             }  
  115.         }  
  116.   
  117.         private void btnDelete_Click(object sender, EventArgs e)  
  118.         {  
  119.               
  120.             //tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  121.         }  
  122.   
  123.         private void btnReset_Click(object sender, EventArgs e)  
  124.         {  
  125.              tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  126.         }  
  127.   
  128.   
  129.         private void btnExit_Click(object sender, EventArgs e)  
  130.         {  
  131.             Application.Exit();  
  132.         }  
  133.     }  
  134. }  
Answers (2)
1
Pankaj  Kumar Choudhary

Pankaj Kumar Choudhary

NA 29.8k 3.1m 8y
Hello! JG Reason behind is If you don't provide the Where condition in update command then all values in table will update So provide some condition suing the where clause then try to update the data....
You can selected any attribute or combination of atttributes that make each record unique in table.
Accepted
1
Bhuvanesh Mohankumar

Bhuvanesh Mohankumar

NA 14.4k 1.3m 8y
Pankaj is right.
Query Sample:
Without Where:
string Sql = "UPDATE Members SET MemberId = '" + tbID.Text +"', Name = '" + tbName.Text + "', Address = '" + tbAddress.Text + "', Mobile = '" + tbMobile.Text + "', Email = '" + tbEmail.Text + "'";
With Where Clause:
string Sql = "UPDATE Members SET MemberId = '" + tbID.Text +"', Name = '" + tbName.Text + "', Address = '" + tbAddress.Text + "', Mobile = '" + tbMobile.Text + "', Email = '" + tbEmail.Text + "'" + "WHERE MemberId = '" + tbID.Text + "' ";