Omer Butt
Active member
- Joined
- Sep 6, 2021
- Messages
- 29
- Programming Experience
- Beginner
What I have is a Login Form with the name
In
Below is a Code I used for above explanation:
Now After I run the program and try add one user as Administrator and its successfully added to the database including the picture in binary. Now as one User is Saved I am able to use Login Form to enter in to the program through that User Data I just Saved in Database.
In the
Here are the Db Connection and the Strings and object I declared:
Below is a code to Login to Administrator Dashboard or Pharmacy Dashboard:
In above
And writing the code on line 23 correctly, Upon Login It will take us to Administrator Panel where on Picture box I want to display a user Picture using that Upic code from Line 23 of Login_Form
Below is a code for Administrator_Load event: (Where I already have done the code for to display Role, UName and UPic)
In above code Line 4 and 5 We are displaying Picture in Picturebox named
Line 5 is working Fine But Line 4 to display a Picture is not working.
I hope I provided a well explained Question and situation over here Please Guide
Login_Form
in which I have two fields one is Username and the other is Password, Users are added to database from a Add User User Control named as UC_AddUser
.In
UC_AddUser
User Control I have fields like User_Role
, User_Name
, User_DOB
, User_Mobile
, User_Email
, User_Username
, User_Password
, and PicBox_AddUser_Picture
and have Browse Button to add a Picture to picture box using OpenDialog and a Signup button that adds all the data to the database including Picture in a database in a Binary form.Below is a Code I used for above explanation:
Browse Button Code to show picture in a Picture Box and file path in a read only textbox.:
private void Btn_AddUser_Browse_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "C://Desktop";
openFileDialog1.Title = "Select your Picture";
openFileDialog1.Filter = "Image Only(*.jpg; *.pnp; *.gif) | *.jpg; *.png; *.gif";
openFileDialog1.FilterIndex = 1;
openFileDialog1.FileName = "";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileInfo fi = new FileInfo(openFileDialog1.FileName);
long filesize = fi.Length;
if (filesize > 100000)
{
Lbl_AddUser_PicSize_Error.Text = "Please Select the Image Less then 100KB";
}
else
{
Lbl_AddUser_PicSize_Error.Text = "";
string Path = System.IO.Path.GetFullPath(openFileDialog1.FileName);
Txt_AddUser_Filename.Text = Path;
PicBox_AddUser_Picture.Image = new Bitmap(openFileDialog1.FileName);
}
}
}
Signup Button Code to add Form data to Database:
private void Btn_SignUp_Click(object sender, EventArgs e)
{
try
{
var UserAvailability = new SqlCommand("Sp_UserAvailability")
{
};
UserAvailability.CommandType = CommandType.StoredProcedure;
UserAvailability.Connection = Conn.Connect;
UserAvailability.Parameters.AddWithValue("UN", Txt_AddUser_UserName.Text);
SqlDataAdapter QryCmd = new SqlDataAdapter(UserAvailability);
DataTable dt = new DataTable();
QryCmd.Fill(dt);
if (dt.Rows.Count == 0)
{
PictureBox_Username_Tick_Cross.ImageLocation = @"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS v1.1\Images\yes.png";
var Ms = new MemoryStream();
PicBox_AddUser_Picture.Image.Save(Ms, ImageFormat.Jpeg);
byte[] Rr = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(Rr, 0, Rr.Length);
var AddUsers_Signup = new SqlCommand("Sp_AddUsers_Signup")
{
};
AddUsers_Signup.CommandType = CommandType.StoredProcedure;
AddUsers_Signup.Connection = Conn.Connect;
AddUsers_Signup.Parameters.AddWithValue("UR", Combo_UserRole.Text);
AddUsers_Signup.Parameters.AddWithValue("name", Txt_AddUser_Name.Text);
AddUsers_Signup.Parameters.AddWithValue("dob", DatePicker_AddUser_DOB.Text);
AddUsers_Signup.Parameters.AddWithValue("mobile", Int64.Parse(Txt_AddUser_MobileNo.Text));
AddUsers_Signup.Parameters.AddWithValue("email", Txt_AddUser_Email.Text);
AddUsers_Signup.Parameters.AddWithValue("username", Txt_AddUser_UserName.Text);
AddUsers_Signup.Parameters.AddWithValue("password", Txt_AddUser_Password.Text);
AddUsers_Signup.Parameters.AddWithValue("picture", Rr);
SqlDataAdapter cmd = new SqlDataAdapter(AddUsers_Signup);
DataTable AdU = new DataTable();
cmd.Fill(AdU);
if (AdU.Rows.Count == 0)
{
MessageBox.Show("Data Inserted Successfully!", "Success! Data Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Data is not Inserted!", "Try enter the data correctly", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
PictureBox_Username_Tick_Cross.ImageLocation = @"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS v1.1\Images\no.png";
}
}
catch (Exception msg)
{
MessageBox.Show(msg.Message);
}
}
Now After I run the program and try add one user as Administrator and its successfully added to the database including the picture in binary. Now as one User is Saved I am able to use Login Form to enter in to the program through that User Data I just Saved in Database.
In the
Login_Form
I declared two Public Static Strings
named UName
and Role
and one Public Static object
named UPic
Here are the Db Connection and the Strings and object I declared:
private readonly DbConnect Conn = new DbConnect();
public static string UName;
public static string Role;
public static object Upic;
Below is a code to Login to Administrator Dashboard or Pharmacy Dashboard:
Code that show Validation and Login the user according to the Role and Also Displays a UserRole and UserName and UserPicture on Administrator Panal:
private void Btn_Login_Click(object sender, EventArgs e)
{
try
{
if (IsValid())
{
var Login = new SqlCommand("sp_Login")
{
};
Login.CommandType = CommandType.StoredProcedure;
Login.Connection = Conn.Connect;
Login.Parameters.AddWithValue("UN", Txt_Login_Username.Text);
Login.Parameters.AddWithValue("pass", Txt_Login_Password.Text);
SqlDataAdapter ConnCommand = new SqlDataAdapter(Login);
DataTable dt = new DataTable();
ConnCommand.Fill(dt);
if (dt.Rows.Count > 0)
{
var ms = new MemoryStream();
Upic = As[ms]dt.Rows[0]["User_Picture"];
UName = dt.Rows[0]["User_Name"].ToString();
Role = dt.Rows[0]["User_Role"].ToString();
String UserRole = dt.Rows[0][1].ToString();
if (UserRole == "Administrator")
{
Administrator_Panel Admin = new Administrator_Panel();
Admin.Show();
this.Hide();
}
else if (UserRole == "Pharmacist")
{
Pharmacist_Panel Pharm = new Pharmacist_Panel();
Pharm.Show();
this.Hide();
}
}
else
{
MessageBox.Show("You have entered an incorrect Username or Password! Please Try Again", "Wrong Details Entered! Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
catch (Exception msg)
{
MessageBox.Show(msg.Message);
}
}
private bool IsValid()
{
if (Txt_Login_Username.Text.Trim() == String.Empty)
{
MessageBox.Show("Username is Required", "Form Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (Txt_Login_Password.Text.Trim() == String.Empty)
{
MessageBox.Show("Password is Required", "Form Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
In above
Btn_Login_Click
event code in Line 23: Upic = As[ms]dt.Rows[0]["User_Picture"];
is not a write way what is the write way to write it here just like we write: UName = dt.Rows[0]["User_Name"].ToString();
Role = dt.Rows[0]["User_Role"].ToString();
in line 24 and 25.And writing the code on line 23 correctly, Upon Login It will take us to Administrator Panel where on Picture box I want to display a user Picture using that Upic code from Line 23 of Login_Form
Below is a code for Administrator_Load event: (Where I already have done the code for to display Role, UName and UPic)
Code for After Login Administrator Panel on Load Event.:
private void Administrator_Panel_Load(object sender, EventArgs e)
{
PicBox_Main_Admin_UserPic.Image = (Image)Login_Form.Upic;
AdminUserName.Text = Login_Form.Role + ": " + Login_Form.UName;
}
In above code Line 4 and 5 We are displaying Picture in Picturebox named
PicBox_Main_Admin_UserPic
and Role and Uname in a Label name AdminUserName
Line 5 is working Fine But Line 4 to display a Picture is not working.
I hope I provided a well explained Question and situation over here Please Guide