Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to get the highest value from my custID column in my Customer table and to then save this out to an integer.

VB
Dim getHighAccountNo As SqlCommand = AccountsTableAdapter.Connection.CreateCommand()

        getHighAccountNo.CommandType = CommandType.Text

        custID = getHighAccountNo.CommandText = "SELECT MAX(custID) AS HighestAccountID FROM Customer"

        'custID = getHighAccountNo.CommandText = "SELECT IDENT_CURRENT('Customer') as LastIdentity"

        lblHighestAccountNo.Text = custID.ToString()


I'm not sure what I have done wrong as the code above always returns 0.

Any help is appreciated,

Thanks Glen.
Posted

You define the command text, but I cannot see where you actually execute the command and fetch its result.

Maybe:
C#
getHighAccountNo.CommandText = "SELECT MAX(custID) AS HighestAccountID FROM Customer"
int cutomerId = getHighAccountNo.ExecuteScalar();
lblHighestAccountNo.Text = customerId.ToString();


Hope this helps.
 
Share this answer
 
v3
You are missing "ExecuteScalar". The code below should do!

VB
Dim getHighAccountNo As SqlCommand = AccountsTableAdapter.Connection.CreateCommand()

        getHighAccountNo.CommandType = CommandType.Text

        getHighAccountNo.CommandText = "SELECT MAX(custID) AS HighestAccountID FROM Customer"
        custID = getHighAccountNo.ExecuteScalar()

        'custID = getHighAccountNo.CommandText = "SELECT IDENT_CURRENT('Customer') as LastIdentity"

         
        lblHighestAccountNo.Text = custID.ToString()
 
Share this answer
 
Thanks guys! That worked! I am silly! Should have noticed that one!

Thanks again!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900