FormatException: Input string is not in a current format

W

weird0

localhost.BankingService bservice = new localhost.BankingService();
int
balance=Convert.ToInt32(bservice.CheckBalance(InfoAccount.pincode)); //
Line of Error

How can i remove this error?

I get the error above. Here are the definitions for the functions
CheckBalance() and SearchDatabase().

[WebMethod]
public string CheckBalance(string pincode)
{
string Balance = "";
string pinCode = pincode.ToString();
// create a Command,Connection and Reader object
if (SearchDatabase(Convert.ToInt32(pinCode))>0)
{
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();

Object returnValue;

// Search for the pincode

cmd.CommandText = "SELECT acc_balance as SqlBalance FROM
Account_Information where pincode_atm='" + pincode + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
returnValue = cmd.ExecuteScalar();
Balance=returnValue.ToString();

sqlConnection1.Close();
}

return Balance;

}


[WebMethod]
public int SearchDatabase(int pincode)
{
int RowCount = 0;
// create a Command,Connection and Reader object

System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();
SqlDataReader reader;

// Search for the pincode

cmd.CommandText = "SELECT pincode_atm FROM Account_Information
where pincode_atm='" + pincode.ToString() + "';";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Loop through and find the number of rows
while (reader.Read())
{
RowCount++;
}

reader.Close();
sqlConnection1.Close();

return RowCount;
}
 
J

Jon Skeet [C# MVP]

weird0 said:
localhost.BankingService bservice = new localhost.BankingService();
int
balance=Convert.ToInt32(bservice.CheckBalance(InfoAccount.pincode)); //
Line of Error

How can i remove this error?

Well, what do you want it to do if you haven't provided a valid number?
(By the way, using a parameter called "pincode" and a variable name of
"pinCode" is just *begging* for maintenance issues. Mind you, as one of
them is just the result of calling ToString() on the other, and they're
both strings in the first place, it doesn't make much odds. I'd
recommend using parameterised queries rather than inserting arbitrary
strings into SQL though... Oh, and use "using" statements to close
things even in the face of exceptions.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top