Learn how to run the logon window and main window.

Source: Internet
Author: User

Learn how to run the logon window and main window.

Reprint please indicate from Zhu jiayuan http://blog.csdn.net/zhgl7688

To sum up, learn from each other:

The logon window is displayed. the return value of the logon window is determined. The main window is displayed when logon is successful. Otherwise, the program is closed.

The main code is as follows:

1. modify some codes in Main as follows: FrmUserLogin indicates the logon window and FrmMain indicates the Main window;

Process: the logon window is displayed first, and the success is judged based on the result returned by the logon window. The main window is displayed successfully; otherwise, the program is closed.

Static void Main () {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); // display the logon form FrmUserLogin objUserFrm = new FrmUserLogin (); DialogResult result = objUserFrm. showDialog (); // determine the return value of the logon form if (result = DialogResult. OK) // The Application is successfully logged on. run (new FrmMain (); else Application. exit (); // Exit the entire program}

2. Two types of Logon Windows are used: Administrator data logging AdminService and administrator entity Admin.

2.1 administrator-type Admin: used to store administrator data to facilitate inter-class transfer and further expand Member use.

Three members are used in the class: logon Id (LoginId), logon password (Loginpwd), and Administrator name (AdminName ).

    public class Admin    {        public int LoginId { get; set; }        public string Loginpwd { get; set; }        public string AdminName { get; set; }    }
2.2 administrator data types AdminService: used to determine whether the logon information exists in the database. The static method SQLHelper. GetReader is used.

Process: Create a query statement SQL, query loginid = logon Id and loginpwd = logon password in the database Admins, and return the Administrator name.

public class AdminService    {        public Admin AdminLogin(Admin objAdmin)        {            string sql = "select AdminName from Admins where loginid={0} and loginpwd={1}";            sql = string.Format(sql, objAdmin.LoginId, objAdmin.Loginpwd);            SqlDataReader objReader = SQLHelper.GetReader(sql);            if (objReader.Read())            {                objAdmin.AdminName = objReader["AdminName"].ToString();            }            else            { objAdmin = null; }            objReader.Close();            return objAdmin;        }    }

2.2.1 Class static method SQLHelper. GetReader

  public static  SqlDataReader GetReader(string sql)        {            SqlConnection conn = new SqlConnection(connString);            SqlCommand cmd = new SqlCommand(sql, conn);            try            {                conn.Open();                return cmd.ExecuteReader(CommandBehavior.CloseConnection) ;            }            catch (Exception ex)            {                throw ex;            }        }


3. Judgment in the logon window: The main statement this. DialogResult = DialogResult. OK is returned;

Private void btnLogin_Click (object sender, EventArgs e) {if (this.txt LoginId. text. trim (). length = 0) {MessageBox. show ("Enter the user name", "prompt"); this.txt LoginId. focus (); return;} if (this.txt LoginPwd. text. trim (). length = 0) {MessageBox. show ("Enter Password", "prompt"); this.txt LoginPwd. focus (); return;} Admin objAdmin = new Admin () {LoginId = Convert. toInt32 (txtLoginId. text), Loginpwd = txtLoginPwd. text. trim ()}; Try {objAdmin = new AdminService (). AdminLogin (objAdmin); if (objAdmin! = Null) {Program. objCurrentAdmin = objAdmin; this. DialogResult = DialogResult. OK; this. Close () ;}else {MessageBox. Show ("Incorrect Logon account or password! "," Logon prompt ") ;}} catch (Exception ex) {MessageBox. Show (" Data Access Error "+ ex. Message );}}

The above code is taken from the courseware of Andy.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.