vakho

ADO.NET DataGridView Update & Insert

Oct 24th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 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 ADO_Project
  12. {
  13.     public partial class myForm : Form
  14.     {
  15.         private string connectionString = "Server=VAKHO-PC; Database=TestDB; Integrated Security=true;";
  16.         private SqlConnection connection = null;
  17.  
  18.         private SqlDataAdapter dataAdapter = null;
  19.         private DataSet dataSet = null; // in-memory cache of data
  20.  
  21.         // create and open connection
  22.         public myForm()
  23.         {
  24.             InitializeComponent();
  25.  
  26.             connection = new SqlConnection(this.connectionString);
  27.             try
  28.             {
  29.                 connection.Open();
  30.             }
  31.             catch (SqlException ex)
  32.             {
  33.                 MessageBox.Show(ex.Message, "Connection Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  34.             }
  35.         }
  36.  
  37.         // close the connection when we exit our form
  38.         private void myForm_FormClosing(object sender, FormClosingEventArgs e)
  39.         {
  40.             try
  41.             {
  42.                 connection.Close();
  43.             }
  44.             catch (SqlException ex)
  45.             {
  46.                 MessageBox.Show(ex.ToString(), "Connection Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  47.             }
  48.         }
  49.  
  50.         // checking connection state
  51.         private void buttonState_Click(object sender, EventArgs e)
  52.         {
  53.             MessageBox.Show(connection.State.ToString(), "Connection State", MessageBoxButtons.OK);
  54.         }
  55.  
  56.         private void myForm_Load(object sender, EventArgs e)
  57.         {
  58.             // setting select command inside the constructor
  59.             dataAdapter = new SqlDataAdapter("SELECT * FROM dbo.Users", connection);
  60.  
  61.             // update command for adapter
  62.             dataAdapter.UpdateCommand = new SqlCommand("UPDATE dbo.Users SET username = @username WHERE id = @id", connection);
  63.             dataAdapter.UpdateCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar));
  64.             dataAdapter.UpdateCommand.Parameters["@username"].SourceColumn = "username";
  65.             dataAdapter.UpdateCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
  66.             dataAdapter.UpdateCommand.Parameters["@id"].SourceColumn = "id";
  67.  
  68.             dataSet = new DataSet("People"); // dataSet with dataSetName "People"
  69.             dataAdapter.Fill(dataSet, "People"); // fill "People" table with rows (from start to end)
  70.  
  71.             dataGridView1.DataSource = dataSet.Tables["People"];
  72.  
  73.             // other stuff
  74.             dataGridView1.Columns[0].ReadOnly = true;
  75.         }
  76.  
  77.         private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
  78.         {
  79.             dataGridView1.BeginEdit(true);
  80.         }
  81.  
  82.         private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
  83.         {
  84.             dataGridView1.EndEdit();
  85.             dataAdapter.Update(dataSet, "People");
  86.         }
  87.  
  88.         private void buttonAdd_Click(object sender, EventArgs e)
  89.         {
  90.             string username = textBox1.Text.Trim();
  91.             if (username != "")
  92.             {
  93.                 try
  94.                 {
  95.                     // add new user
  96.                     SqlCommand sqlCommand = new SqlCommand("INSERT INTO dbo.Users(username) VALUES ('" + textBox1.Text + "')", connection);
  97.                     sqlCommand.ExecuteNonQuery();
  98.  
  99.                     dataAdapter.SelectCommand.ExecuteNonQuery(); // fetch new results from db
  100.                     dataSet.Clear(); // clear old dataSet
  101.                     dataAdapter.Fill(dataSet, "People"); // fill results into dataSet's "People" table
  102.  
  103.                     // refresh dataGridView1
  104.                     dataGridView1.ClearSelection();
  105.                     dataGridView1.DataSource = dataSet.Tables["People"];
  106.                 }
  107.                 catch (Exception ex)
  108.                 {
  109.                     MessageBox.Show(ex.Message, "Error while inserting new row!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  110.                 }
  111.             }
  112.             else
  113.             {
  114.                 MessageBox.Show("Please enter at least one symbol!", "Empty Field Detected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment