2016-09-03 2 views
1

ユーザーごとに別々のウィンドウを開くログインフォームを作成したい タイプです。このプログラムデータベースには、3つのカラム(ユーザ名、パスワード、ユーザタイプ)があります。このユーザタイプは3つあります(admin、manager、user)&各ユーザタイプは別々のウィンドウを持っています。ユーザーごとに別のウィンドウを開く方法

  1. 管理者= Form2の
  2. マネージャー=
  3. たForm3
  4. ユーザー= Form4

ここでは私のログインボタンのコードです。それぞれのユーザータイプごとにこのウィンドウを個別に開きますようにしてください。 ウィンドウ

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\sasindu\documents\visual studio 2010\Projects\Employee Database\Employee Database\Database.mdf;Integrated Security=True;User Instance=True"); 
    SqlCommand cmd = new SqlCommand("select * from login where [email protected] and password [email protected]", con); 
    cmd.Parameters.AddWithValue("@username", textBox1.Text); 
    cmd.Parameters.AddWithValue("@password", textBox2.Text); 

    SqlDataAdapter sda = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 
    sda.Fill(dt); 
    con.Open(); 
    int i = cmd.ExecuteNonQuery(); 

    con.Close(); 

    if (dt.Rows.Count > 0) 
    { 
     Form3 Form = new Form3(); 
     Form.Show(); 
     this.Hide(); 
    }  
    else 
    { 
     MessageBox.Show("Please enter Correct Username and Password"); 
    } 

答えて

5

ログインテーブルのユーザータイプに列を追加したと思います。 ユーザタイプの列は、その後、あなたが簡単に私がコメントしてください任意のクエリyou.ifこのコードの助けを願っています

if(dt.rows[0]["user_type"].tostring()=="Admin") 
{ 
// which form you show 

} 

if(dt.rows[0]["user_type"].tostring()=="User") 
{ 
// which form you show 

} 

if(dt.rows[0]["user_type"].tostring()=="Manager") 
{ 
// which form you show 

} 

を検証します管理者、利用者、管理者など を導き出すています。

+0

マニッシュ仲間... – Sasindu

+0

は –

+0

を@Sasinduようこそ代わりの場合-else'、コメントをbro..anywayおかげで動作していない '切り替える-case' –

1
switch (dt.Rows[0]["user_type"].ToString().ToLower()) 
     { 
      case "admin": 
      //Show Admin form 
       MessageBox.Show("admin form"); 
       break; 
      case "user": 
      //Show User form 
       MessageBox.Show("user form"); 
       break; 
      case "manager": 
      //Show Manager form 
       MessageBox.Show("manager form"); 
       break; 
      default: 
       MessageBox.Show("Invalid user type."); 
       break; 
     } 
+0

に上記をひそか'やっおかげで.. – Sasindu

+0

何あなたが直面しているエラーですか? –

関連する問題