Home > Back-end >  Operation of the login window, the display will object reference not set instance, brother please he
Operation of the login window, the display will object reference not set instance, brother please he

Time:09-28

using System;
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

Using System. The Data. SqlClient.

The namespace StudentInfoSystem
{
Public partial class frmLogin: Form
{
Public frmLogin ()
{
InitializeComponent ();
}

Public static string userName;//record login user name//main form used in
Public static string userRight;//record login user permissions//main form used in

SqlConnection conn.
SqlCommand com;
SqlDataAdapter da;
SqlDataReader Dr;
Private void frmLogin_Load (object sender, EventArgs e)
{

Try
{
Conn=new SqlConnection (" server=(local) \ \ sqlexpress; Integrated security=true; The database=StudentBC ");
The DataSet ds=new DataSet ();
Da=new SqlDataAdapter (" select user name from the user table ", conn);
If (conn. State==ConnectionState. Closed)
{
Conn. The Open ();
}
Da. The Fill (ds, "user list");
Conn. Close ();
CbxUserName. The DataSource=ds. The Tables (" user list "),
CbxUserName. DisplayMember="user name";
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message. The ToString ());
}
}

Private void cbxUserName_SelectedIndexChanged (object sender, EventArgs e)//query permission values
{
Try
{
Com=new SqlCommand (" select the user name, user permissions from the users table where user name='" + cbxUserName. Text + "' ", conn);
If (conn. State==ConnectionState. Closed)
{
Conn. The Open ();
}
Dr=com. ExecuteReader ();



If (Dr. Read ())


{
LblUserRight1. Text=Dr [r]. "user rights" ToString ();
UserRight=lblUserRight1. Text;
}
Dr. Close ();
Conn. Close ();
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message. The ToString ());
}
}

Private void btnLogin_Click (object sender, EventArgs e)//you can use the reader can also use data set class implements the
{
//data set class implements the
Try
{
Da=new SqlDataAdapter (" select * from the users table where user name='" + cbxUserName. Text. The Trim () + "' and the user password='" + txtUserPwd. Text. The Trim () + "' ", conn);
The DataSet ds=new DataSet ();
If (conn. State==ConnectionState. Closed)
{
Conn. The Open ();
}
Da. The Fill (ds, "user list");
Conn. Close ();
If (ds) Tables [r]. "the user table" Rows. Count & gt; 0)
{
The userName=cbxUserName. Text;

FrmMain frmMain=new frmMain ();
This. Hide ();
Frmmain. The Show ();
}
The else
{
MessageBox. Show (" the user name or password error!" , "tip", MessageBoxButtons. OK, MessageBoxIcon. Information);
TxtUserPwd. Text="";
CbxUserName. Focus ();
}
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message. The ToString ());
}
}
//reader class implements the


Private void btnExit_Click (object sender, EventArgs e)
{
Application. The Exit ();
}

Private void frmLogin_FormClosed (object sender, FormClosedEventArgs e)
{
Application. The Exit ();
}

Private void frmLogin_Load_1 (object sender, EventArgs e)
{

}



}
}

CodePudding user response:

Is this your c #?
  • Related