vakho

ADO.NET Amocana N1

Oct 24th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace AdoMarketDev
  12. {
  13.     public partial class MyForm : Form
  14.     {
  15.         // category list
  16.         private List<string> categoryList = new List<string>();
  17.  
  18.         // price
  19.         private int price;
  20.  
  21.         private SqlConnection con = new SqlConnection("Server=VAKHO-PC; Database=MarketDev; Integrated Security=true;");
  22.         private SqlDataAdapter dataAdapter = new SqlDataAdapter();
  23.         private DataSet dataSet = null;
  24.  
  25.         public MyForm()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         private void MyForm_Load(object sender, EventArgs e)
  31.         {
  32.             // display categories inside the combobox
  33.             this.updateCategoryList();
  34.             comboBox1.Items.Clear();
  35.             foreach (string name in categoryList)
  36.             {
  37.                 comboBox1.Items.Add(name);
  38.             }
  39.             comboBox1.SelectedIndex = 0;
  40.         }
  41.  
  42.         // refresh category list
  43.         private void button2_Click(object sender, EventArgs e)
  44.         {
  45.             this.updateCategoryList();
  46.             comboBox1.Items.Clear();
  47.             foreach (string name in categoryList)
  48.             {
  49.                 comboBox1.Items.Add(name);
  50.             }
  51.             comboBox1.SelectedIndex = 0;
  52.         }
  53.  
  54.         // update categoryList function
  55.         private void updateCategoryList()
  56.         {
  57.             categoryList.Clear(); // remove old data
  58.             con.Open();
  59.             SqlDataReader reader = new SqlCommand("SELECT * FROM Category", con).ExecuteReader();
  60.             while (reader.Read())
  61.             {
  62.                 categoryList.Add(reader["name"].ToString());
  63.             }
  64.             con.Close();
  65.         }
  66.  
  67.         private void buttonSelect_Click(object sender, EventArgs e)
  68.         {
  69.             string priceStr = textBox2.Text.Trim();
  70.             if (priceStr != "")
  71.             {
  72.                 try
  73.                 {
  74.                     price = Convert.ToInt32(priceStr); // may throw FormatException
  75.  
  76.                     // getting id by category name
  77.                     con.Open();
  78.                     int category_id = Convert.ToInt32(new SqlCommand("SELECT id FROM Category WHERE name=N'" + categoryList[comboBox1.SelectedIndex] + "'", con).ExecuteScalar());
  79.                     dataAdapter.SelectCommand = new SqlCommand("SELECT name, price FROM Product WHERE price < " + this.price + " AND category_id = " + category_id, con);
  80.                     MessageBox.Show(category_id.ToString());
  81.                     dataSet = new DataSet("Products");
  82.                     dataAdapter.Fill(dataSet, "Products");
  83.  
  84.                     // draw (redraw) dataGridView1
  85.                     dataGridView1.ClearSelection();
  86.                     dataGridView1.DataSource = dataSet.Tables["Products"];
  87.                 }
  88.                 catch (FormatException ex)
  89.                 {
  90.                     MessageBox.Show("Unsupported price format!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  91.                 }
  92.                 catch (SqlException ex)
  93.                 {
  94.                     MessageBox.Show("SQL Error!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  95.                 }
  96.                 finally
  97.                 {
  98.                     con.Close();
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 MessageBox.Show("The price box is empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  104.             }
  105.         }
  106.  
  107.         // add category button event
  108.         private void button1_Click(object sender, EventArgs e)
  109.         {
  110.             string name = textBox1.Text.Trim();
  111.             if (name != "")
  112.             {
  113.                 con.Open();
  114.                 new SqlCommand("INSERT INTO Category (name) VALUES (N'" + name + "')", con).ExecuteNonQuery();
  115.                 con.Close();
  116.  
  117.                 MessageBox.Show("New category has been added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  118.                
  119.                 // refresh categoryList and comboBox
  120.                 updateCategoryList();
  121.                 comboBox1.Items.Clear(); // empty comboBox
  122.                 foreach (string tmp_name in categoryList)
  123.                 {
  124.                     comboBox1.Items.Add(tmp_name);
  125.                 }
  126.                 comboBox1.SelectedIndex = 0;
  127.             }
  128.             else
  129.             {
  130.                 MessageBox.Show("Category name box is empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment