Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace ADO_Project
- {
- public partial class myForm : Form
- {
- private string connectionString = "Server=VAKHO-PC; Database=TestDB; Integrated Security=true;";
- private SqlConnection connection = null;
- private SqlDataAdapter dataAdapter = null;
- private DataSet dataSet = null; // in-memory cache of data
- // create and open connection
- public myForm()
- {
- InitializeComponent();
- connection = new SqlConnection(this.connectionString);
- try
- {
- connection.Open();
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.Message, "Connection Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- // close the connection when we exit our form
- private void myForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- try
- {
- connection.Close();
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.ToString(), "Connection Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- // checking connection state
- private void buttonState_Click(object sender, EventArgs e)
- {
- MessageBox.Show(connection.State.ToString(), "Connection State", MessageBoxButtons.OK);
- }
- private void myForm_Load(object sender, EventArgs e)
- {
- // setting select command inside the constructor
- dataAdapter = new SqlDataAdapter("SELECT * FROM dbo.Users", connection);
- // update command for adapter
- dataAdapter.UpdateCommand = new SqlCommand("UPDATE dbo.Users SET username = @username WHERE id = @id", connection);
- dataAdapter.UpdateCommand.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar));
- dataAdapter.UpdateCommand.Parameters["@username"].SourceColumn = "username";
- dataAdapter.UpdateCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
- dataAdapter.UpdateCommand.Parameters["@id"].SourceColumn = "id";
- dataSet = new DataSet("People"); // dataSet with dataSetName "People"
- dataAdapter.Fill(dataSet, "People"); // fill "People" table with rows (from start to end)
- dataGridView1.DataSource = dataSet.Tables["People"];
- // other stuff
- dataGridView1.Columns[0].ReadOnly = true;
- }
- private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
- {
- dataGridView1.BeginEdit(true);
- }
- private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
- {
- dataGridView1.EndEdit();
- dataAdapter.Update(dataSet, "People");
- }
- private void buttonAdd_Click(object sender, EventArgs e)
- {
- string username = textBox1.Text.Trim();
- if (username != "")
- {
- try
- {
- // add new user
- SqlCommand sqlCommand = new SqlCommand("INSERT INTO dbo.Users(username) VALUES ('" + textBox1.Text + "')", connection);
- sqlCommand.ExecuteNonQuery();
- dataAdapter.SelectCommand.ExecuteNonQuery(); // fetch new results from db
- dataSet.Clear(); // clear old dataSet
- dataAdapter.Fill(dataSet, "People"); // fill results into dataSet's "People" table
- // refresh dataGridView1
- dataGridView1.ClearSelection();
- dataGridView1.DataSource = dataSet.Tables["People"];
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error while inserting new row!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- else
- {
- MessageBox.Show("Please enter at least one symbol!", "Empty Field Detected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment