juzar para

juzar para

  • NA
  • 51
  • 2.6k

not able to activate / enable as per role

Jun 22 2018 6:41 AM
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 :
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.ComponentModel;  
  8. using System.Data;  
  9. using System.Drawing;  
  10. using System.Linq;  
  11. using System.Text;  
  12. using System.Threading.Tasks;  
  13. using System.Windows.Forms;  
  14. using System.IO;  
  15. using MySql.Data.MySqlClient;  
  16.   
  17. namespace PhoneBook  
  18. {  
  19. public partial class Form3 : Form  
  20. {  
  21. public Form3()  
  22. {  
  23. InitializeComponent();  
  24. }  
  25. private void Form3_Load(object sender, EventArgs e)  
  26. {  
  27. }  
  28. private void button1_Click(object sender, EventArgs e)  
  29. {  
  30. this.Close();  
  31. }  
  32. private void button2_Click(object sender, EventArgs e)  
  33. {  
  34. MySqlConnection connection = new MySqlConnection("Server=xxx.xxx.xxx.xxx;port=xxxx;Database = xxxx; Uid =xxxx;Password =xxxx;connection Timeout=60");  
  35. MySqlDataAdapter sda = new MySqlDataAdapter("Select Role From LOGIN2 where USERNAME = '" + textBox1.Text + "' and PASSWORD = '" + textBox2.Text + "'", connection);  
  36. DataTable dt = new DataTable();  
  37. sda.Fill(dt);  
  38. if (dt.Rows.Count == 1)  
  39. {  
  40. this.Hide();  
  41. Form1 ss = new Form1(dt.Rows[0][0].ToString());  
  42. ss.Show();  
  43. }  
  44. else  
  45. {  
  46. MessageBox.Show("please check you user name and password");  
  47. }  
  48. }  
  49. }  
  50. }  
