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
776
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
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Answer accepted by question author
1 additional answer
Sort by: Most helpful
-
Sergio Parra Guerra 1 Reputation point MVP Volunteer Moderator2021-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.