所以我有一个有趣的问题......我有一个可以运行的应用程序,但需要“OrderID.text”中的一个值。然后在 MySQL 数据库中搜索该值,并返回我需要的信息。在这种情况下,电话号码。
当我在代码中包含该值时。(例如,newCurrentID = 8),它返回电话号码。但是,我希望用户能够输入自己的值,并且电话号码更改为用户的偏好。
当我输入一个值时,MessageBox 在我按下回车后工作,但电话的文本框保持空白。有任何想法吗????
我只包括与此相关的代码。
public partial class Form1:
{
//the rest is just design code for the app.
private void InitializeComponent()
{
this.orderID.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EnterKey);
this.orderID.Text = "";
this.phoneNumber.Text = DB_App.phone_number;
this.phoneNumber.TextChanged += new System.EventHandler(this.phoneNumber_TextChanged);
}
}
public partial class Form1 : Form
{
private DBApplication DB_App;
public Form1()
{
DB_App = new DBApplication(this);
InitializeComponent();
}
// I assigned a value to this to make sure the MySQL connection worked.
public int newCurrentID;
private void EnterKey(object o, KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Enter)
{
if (!int.TryParse(orderID.Text, out newCurrentID))
MessageBox.Show("not a number");
else
{
int.TryParse(orderID.Text, out newCurrentID);
//I used this to make sure the "newCurrentID" was being read.
//MessageBox.Show(newCurrentID.ToString());
}
DB_App.OrderID();
e.Handled = true;
}
}
private void phoneNumber_TextChanged(object sender, EventArgs e)
{
}
}
public class DBApplication : DBInfo
{
public DBApplication(Form1 form)
{
this.Form = form;
OrderID();
CloseConnection();
}
//my guess is that the problem is somewhere in here, but im not certain.
public void OrderID()
{
customer_id = this.Form.newCurrentID;
//add the DataBase query to the string.
query = "SELECT * FROM wdb_customer WHERE customer_id= @customer_id";
if (OpenConnection() == true)
{
//MySqlCommand myComm = connection.CreateCommand();
MySqlCommand myComm = new MySqlCommand(query, connection);
MySqlDataReader Reader;
//myComm.CommandText = query;
//myComm.Connection = connection;
myComm.Parameters.AddWithValue("@customer_id", customer_id);
Reader = myComm.ExecuteReader();
while (Reader.Read())
{
lastname = Reader["customer_name"].ToString();
phone_number = Reader["customer_telephone"].ToString();
}
Reader.Close();
}
CloseConnection();
//return lastname;
}
}