?
Solved

Cannot convert sql query result in interger

Posted on 2016-09-30
3
Medium Priority
?
53 Views
Last Modified: 2016-09-30
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)
0
Comment
Question by:Marina K
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41823265
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

0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 total points
ID: 41823266
You should use
[...]where id = (Select MAX(ID) from Userrecords) [...]

Open in new window

1
 

Author Closing Comment

by:Marina K
ID: 41823272
Thanks! This worked well...thank you very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question