Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
0
Answer
not able to activate / enable as per role
juzar para
6y
613
1
Reply
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;
// this will be used for pic programming
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)
{
}
}
}
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
How do Xamarin forms connect to SQL Server ?
Troubles using SSH.net library for SSH and SFTP connections