Solved

Cannot convert sql query result in interger

Posted on 2016-09-30
3
40 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
3 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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 22

Accepted Solution

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

Open in new window

1
 

Author Closing Comment

by:Marina K
Comment Utility
Thanks! This worked well...thank you very much
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now