C#:System.InvalidOperationException错误 [英] C# : System.InvalidOperationException Error

查看:82
本文介绍了C#:System.InvalidOperationException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个方案为的桌面应用程序.一个用户将被登录,并且其ID的另一种形式将显示在文本框中.

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.

但是在用户登录后,我看到了这样的错误:

But After user logged in I saw the error like this :

这是我的第一个表格(Form1.cs)

Here is my first form (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");
            }
        }

    }
}

这是我的第二个表单(Entry.cs),我要在其中打印用户ID:

And Here is my Second form(Entry.cs) where I want to print the user 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!

推荐答案

您需要先打开sql连接实例才能对sql服务器进行查询.

You need to open your sql connection instance first to make a query against sql server.

更改您的代码:

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);

//...

这篇关于C#:System.InvalidOperationException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