weixin_45843354 2020-04-08 09:36 采纳率: 0%
浏览 462

运行登录窗体时,显示未将对象引用设置到对象的实例

图片说明

using System;https://img-ask.csdn.net/upload/202004/08/1586312099_392212.png
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

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

    public static string userName ;//记录登录用户名字//主窗体中使用
    public static string userRight;//记录登录用户的权限//主窗体中使用

    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;database=StudentBC");
            DataSet ds = new DataSet();
            da = new SqlDataAdapter("select 用户名称 from 用户表", conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "用户表");
            conn.Close();
            cbxUserName.DataSource = ds.Tables["用户表"];
            cbxUserName.DisplayMember = "用户名称";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

    private void cbxUserName_SelectedIndexChanged(object sender, EventArgs e)//查询权限值
    {
        try
        {
            com = new SqlCommand("select 用户名称,用户权限 from 用户表 where 用户名称='" + cbxUserName.Text + "'", conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            dr = com.ExecuteReader();



            if (dr.Read())


            {
                lblUserRight1.Text = dr["用户权限"].ToString();
                userRight = lblUserRight1.Text;
            }
            dr.Close();
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

    private void btnLogin_Click(object sender, EventArgs e)//可以用读取器类也可以用数据集类实现
    {
        //用数据集类实现
        try
        {
            da = new SqlDataAdapter("select * from 用户表 where 用户名称='" + cbxUserName.Text.Trim() + "' and 用户密码='" + txtUserPwd.Text.Trim() + "'", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "用户表");
            conn.Close();
            if (ds.Tables["用户表"].Rows.Count > 0)
            {
                userName = cbxUserName.Text;

                frmMain frmmain = new frmMain();
                this.Hide();
                frmmain.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserPwd.Text = "";
                cbxUserName.Focus();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
        //用读取器类实现


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

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

    private void frmLogin_Load_1(object sender, EventArgs e)
    {

    }



}

}

  • 写回答

1条回答 默认 最新

  • 小学狗喵喵叫 2020-04-08 10:03
    关注

    我不会,可以转问题到C#或者.net标签

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法