2017-02-19 1 views
0

私の見出しが混乱していて申し訳ありませんが、問題を別々に表現する方法がわかりませんでした。私は2つのクラス、両方のウィンドウフォームを持っています。そのうちの1つはログインインタフェースであり、もう1つは、ログインが有効である場合にユーザが奪取される店舗インタフェースである。さて、私は、データベースの正しい行から "購入"を差し引くことができるように、ショップのインターフェイスクラスにユーザー名とパスワードを渡そうとしていますが、何らかの理由でこれが動作していません。私はコードを直接有効なユーザー名とパスワードの文字列を渡すだけで、私のメソッドをテストして、私がそれを行う場合、メソッドは正常に動作します。デバッグモードでも、ユーザー名とパスワードの値がgetメソッドを通じてショップインターフェースクラスに渡されないことが示されます。事前のおかげで、あなたが役立つことを願って、テキスト:)get/setメソッドを使用してもint値がオブジェクトと一緒に渡されない

ここ

の非常に大きなブロックを気の毒には、問題の2つのクラスがあり、加えて私は、データベース用に使用していますメソッドを持つクラス:

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 ShopshopFinalfinal 
{ 
    public partial class LoginInterface : Form 
    { 
     DatabaseConnection dbConnection = new DatabaseConnection(); 
     private ShopInterface shop1Interface; 
     private RegisterInterface reg1Interface; 
     private string _username; 
     private string _password; 

     public void SetUsername(string username) 
     { 
      _username = username; 
     } 

     public void SetPassword(string password) 
     { 
      _password = password; 
     } 

     public string GetUsername() 
     { 
      return _username; 
     } 

     public string GetPassword() 
     { 
      return _password; 
     } 


     public LoginInterface() 
     { 
      InitializeComponent(); 
      txt_password.PasswordChar = '*'; 
     } 

     private void btn_login_Click(object sender, EventArgs e) 
     { 
      if (dbConnection.CheckUsername(txt_username.Text) == 1 && dbConnection.CheckPassword(txt_password.Text) == 1) 
      { 
       SetUsername(txt_username.Text); 
       SetPassword(txt_password.Text); 
       //txt_username.Clear(); 
       //txt_password.Clear(); 
       shop1Interface = new ShopInterface(); 
       shop1Interface.Show(); 

      } 
     } 

     private void btn_register_Click(object sender, EventArgs e) 
     { 
      reg1Interface = new RegisterInterface(); 
      reg1Interface.Show(); 
     } 
    } 
} 


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 ShopshopFinalfinal 
{ 
    public partial class ShopInterface : Form 
    { 

     private DatabaseConnection dbConnection = new DatabaseConnection(); 
     private LoginInterface login = new LoginInterface(); 



     public ShopInterface() 
     { 
      InitializeComponent(); 
     } 

     private void btn_buyapple_Click(object sender, EventArgs e) 
     { 


      //string username = login.GetUsername(); 
      //string password = login.GetPassword(); 

      dbConnection.Transaction(login.GetUsername(), login.GetPassword(), 10); 


     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ShopshopFinalfinal 
{ 
    class DatabaseConnection 
    { 
     private SqlConnection conn; 
     private SqlDataReader rdr; 
     private SqlCommand cmd; 

     public DatabaseConnection() 
     { 
      conn = new SqlConnection(Properties.Settings.Default.connectionstring); 
     } 

     public int CheckUsername(string username) 
     { 
      int result = 0; 

      //Opret den ønskede SQL kommmando - her tjekker den om username fra textboxen er lig en i User table. 
      cmd = new SqlCommand("select * from dbo.Users where Username ='" + username + "'", conn); 

      //Åbn forbindelsen til databasen. 
      conn.Open(); 

      //Udfører det ønskede SQL statement på databasen. 
      rdr = cmd.ExecuteReader(); 

      //Tjek om den har læst og om det er rigtigt. 
      if (rdr.Read()) 
      { 
       result = 1; 
      } 
      else 
      { 
       result = 0; 
      } 

      //Luk forbindelsen til databasen. 
      conn.Close(); 

      return result; 
     } 

     public int CheckPassword(string password) 
     { 
      int result = 0; 

      //Opret SQL-kommando - tjek om password fra textbox er lig med et i databasen. 
      cmd = new SqlCommand("select * from dbo.Users where Password ='" + password + "'", conn); 

      //Åben forbindelsen. 
      conn.Open(); 

      //Udfør det ønskede SQL statement på databasen. 
      rdr = cmd.ExecuteReader(); 

      if (rdr.Read()) 
      { 
       result = 1; 
      } 
      else 
      { 
       result = 0; 
      } 

      conn.Close(); 

      return result; 

     } 

     public void RegisterUser(string username, string password) 
     { 
      conn.Open(); 

      cmd = 
       new SqlCommand("insert into dbo.Users (Username, Password, IsAdmin, Balance) values ('" + 
           username + 
           "', '" + password + "', '0', '0')", conn); 
      rdr = cmd.ExecuteReader(); 
      rdr.Close(); 
      conn.Close(); 
     } 

     public void Transaction(string username, string password, int price) 
     { 


      conn.Open(); 

      cmd = new SqlCommand("update dbo.Users set Balance = Balance - " + price + " where Username = '"+username+"' and Password = '"+password+"'", conn); 

      rdr = cmd.ExecuteReader(); 

      rdr.Close(); 

      conn.Close(); 
     } 
    } 
} 
+1

フォーム間でデータを渡す場合は、フォームのインスタンスを使用する必要があります。私の2つのフォームプロジェクトの例を参照してください:http://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng

+0

おかげで、完璧に動作します:) – sunero4

答えて

1

あなたは、ほとんど存在していますが、他の形式にデータを渡していません。 ShopInterfaceでこれを変更します。

これに
private LoginInterface login = new LoginInterface(); 

public LoginInterface Login {get; set;} 

次に、あなたのLoginInterfaceにあなたがshop1Interfaceを表示する前にこの操作を行います。

private void btn_login_Click(object sender, EventArgs e) 
{ 
    if (dbConnection.CheckUsername(txt_username.Text) == 1 && dbConnection.CheckPassword(txt_password.Text) == 1) 
    { 
     SetUsername(txt_username.Text); 
     SetPassword(txt_password.Text); 
     //txt_username.Clear(); 
     //txt_password.Clear(); 
     shop1Interface = new ShopInterface(); 

     // This is the line you need 
     shop1Interface.Login = this; 

     shop1Interface.Show(); 

    } 
} 

を、あなたのLoginInterfaceへの参照を渡すので、 shop1Interfaceにアクセスすると、次のようにアクセスできます。

private void btn_buyapple_Click(object sender, EventArgs e) 
{ 
    // Now it will work 
    string username = this.Login.GetUsername(); 
    string password = this.Login.GetPassword(); 

    dbConnection.Transaction(login.GetUsername(), login.GetPassword(), 10); 
} 
+0

ありがとうございました! – sunero4

1

問題は、LoginInterfaceの新しいインスタンスを作成するShopInterfaceがあり、すべてのプロパティがデフォルト状態になることです。これは実際に値を持つインスタンスと同じインスタンスではないため、なぜ空白なのでしょうか。

私はむしろ(ShopInterfaceからLoginInterfaceへの強い依存性を意味している)LoginInterfaceから値を取るようにShopInterfaceを取得する方法を見つけようとするよりも、より良いアプローチは、それらを渡す方法を提供することだと思います:あなたはLoginInterfaceこれを行うことができます

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 ShopshopFinalfinal 
{ 
    public partial class ShopInterface : Form 
    { 

     private DatabaseConnection dbConnection = new DatabaseConnection(); 
     private string Username { get; set; } 
     private string Password { get; set; } 

     public ShopInterface(string username, string password) 
     { 
      InitializeComponent(); 
      Username = username; 
      Password = password; 
     } 

     private void btn_buyapple_Click(object sender, EventArgs e) 
     { 

      dbConnection.Transaction(username, password, 10); 


     } 
    } 
} 

は次に:

shop1Interface = new ShopInterface(txt_username.Text, txt_password.Text); 
shop1Interface.Show(); 
+0

ありがとう、これはおそらく良いですアイデアのように私はそれらのお互いのインスタンスを作成する必要はありません! – sunero4

関連する問題