Dynamic code issue

  • Thread starter Thread starter LeighGrant
  • Start date Start date
L

LeighGrant

Guest
Hi,


I am trying to create a login form using Visual Studio and C#. I have used this code when using drag and drop windows form, but wanted to try making a dynamic form. I have not had an issue creating the form however, when I want to assign code to one of the buttons intellisence informs me the text boxes I created do not exist. can anybody please point me in the right direction. This is the code I am working with. I have formatted where the error occurs in bold and underline.


Thank you in advance

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.Data.OleDb;

namespace Costal_College_Admissions_System
{
public partial class Admin_Login : Form
{
DataTable dt = new DataTable();
static string connection = "Provider=Microsoft.ACE.Oledb.12.0;Data Source= " + Application.StartupPath + "/School Admission Database.accdb";
OleDbConnection con = new OleDbConnection(connection);

public Admin_Login()
{
InitializeComponent();
}

private void Admin_Login_Load(object sender, EventArgs e)
{
this.Size = new Size(600, 300);
this.BackColor = Color.Black;

Button btnLogin = new Button();
btnLogin.Click += new EventHandler(btnLogin_Click);
btnLogin.Location = new Point(235, 183);
btnLogin.Text = "Login";
btnLogin.BackColor = Color.White;


this.Controls.Add(btnLogin);

Button btnBack = new Button();
btnBack.Click += new EventHandler(btnBack_Click);
btnBack.Location = new Point(370, 183);
btnBack.Text = "Back";
btnBack.BackColor = Color.White;


this.Controls.Add(btnBack);

Label lblWelcome = new Label();
lblWelcome.Text = "Welcome to East Coast College Admin Log in";
lblWelcome.Location = new Point(202, 31);
lblWelcome.Font = new Font("Calibri", 12);
lblWelcome.AutoSize = true;
lblWelcome.ForeColor = Color.Yellow;


this.Controls.Add(lblWelcome);

Label lblWelcome2 = new Label();
lblWelcome2.Text = "Please enter your username and password to login";
lblWelcome2.Location = new Point(203, 79);
lblWelcome2.Font = new Font("Calibri", 11);
lblWelcome2.AutoSize = true;
lblWelcome2.ForeColor = Color.Yellow;

this.Controls.Add(lblWelcome2);

PictureBox picLogo = new PictureBox();
picLogo.Location = new Point(12, 31);
picLogo.BackgroundImage = Properties.Resources.EastCoastBadge1;
picLogo.BackgroundImageLayout = ImageLayout.Stretch;
picLogo.Size = new Size(158, 178);


this.Controls.Add(picLogo);

TextBox txtUsername = new TextBox();
txtUsername.Name = "txtUsername";
txtUsername.Location = new Point(216, 150);
txtUsername.Size = new Size(109, 20);

this.Controls.Add(txtUsername);

TextBox txtPassword = new TextBox();
txtPassword.Name = "txtPassword";
txtPassword.Location = new Point(350, 150);
txtPassword.Size = new Size(109, 20);
txtPassword.UseSystemPasswordChar = true; //Alters the text entered so it is hidden for security

this.Controls.Add(txtPassword);

Label lblUsername = new Label();
lblUsername.Text = "Username";
lblUsername.Location = new Point(235, 123);
lblUsername.Font = new Font("Calibri", 12);
lblUsername.AutoSize = true;
lblUsername.ForeColor = Color.Yellow;

this.Controls.Add(lblUsername);

Label lblPassword = new Label();
lblPassword.Text = "Password";
lblPassword.Location = new Point(370, 123);
lblPassword.Font = new Font("Calibri", 12);
lblPassword.AutoSize = true;
lblPassword.ForeColor = Color.Yellow;

this.Controls.Add(lblPassword);




}


private void btnLogin_Click(object sender, EventArgs e)
{
string usr = txtUsername.Text;
string psw = txtPassword.Text;
if (txtUsername.Text.Trim() == "" || txtPassword.Text.Trim() == "")
{
MessageBox.Show("Please Enter Both Fields!", "WARNING");
}
else
{
con.Open();

dt = new DataTable();
string sql = "Select * from Admin where Admin_ID= '" + txtUsername.Text + "' AND A_password= '" + txtPassword.Text + "'";
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
da.Fill(dt);

if (dt.Rows.Count > 0)

{
Welcome f2 = new Welcome();
this.Visible = false;
f2.Show();
}
else
{
MessageBox.Show("Username or Password Not Registered! Please Contact Administrator!");
}
con.Close();
}
}
private void btnBack_Click(object sender, EventArgs e)
{
Welcome f2 = new Welcome();
this.Visible = false;
f2.Show();
}
}
}

Continue reading...
 
Back
Top