Yuval Peretz
Yuval Peretz

Reputation: 131

Error "Input string was not in a correct format" with sql

I'm having trouble with getting an int value from my sql data base:

if(Convert.ToDouble(dbh.getInfo("firstTime", username))==1)

and I also tried:

if((int)dbh.getInfo("firstTime", username)==1)

and this is the getInfo function:

public object getInfo(string infoReq, string username)
        {
            string query = "select (@infoReq) from AccountDB where username like @username";
            try
            {
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.Parameters.AddWithValue("@infoReq", infoReq);
                    cmd.Parameters.AddWithValue("@username", username);
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
            catch (Exception e){

            }
            return MessageBox.Show("Please check your fields");
        }

dbh is a DBHandler type which that is where that function is from of course

in the sql data base, the DataType of that @infoReq for this matter is a bit (in sql: [firstTime] BIT NOT NULL)

what is the problem with my code?

Upvotes: 1

Views: 1297

Answers (1)

zip
zip

Reputation: 4061

Try this:

public object getInfo(string infoReq, string username)
{
    string query = "select @infoReq from AccountDB where username like '%@username%'";
    try
    {
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@infoReq", infoReq);
            cmd.Parameters.AddWithValue("@username", username);
            con.Open();
            return cmd.ExecuteScalar();
        }
    }
    catch (Exception e){

    }
    return MessageBox.Show("Please check your fields");
}

Upvotes: 2

Related Questions