Solved

Cannot convert sql query result in interger

Posted on 2016-09-30
3
43 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 23

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 22

Accepted Solution

by:
Ferruccio Accalai earned 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

15 Experts available now in Live!

Get 1:1 Help Now