变量未返回正确信息 [英] variables not returning correct information
本文介绍了变量未返回正确信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定为什么我的变量pin,firstName和balance没有给出正确的值.当我运行该应用程序并选择一个帐号时,我可以输入图钉,并且messageBox显示欢迎",但不显示成员的姓名.然后,当我按下平衡按钮时,每次都会打印0.我不确定为什么会这样.
I''m not sure why my variables pin, firstName, and balance aren''t giving correct values. When I run the app and pick an account number, I am able to enter a pin, and the messageBox displays welcome, but not the member''s name. Then, when I hit the balance button, it prints 0 everytime. Im not sure why this is happening.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLDll;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
private Connection myConnection;
private Statement myStatement;
private ResultSet myResultSet;
String databaseURL = "http://www.boehnecamp.com/phpMyAdmin/razorsql_mysql_bridge.php";
private string pin ,firstName, userAccountNumber;
private double balance;
private double withdraw = 0;
private double deposit = 0;
bool buttonClicked, buttonClicked2;
public Form1()
{
InitializeComponent();
try
{
//connect to database
SQL sql = new SQL();
myConnection = sql.getConnection(databaseURL);
//create Statement for executing SQL
myStatement = myConnection.createStatement(databaseURL);
loadAccountNumbers();
updateBalance();
}
catch (Exception)
{
Console.WriteLine("Cannot connect to database server");
}
//close statement and database connection
myStatement.close();
myConnection.close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void retrieveAccountInformation()
{
//get account info
try
{
myResultSet = myStatement.executeQuery("SELECT pin, " +
"firstName, balanceAmount FROM accountInformation " +
"WHERE accountNumber = '" + userAccountNumber + "'");
//get next result
if (myResultSet.next())
{
pin = myResultSet.getString("pin");
firstName = myResultSet.getString("firstName");
balance = myResultSet.getDouble("balanceAmount");
}
myResultSet.close(); //close myResultSet
}//end try
catch (Exception)
{
Console.WriteLine("Error in retrieveAccountInformation");
}
}// end method retrieveAccountInformation
//update database after withdrawing
private void updateBalance()
{
//update balance in database
try
{
myStatement.executeUpdate("UPDATE accountInformation" +
" SET balanceAmount = " + balance + " WHERE " +
"accountNumber = '" + userAccountNumber + "'");
}
catch (Exception)
{
Console.WriteLine("Error in updateBalace");
}
}//end method updateBalance
private void accountNumberComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
accountNumberComboBox.Enabled = false;
numberTextField.Text = " ";
messageTextArea.Text = "Enter your PIN #.";
buttonDone.Enabled = true;
}
private void buttonEnter_Click(object sender, EventArgs e)
{
retrieveAccountInformation();
numberTextField.Text = " ";
buttonEnter.Enabled = false;
buttonBalance.Enabled = true;
buttonWithdraw.Enabled = true;
buttonDeposit.Enabled = true;
messageTextArea.Text = "Welcome" + firstName;
updateBalance();
}
private void buttonBalance_Click(object sender, EventArgs e)
{
retrieveAccountInformation();
updateBalance();
messageTextArea.Text=(balance.ToString());
//display balance to messageTextArea
}
}
}
推荐答案
你好,
代替这个,
Hello,
Instead of this,
//get next result
if (myResultSet.next())
{
pin = myResultSet.getString("pin");
firstName = myResultSet.getString("firstName");
balance = myResultSet.getDouble("balanceAmount");
}
试试这个,
Try this,
//get next result
if (myResultSet.ReadFirst())
{
pin = myResultSet.getString("pin");
firstName = myResultSet.getString("firstName");
balance = myResultSet.getDouble("balanceAmount");
}
这篇关于变量未返回正确信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文