简体   繁体   English

C#:System.InvalidOperationException错误

[英]C# : System.InvalidOperationException Error

I'm building a desktop application where the scenario is A user will be logged in and in another form it's id will be shown in text box. 我正在构建一个方案为的桌面应用程序。一个用户将登录,并以另一种形式在文本框中显示其ID。

But After user logged in I saw the error like this : 但是在用户登录后,我看到了这样的错误:

在此处输入图片说明

Here is my first form (Form1.cs) 这是我的第一个表格(Form1.cs)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace EmployeeApp
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        private string employeeID;
        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void loginButton_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=INCEPSYS-SE\TEST;Initial Catalog=Employee;Integrated Security=True");

            SqlCommand command = new SqlCommand("select * from Employees where Name = '" + nameTextBox.Text + " ' and Password = '" + passwordTextBox.Text + "'", connection);
            SqlDataReader myReader = command.ExecuteReader();
            while (myReader.Read())
            {
                string employeeID = myReader["EmployeeID"].ToString();
            }


            SqlDataAdapter sda = new SqlDataAdapter("select count(*) from Employees where Name = '" + nameTextBox.Text + " ' and Password = '" + passwordTextBox.Text + "'", connection);

            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                this.Hide();
                Entry ss = new Entry(employeeID);
                ss.Show();
            }
            else
            {
                MessageBox.Show("Please Check your Username & password");
            }
        }

    }
}

And Here is my Second form(Entry.cs) where I want to print the user id: 这是我的第二个表单(Entry.cs),我要在其中打印用户ID:

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;

namespace EmployeeApp
{
    public partial class Entry : Form
    {      public Entry(string employeeId)
        {
            InitializeComponent();
              idTextBox.Text = employeeId;
        }


       private void reportButton_Click(object sender, EventArgs e)
        {
            Report report = new Report();
            report.Show();
        }
    }
}

If anyone find out the problem..help me to find it out please! 如果有人发现了问题..请帮助我找出问题!

You need to open your sql connection instance first to make a query against sql server. 您需要先打开sql连接实例,才能对sql服务器进行查询。

Change your code: 更改您的代码:

SqlConnection connection = new SqlConnection(@"Data Source=INCEPSYS-SE\TEST;Initial Catalog=Employee;Integrated Security=True");

connection.Open();

SqlCommand command = new SqlCommand("select * from Employees where Name = '" + nameTextBox.Text + " ' and Password = '" + passwordTextBox.Text + "'", connection);

//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM