Advertisement
Nithisaran

Untitled

Aug 17th, 2020
1,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace WindowsFormsApp1
  14. {
  15.     public partial class Form5 : Form
  16.     {
  17.  
  18.         public static string strProvider = "server=localhost;Database=testDB;User IF=root;Password=";
  19.         public Form5()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void label1_Click(object sender, EventArgs e)
  25.         {
  26.  
  27.         }
  28.  
  29.         private void button2_Click(object sender, EventArgs e)
  30.         {
  31.             try
  32.             {
  33.                 if(textBox1.Text =="" || textBox2.Text =="")
  34.                 {
  35.                     MessageBox.Show("Null Data");
  36.                 } else
  37.                 {
  38.                     string strProvider = "server=localhost;Database=testDB;User ID=root;Password=";
  39.                     string Query = "INSERT INTO majorTbl(majorID,majorName) VALUE(" + textBox1.Text + "," + textBox2.Text + ")";
  40.                     MySqlConnection MyConn2 = new MySqlConnection(strProvider);
  41.                     MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
  42.  
  43.                     MySqlDataReader MyReader2;
  44.                     MyConn2.Open();
  45.                     MyReader2 = MyCommand2.ExecuteReader();
  46.                     MessageBox.Show("Save Data");
  47.                     MyConn2.Close();
  48.                     showdata();
  49.                 }
  50.  
  51.             }
  52.             catch (Exception ex)
  53.             {
  54.                 MessageBox.Show(ex.Message);
  55.             }
  56.  
  57.         }
  58.  
  59.         private void showdata()
  60.         {
  61.            
  62.             try
  63.             {
  64.                 string strProvider = "server=localhost;Database=testDB;User ID=root;Password=";
  65.                 string Query = "SELECT * FROM majorTbl";
  66.                 MySqlConnection MyConn2 = new MySqlConnection(strProvider);
  67.                 MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
  68.                 MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
  69.                 MyAdapter.SelectCommand = MyCommand2;
  70.                 DataTable dTable = new DataTable();
  71.                 MyAdapter.Fill(dTable);
  72.                 dataGridView1.DataSource = dTable;
  73.             }
  74.             catch (Exception ex)
  75.             {
  76.                 MessageBox.Show(ex.Message);
  77.             }
  78.         }
  79.  
  80.         private void Form5_Load(object sender, EventArgs e)
  81.         {
  82.             showdata();
  83.         }
  84.     }
  85.  
  86.  
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement