Jahangir Khan

Jahangir Khan

  • NA
  • 27
  • 5.8k

technical question about the internal functions

May 14 2016 2:59 AM
Hello, 
 
Here is a demo of a program doing what it should do "Add", "Update","Delete" data. 
  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 + "' WHERE MemberId = '" + tbID.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 + "' WHERE MemberId = '" + tbID.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.             try  
  120.             {  
  121.                 SqlCommand cmd = new SqlCommand();  
  122.                 string Sql = "DELETE FROM Members WHERE MemberId = '" + tbID.Text + "' ";  
  123.                 cmd.CommandText = Sql;  
  124.                 cmd.Connection = con;  
  125.                 con.Open();  
  126.                 cmd.ExecuteNonQuery();  
  127.                 con.Close();  
  128.   
  129.                 Sql = "DELETE  FROM Payments WHERE MemberId = '" + tbID.Text + "' ";  
  130.                 cmd.CommandText = Sql;  
  131.                 cmd.Connection = con;  
  132.                 con.Open();  
  133.                 cmd.ExecuteNonQuery();  
  134.                 tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  135.                 MessageBox.Show("Data Deleted");  
  136.                   
  137.                 con.Close();  
  138.             }  
  139.             catch (Exception error)  
  140.             {  
  141.                 MessageBox.Show(error.ToString());  
  142.             }  
  143.   
  144.         }  
  145.   
  146.         private void btnReset_Click(object sender, EventArgs e)  
  147.         {  
  148.              tbID.Clear(); tbName.Clear(); tbAddress.Clear(); tbMobile.Clear(); tbEmail.Clear(); tbYear.Clear(); tbAmount.Clear();  
  149.         }  
  150.   
  151.   
  152.         private void btnExit_Click(object sender, EventArgs e)  
  153.         {  
  154.             Application.Exit();  
  155.         }  
  156.     }  
  157. }  
My question is "WHY" I have to declare the connection twice in line 11 & 12, ( remeber it's a learning and uderstanding process ) is it wrong to have it declared twice or is it correct ? or there is a more simple short and smarter way to do it ?  

Answers (1)