2016-10-02 7 views
0

シナリオがあるデスクトップアプリケーションを構築しています。ユーザーはログインし、別のフォームにIDがテキストボックスに表示されます。C#:System.InvalidOperationExceptionエラー

しかし、私にログインしたユーザーが、このようなエラーを見た後:

enter image description here

は、ここに私の最初の形式(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"); 
      } 
     } 

    } 
} 

そして、ここでは私の第二の形式(項目です.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(); 
     } 
    } 
} 

彼は問題を解決するために私を助けてください。

+0

あなたは、SQL-Serverのインスタンスに接続していますか?あなたの接続要求を拒否しているようです。リモートアクセスまたは非ウィンドウ認証を有効にし、その接続文字列でユーザー名とパスワードを使用するなどの設定を確認したい場合があります。 –

答えて

1

最初にSQL接続インスタンスを開いて、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); 

//... 
関連する問題