Dear All,
Below is login form and code form,
but i am not able to active add / update / delete button as per role, i am trying to pass role from form3 to form1,
login form :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
namespace PhoneBook
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("Server=xxx.xxx.xxx.xxx;port=xxxx;Database = xxxx; Uid =xxxx;Password =xxxx;connection Timeout=60");
MySqlDataAdapter sda = new MySqlDataAdapter("Select Role From LOGIN2 where USERNAME = '" + textBox1.Text + "' and PASSWORD = '" + textBox2.Text + "'", connection);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
this.Hide();
Form1 ss = new Form1(dt.Rows[0][0].ToString());
ss.Show();
}
else
{
MessageBox.Show("please check you user name and password");
}
}
}
}
Code Form :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
using System.Drawing.Imaging;
namespace PhoneBook
{
public partial class Form1 : Form
{
MySqlConnection connection = new MySqlConnection("Server=xxxxx;port=xxx;Database =xxxxx; Uid =xxxx;Password =xxx;connection Timeout=60");
MySqlCommand command;
MySqlDataAdapter da;
DataTable dt;
DataSet ds = new DataSet();
public Form1(string Role)
{
InitializeComponent();
label9.Text = Role;
textBox4.Visible = false;
this.BtnDelete.Enabled = false;
this.BtnUpdate.Enabled = false;
this.BtnSave.Enabled = false;
}
private void label3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
connection.Open();
da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);
dt = new DataTable();
da.Fill(dt);
dataGridView1.RowTemplate.Height = 60;
dataGridView1.DataSource = dt;
DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
imgCol = (DataGridViewImageColumn)dataGridView1.Columns[7];
imgCol.ImageLayout = DataGridViewImageCellLayout.Stretch;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
label9.Text = "logged as " + this.Controls["label9"].Text;
if(this.Controls["label9"].Text == "Admin")
{
this.BtnDelete.Enabled = true;
this.BtnUpdate.Enabled = true;
this.BtnSave.Enabled = true;
}
if (this.Controls["label9"].Text == "Client")
{
BtnDelete.Enabled = false;
BtnUpdate.Enabled = false;
BtnSave.Enabled = false;
}
if (this.Controls["label9"].Text == "Cust")
{
this.BtnDelete.Enabled = false;
this.BtnUpdate.Enabled = false;
this.BtnSave.Enabled = true;
}
connection.Close();
}
private void BtnSave_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Please Enter Details");
}
else
{
command = new MySqlCommand("SELECT * FROM TblPhoneBook WHERE MobileNo = '" + textBox2.Text + "' ", connection);
MySqlDataAdapter da = new MySqlDataAdapter(command);
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i > 0)
{
MessageBox.Show("mobile no " + textBox2.Text + " Already Exist");
ds.Clear();
}
else
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
connection.Open();
command = new MySqlCommand("INSERT INTO Fakhri2(CustName, InvNo, Dtd, Amt, Tax, Desc1, PIC)VALUES( @cust, @inv,@dtd, @amt, @tax, @des, @img)", connection);
command.Parameters.Add("@cust", MySqlDbType.VarChar).Value = textBox1.Text;
command.Parameters.Add("@inv", MySqlDbType.VarChar).Value = textBox2.Text;
command.Parameters.Add("@dtd", MySqlDbType.VarChar).Value = dateTimePicker1.Text;
command.Parameters.Add("@amt", MySqlDbType.VarChar).Value = textBox5.Text;
command.Parameters.Add("@tax", MySqlDbType.VarChar).Value = textBox6.Text;
command.Parameters.Add("@des", MySqlDbType.VarChar).Value = textBox7.Text;
command.Parameters.Add("@img", MySqlDbType.LongBlob).Value = img;
command.ExecuteNonQuery();
da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Customer Record ADDED");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
textBox1.Text = "";
textBox2.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
private void BtnUpdate_Click(object sender, EventArgs e)
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
connection.Open();
command = new MySqlCommand("UPDATE Fakhri2 SET CustName=@cust,InvNo=@inv,Dtd=@dtd,Amt=@amt,Tax=@tax,Desc1 =@des,PIC=@img WHERE SlNo = @SNo", connection);
command.Parameters.Add("@cust", MySqlDbType.VarChar).Value = textBox1.Text;
command.Parameters.Add("@SNo", MySqlDbType.VarChar).Value = textBox4.Text;
command.Parameters.Add("@inv", MySqlDbType.VarChar).Value = textBox2.Text;
command.Parameters.Add("@dtd", MySqlDbType.VarChar).Value = dateTimePicker1.Text;
command.Parameters.Add("@amt", MySqlDbType.VarChar).Value = textBox5.Text;
command.Parameters.Add("@tax", MySqlDbType.VarChar).Value = textBox6.Text;
command.Parameters.Add("@des", MySqlDbType.VarChar).Value = textBox7.Text;
command.Parameters.Add("@img", MySqlDbType.LongBlob).Value = img;
command.ExecuteNonQuery();
da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Contact list update....");
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
textBox1.Text = "";
textBox2.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
int i;
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
i = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[i];
textBox1.Text = row.Cells[1].Value.ToString();
textBox2.Text = row.Cells[2].Value.ToString();
textBox4.Text = row.Cells[0].Value.ToString();
dateTimePicker1.Text = row.Cells[3].Value.ToString();
textBox5.Text = row.Cells[4].Value.ToString();
textBox6.Text = row.Cells[5].Value.ToString();
textBox7.Text = row.Cells[6].Value.ToString();
Byte[] img = (Byte[])dataGridView1.CurrentRow.Cells[7].Value;
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = Image.FromStream(ms);
}
private void BtnDelete_Click(object sender, EventArgs e)
{
try
{
connection.Open();
command = new MySqlCommand("Delete FROM Fakhri2 WHERE SlNo = '" + textBox4.Text + "' ", connection);
command.ExecuteNonQuery();
da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Contact...DELETED...");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
textBox1.Text = "";
textBox2.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "CustName like '%" + textBox3.Text + "%'";
dataGridView1.DataSource = bs;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void BTN_CHOOSE_IMAGE_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.JPG;*.PNG;*.PDF)|*.jpg;*.png;*.pdf";
if (opf.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opf.FileName);
}
}
private void label9_Click(object sender, EventArgs e)
{
}
}
}
What I have tried:
i tried in to pass value but did not work