daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shopping book and
  2. private void textBox1_KeyDown(object sender, KeyEventArgs e)
  3.     {
  4.         if (e.KeyCode == Keys.Return)
  5.         {
  6.             sql = new SqlConnection(@"Data Source=PC-PCPC;Initial Catalog=Anbar;Integrated Security=True");
  7.             adapter = new SqlDataAdapter("select * from Goods", sql);
  8.             cmd = new SqlCommand();
  9.             cmd.Connection = sql;
  10.             cmd.CommandText = ("Update Goods set Buy =@Buy, Remain =@Remain where GoodsNumber =@GoodsNumber");
  11.             cmd.Parameters.AddWithValue("@Buy", Convert.ToInt32(textBox1.Text));
  12.             cmd.Parameters.AddWithValue("@GoodsNumber", Convert.ToInt32(comboBox1.Text));
  13.             cmd.Parameters.AddWithValue("@Remain", Convert.ToInt32(comboBox3.Text) - Convert.ToInt32(textBox1.Text));
  14.             sql.Open();
  15.             cmd.ExecuteNonQuery();
  16.             sql.Close();
  17.             fill();
  18.         }
  19.     }
  20.    
  21. private void Form_Load(object sender, EventArgs e)
  22. {
  23.     productsComboBox.DataSource = GetAllProducts();
  24.     productsComboBox.DisplayMember = "Name";
  25.     productsComboBox.ValueMember = "Id";
  26. }
  27.  
  28. private IList<Product> GetAllProducts()
  29. {
  30.     List<Product> products = new List<Product>();
  31.  
  32.     // I use ConfigurationManager from System.Configuration.dll
  33.     // to read connection strings from App.config
  34.     string connectionString = ConfigurationManager.ConnectionStrings["anbar"].ConnectionString;
  35.  
  36.     using (SqlConnection conn = new SqlConnection(connectionString))
  37.     {
  38.         string query = "SELECT * FROM Products";
  39.         SqlCommand cmd = new SqlCommand(query, conn);
  40.         conn.Open();
  41.         SqlDataReader reader = cmd.ExecuteReader();
  42.  
  43.         while (reader.Read())
  44.         {
  45.             products.Add(new Product() { Id = (int)reader["Id"],
  46.                                          Name = (string)reader["Name"],
  47.                                          Quantity = (int)reader["Quantity"] });
  48.         }
  49.     }
  50.     return products;
  51. }
  52.    
  53. public class Product
  54. {
  55.     public int Id { get; set; }
  56.     public string Name { get; set; }
  57.     public int Quantity { get; set; }
  58. }
  59.    
  60. private void ProductsComboBox_SelectedIndexChanged(object sender, EventArgs e)
  61. {
  62.     Product product = productsComboBox.SelectedItem as Product;
  63.     qtyTextBox.Text = product.Quantity.ToString();
  64.     // I use NumericUpDown control to input numbers
  65.     // Minimum is set to 1
  66.     qtyToSellNumericUpDown.Maximum = product.Quantity;
  67. }
  68.    
  69. private void ButtonSell_Click(object sender, EventArgs e)
  70. {
  71.     Product product = productsComboBox.SelectedItem as Product;
  72.     int qtyToSell = (int)qtyToSellNumericUpDown.Value;
  73.     SellProduct(product, qtyToSell);
  74.     product.Quantity -= qtyToSell; // update product
  75.     qtyTextBox.Text = product.Quantity.ToString(); // update current quantity
  76. }
  77.  
  78. private void SellProduct(Product product, int qtyToSell)
  79. {
  80.     string connectionString = ConfigurationManager.ConnectionStrings["anbar"].ConnectionString;
  81.  
  82.     using (SqlConnection conn = new SqlConnection(connectionString))
  83.     {
  84.         string query = "UPDATE Products SET Quantity = @Quantity WHERE Id = @Id";
  85.         SqlCommand cmd = new SqlCommand(query, conn);
  86.         cmd.Parameters.AddWithValue("@Id", product.Id);
  87.         cmd.Parameters.AddWithValue("@Quantity", product.Quantity - qtyToSell);
  88.         conn.Open();
  89.         cmd.ExecuteNonQuery();
  90.     }
  91. }
  92.    
  93. <configuration>
  94.   <connectionStrings>
  95.     <add name="anbar"
  96.          connectionString="Data Source=PC-PCPC;Initial Catalog=Anbar;Integrated Security=True"                
  97.          providerName="System.Data.SqlClient"/>
  98.   </connectionStrings>
  99. </configuration>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top