1

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?

2
  • Did you debug your code? What exact value is being returned from the getInfo method? Are you seeing the message box Please check your fields ? You can not use parameter for the columns in Select statement. Commented Nov 30, 2019 at 5:06
  • I’m not sure your query makes any sense. You are passing @infoReq as a string and then just returning it, then trying to convert it to a scalar? Commented Nov 30, 2019 at 5:07

1 Answer 1

2

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

can you shortly explain to me the purpose of the %? it worked
The % is the sql wildcard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.