using System; using System.Data; using System.Collections.Generic; using System.ComponentModel; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.OleDb; namespace WindowsApplication1 { public partial class User : Form { // Constant strings private const string _CommandText = "SELECT UserName,Password,Name,Manager " + "FROM dbo.Users " + "ORDER BY UserName ,Password ";
// private const string _CommandText1 = //"SELECT Manager FROM dbo.Users WHERE (Manager = 1) ";
private const string _ConnectionString = "Data Source=sedgholi;Initial Catalog=pubs;Integrated Security=True"; // Declare global objects... SqlConnection objConnection; SqlDataAdapter objDataAdapter; DataSet objDataSet; DataView objDataView; CurrencyManager objCurrencyManager; public User() { objConnection = new SqlConnection(_ConnectionString); objDataAdapter = new SqlDataAdapter(_CommandText, objConnection); InitializeComponent(); } private void FillDataSetAndView() { // Initialize a new instance of the DataSet object... objDataSet = new DataSet(); // Fill the DataSet object with data... objDataAdapter.Fill(objDataSet,"Users"); // Set the DataView object to the DataSet object... objDataView = new DataView( objDataSet.Tables["Users"]); // Set our CurrencyManager object // to the DataView object... objCurrencyManager = (CurrencyManager)( this.BindingContext[objDataView]);
} private void BindFields() { // Clear any previous bindings... textBox1.DataBindings.Clear(); textBox2.DataBindings.Clear(); textBox3.DataBindings.Clear();
// Add new bindings to the DataView object... textBox1.DataBindings.Add("Text", objDataView, "UserName"); textBox2.DataBindings.Add("Text", objDataView, "Password"); textBox3.DataBindings.Add("Text", objDataView, "Name");
} private void ShowPosition() { addd.DataSource = objDataView; addd.Columns[0].HeaderText = "UserName"; addd.Columns[1].HeaderText="PassWord"; addd.Columns[2].HeaderText = "First And Last Name User"; addd.Columns[3].HeaderText = "Permission";
} private void User_Load_1(object sender, EventArgs e) {
addd.ColumnHeadersDefaultCellStyle.BackColor = Color.Pink; FillDataSetAndView(); BindFields(); ShowPosition(); addd.AllowUserToResizeRows = false; addd.AllowUserToResizeColumns = false;
} private void addd_CellClick(object sender, DataGridViewCellEventArgs e) { if (addd.CurrentRow.Cells[3].Value.ToString() == "0") radioButton1.Checked = true; else if (addd.CurrentRow.Cells[3].Value.ToString() == "1") radioButton2.Checked = true; }
private void Add_Click_1(object sender, EventArgs e) { SqlCommand objCommand = new SqlCommand(); objCommand.Connection = objConnection; objCommand.CommandType = CommandType.Text; if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || (radioButton1.Checked && radioButton2.Checked))
MessageBox.Show("????? ????? ????? ??? ?? ???? ??????", "???? ????", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
else {
objCommand.CommandText = "INSERT INTO [pubs].[dbo].[Users](UserName,Password,Name,Manager)" + "VALUES (@UserName ,@Password,@Name,@Manager)"; objCommand.Parameters.AddWithValue("@UserName", textBox1.Text); objCommand.Parameters.AddWithValue("@Password", textBox2.Text); objCommand.Parameters.AddWithValue("@Name", textBox3.Text); } if (radioButton1.Checked) objCommand.Parameters.AddWithValue("@Manager", 0); else if (radioButton2.Checked) { objCommand.Parameters.AddWithValue("@Manager", 1); } else { MessageBox.Show("??? ?????? ?? ?????? ??????", "?????? ??? ?????", MessageBoxButtons.OK); } try { objConnection.Open(); objCommand.ExecuteNonQuery(); } catch (SqlException SqlExceptionErr) { MessageBox.Show("??? ?????? ?????? ?? ????","???? ????", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning ); } objConnection.Close(); FillDataSetAndView(); BindFields(); addd.DataSource = objDataView; addd.AllowUserToResizeRows = false; addd.AllowUserToResizeColumns = false; } private void Remove_Click_1(object sender, EventArgs e) { if (MessageBox.Show("??? ?? ??? ????? ????? ????? ?????", "??? ?????", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { SqlCommand objCommand = new SqlCommand(); objConnection.Open(); objCommand.Connection = objConnection; objCommand.CommandType = CommandType.Text; objCommand.CommandText = "DELETE FROM Users WHERE UserName = @UserName"; // Parameter for the B_Isbm field... objCommand.Parameters.AddWithValue("@UserName", this.BindingContext[objDataView, "UserName"].Current); objCommand.ExecuteNonQuery(); objConnection.Close(); FillDataSetAndView(); BindFields(); addd.DataSource = objDataView; addd.AllowUserToResizeRows = false; addd.AllowUserToResizeColumns = false; MessageBox.Show("?? ????? ?? ?????? ??? ?????"); } else MessageBox.Show("??? ????? ??????"); } private void button1_Click(object sender, EventArgs e) { textBox1.DataBindings.Clear(); textBox2.DataBindings.Clear(); textBox3.DataBindings.Clear(); radioButton1.DataBindings.Clear(); radioButton2.DataBindings.Clear(); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; radioButton1.Checked = false; radioButton2.Checked = false; }
}
}
|