Link to home
Start Free TrialLog in
Avatar of Marina K
Marina K

asked on

Cannot convert sql query result in interger

I am trying to get the points value from the database. I am using an auto-increment ID so that the points i take are from the last log. Here is my code:

 private void LevelTwoPartTwo_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Marina\\Desktop\\FaceNameGame\\facenamegame\\mygamedb.mdf;Integrated Security=True;Connect Timeout=30");
            SqlCommand cmd = new SqlCommand("select Points from Userrecords where ID='MAX(ID) and Username='" + LoginInfo.UserID + "'", con);
           
            con.Open();
            point = Convert.ToInt32(cmd.ExecuteScalar());
            //cmd.ExecuteNonQuery();
            con.Close();
           
            string[] files = Directory.GetFiles(@"C:\Users\Marina\Desktop\FaceNameGame\images\Level2\used", "*.jpg", SearchOption.AllDirectories);
            PhotoPicBx.Load(files[i]);
            NameTxtBx.Focus();
            Pointlbl.Text = point.ToString();
           

But it always throws exception that MAX('ID') cannot be converted to interger...what can i do?
(Sorry for my bad English, not my native language)
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

SQL Syntax ix incorrect !! Try this ..

select Points from 
Userrecords 
where  Username = LoginInfo.UserID
GROUP BY ID
HAVING ID = MAX(ID)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marina K
Marina K

ASKER

Thanks! This worked well...thank you very much