To make it work with minimal changes, try *“select ****COUNT(*)*** from…”, however this is not the only known approach.
Visual Studio C# Error: Input string was not in correct format

VAer-4038
771
Reputation points
The error shows up when running into " int count = Convert.ToInt32(cmd.ExecuteScalar())", Data Type for Access UserID is short text. What goes wrong?
Thanks.
private void btnConnect_Click(object sender, EventArgs e)
{
OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);
Cn.Open();
string select = "SELECT * from TUser WHERE UserID ='" + Environment.UserName + "'";
using (OdbcCommand cmd = new OdbcCommand(select, Cn))
{
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("Existed!");
}
else
{
MessageBox.Show("No existed");
}
//}
}
}
Developer technologies Visual Studio Other
5,450 questions
Accepted answer
1 additional answer
Sort by: Most helpful
-
Sergio Parra Guerra 1 Reputation point MVP Volunteer Moderator
2021-01-01T18:11:35.423+00:00 Hi! ExecuteScalar returns an object.
Sometimes it can return a DBNull representing a non-existent value, which is not casteable to Int32.