Code Form :
  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.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using MySql.Data.MySqlClient;  
  11. using System.IO;  
  12. using System.Drawing.Imaging;  
  13. // this will be used for pic programming  
  14. namespace PhoneBook  
  15. {  
  16. public partial class Form1 : Form  
  17. {  
  18. MySqlConnection connection = new MySqlConnection("Server=xxxxx;port=xxx;Database =xxxxx; Uid =xxxx;Password =xxx;connection Timeout=60");  
  19. MySqlCommand command;  
  20. MySqlDataAdapter da;  
  21. DataTable dt;  
  22. DataSet ds = new DataSet();  
  23. public Form1(string Role)  
  24. {  
  25. InitializeComponent();  
  26. label9.Text = Role;  
  27. textBox4.Visible = false;  
  28. this.BtnDelete.Enabled = false;  
  29. this.BtnUpdate.Enabled = false;  
  30. this.BtnSave.Enabled = false;  
  31. }  
  32. private void label3_Click(object sender, EventArgs e)  
  33. {  
  34. }  
  35. private void Form1_Load(object sender, EventArgs e)  
  36. {  
  37. connection.Open();  
  38. da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);  
  39. dt = new DataTable();  
  40. da.Fill(dt);  
  41. dataGridView1.RowTemplate.Height = 60;  
  42. dataGridView1.DataSource = dt;  
  43. DataGridViewImageColumn imgCol = new DataGridViewImageColumn();  
  44. imgCol = (DataGridViewImageColumn)dataGridView1.Columns[7];  
  45. imgCol.ImageLayout = DataGridViewImageCellLayout.Stretch;  
  46. dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  
  47. label9.Text = "logged as " + this.Controls["label9"].Text;  
  48. if(this.Controls["label9"].Text == "Admin")  
  49. {  
  50. this.BtnDelete.Enabled = true;  
  51. this.BtnUpdate.Enabled = true;  
  52. this.BtnSave.Enabled = true;  
  53. }  
  54. if (this.Controls["label9"].Text == "Client")  
  55. {  
  56. BtnDelete.Enabled = false;  
  57. BtnUpdate.Enabled = false;  
  58. BtnSave.Enabled = false;  
  59. }  
  60. if (this.Controls["label9"].Text == "Cust")  
  61. {  
  62. this.BtnDelete.Enabled = false;  
  63. this.BtnUpdate.Enabled = false;  
  64. this.BtnSave.Enabled = true;  
  65. }  
  66. connection.Close();  
  67. }  
  68. private void BtnSave_Click(object sender, EventArgs e)  
  69. {  
  70. if (textBox1.Text == "" || textBox2.Text == "")  
  71. {  
  72. MessageBox.Show("Please Enter Details");  
  73. }  
  74. else  
  75. {  
  76. command = new MySqlCommand("SELECT * FROM TblPhoneBook WHERE MobileNo = '" + textBox2.Text + "' ", connection);  
  77. MySqlDataAdapter da = new MySqlDataAdapter(command);  
  78. da.Fill(ds);  
  79. int i = ds.Tables[0].Rows.Count;  
  80. if (i > 0)  
  81. {  
  82. MessageBox.Show("mobile no " + textBox2.Text + " Already Exist");  
  83. ds.Clear();  
  84. }  
  85. else  
  86. {  
  87. try  
  88. {  
  89. MemoryStream ms = new MemoryStream();  
  90. pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);  
  91. byte[] img = ms.ToArray();  
  92. connection.Open();  
  93. command = new MySqlCommand("INSERT INTO Fakhri2(CustName, InvNo, Dtd, Amt, Tax, Desc1, PIC)VALUES( @cust, @inv,@dtd, @amt, @tax, @des, @img)", connection);  
  94. command.Parameters.Add("@cust", MySqlDbType.VarChar).Value = textBox1.Text;  
  95. command.Parameters.Add("@inv", MySqlDbType.VarChar).Value = textBox2.Text;  
  96. command.Parameters.Add("@dtd", MySqlDbType.VarChar).Value = dateTimePicker1.Text;  
  97. command.Parameters.Add("@amt", MySqlDbType.VarChar).Value = textBox5.Text;  
  98. command.Parameters.Add("@tax", MySqlDbType.VarChar).Value = textBox6.Text;  
  99. command.Parameters.Add("@des", MySqlDbType.VarChar).Value = textBox7.Text;  
  100. command.Parameters.Add("@img", MySqlDbType.LongBlob).Value = img;  
  101. command.ExecuteNonQuery();  
  102. da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);  
  103. dt = new DataTable();  
  104. da.Fill(dt);  
  105. dataGridView1.DataSource = dt;  
  106. MessageBox.Show("Customer Record ADDED");  
  107. connection.Close();  
  108. }  
  109. catch (Exception ex)  
  110. {  
  111. MessageBox.Show(ex.Message);  
  112. }  
  113. }  
  114. }  
  115. textBox1.Text = "";  
  116. textBox2.Text = "";  
  117. textBox4.Text = "";  
  118. textBox5.Text = "";  
  119. textBox6.Text = "";  
  120. textBox7.Text = "";  
  121. }  
  122. private void BtnUpdate_Click(object sender, EventArgs e)  
  123. {  
  124. try  
  125. {  
  126. MemoryStream ms = new MemoryStream();  
  127. pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);  
  128. byte[] img = ms.ToArray();  
  129. connection.Open();  
  130. command = new MySqlCommand("UPDATE Fakhri2 SET CustName=@cust,InvNo=@inv,Dtd=@dtd,Amt=@amt,Tax=@tax,Desc1 =@des,PIC=@img WHERE SlNo = @SNo", connection);  
  131. command.Parameters.Add("@cust", MySqlDbType.VarChar).Value = textBox1.Text;  
  132. command.Parameters.Add("@SNo", MySqlDbType.VarChar).Value = textBox4.Text;  
  133. command.Parameters.Add("@inv", MySqlDbType.VarChar).Value = textBox2.Text;  
  134. command.Parameters.Add("@dtd", MySqlDbType.VarChar).Value = dateTimePicker1.Text;  
  135. command.Parameters.Add("@amt", MySqlDbType.VarChar).Value = textBox5.Text;  
  136. command.Parameters.Add("@tax", MySqlDbType.VarChar).Value = textBox6.Text;  
  137. command.Parameters.Add("@des", MySqlDbType.VarChar).Value = textBox7.Text;  
  138. command.Parameters.Add("@img", MySqlDbType.LongBlob).Value = img;  
  139. command.ExecuteNonQuery();  
  140. da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);  
  141. dt = new DataTable();  
  142. da.Fill(dt);  
  143. dataGridView1.DataSource = dt;  
  144. MessageBox.Show("Contact list update....");  
  145. connection.Close();  
  146. }  
  147. catch(Exception ex)  
  148. {  
  149. MessageBox.Show(ex.Message);  
  150. }  
  151. textBox1.Text = "";  
  152. textBox2.Text = "";  
  153. textBox4.Text = "";  
  154. textBox5.Text = "";  
  155. textBox6.Text = "";  
  156. textBox7.Text = "";  
  157. }  
  158. int i;  
  159. private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)  
  160. {  
  161. i = e.RowIndex;  
  162. DataGridViewRow row = dataGridView1.Rows[i];  
  163. textBox1.Text = row.Cells[1].Value.ToString();  
  164. textBox2.Text = row.Cells[2].Value.ToString();  
  165. textBox4.Text = row.Cells[0].Value.ToString();  
  166. dateTimePicker1.Text = row.Cells[3].Value.ToString();  
  167. textBox5.Text = row.Cells[4].Value.ToString();  
  168. textBox6.Text = row.Cells[5].Value.ToString();  
  169. textBox7.Text = row.Cells[6].Value.ToString();  
  170. Byte[] img = (Byte[])dataGridView1.CurrentRow.Cells[7].Value;  
  171. MemoryStream ms = new MemoryStream(img);  
  172. pictureBox1.Image = Image.FromStream(ms);  
  173. }  
  174. private void BtnDelete_Click(object sender, EventArgs e)  
  175. {  
  176. try  
  177. {  
  178. connection.Open();  
  179. command = new MySqlCommand("Delete FROM Fakhri2 WHERE SlNo = '" + textBox4.Text + "' ", connection);  
  180. command.ExecuteNonQuery();  
  181. da = new MySqlDataAdapter("SELECT * FROM Fakhri2 ORDER BY SlNo desc", connection);  
  182. dt = new DataTable();  
  183. da.Fill(dt);  
  184. dataGridView1.DataSource = dt;  
  185. MessageBox.Show("Contact...DELETED...");  
  186. connection.Close();  
  187. }  
  188. catch (Exception ex)  
  189. {  
  190. MessageBox.Show(ex.Message);  
  191. }  
  192. textBox1.Text = "";  
  193. textBox2.Text = "";  
  194. textBox4.Text = "";  
  195. textBox5.Text = "";  
  196. textBox6.Text = "";  
  197. textBox7.Text = "";  
  198. }  
  199. private void textBox3_TextChanged(object sender, EventArgs e)  
  200. {  
  201. BindingSource bs = new BindingSource();  
  202. bs.DataSource = dataGridView1.DataSource;  
  203. bs.Filter = "CustName like '%" + textBox3.Text + "%'";  
  204. dataGridView1.DataSource = bs;  
  205. }  
  206. private void label1_Click(object sender, EventArgs e)  
  207. {  
  208. }  
  209. private void BTN_CHOOSE_IMAGE_Click(object sender, EventArgs e)  
  210. {  
  211. OpenFileDialog opf = new OpenFileDialog();  
  212. opf.Filter = "Choose Image(*.JPG;*.PNG;*.PDF)|*.jpg;*.png;*.pdf";  
  213. if (opf.ShowDialog() == DialogResult.OK)  
  214. {  
  215. pictureBox1.Image = Image.FromFile(opf.FileName);  
  216. }  
  217. }  
  218. private void label9_Click(object sender, EventArgs e)  
  219. {  
  220. }  
  221. }  
  222. }