Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2007, 09:02 AM
Authorized User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get maximum value from database

hi,

i want to know how to implement this query in VB.NET

"Select max(Sno) from Student"

manisha

 
Old March 29th, 2007, 09:06 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Are you asking how to setup database connectivity or are you asking if there is somethign special you need to do to execute that query??

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 29th, 2007, 02:24 PM
Authorized User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually in this code

sub button1_Click()

dim con as sqlconnection
dim cmd as sqlcommand
dim str as string

con=new sqlconnection("server=localhost;integrated security=true;database=pubs")
con.open

str="insert authors(au_id,au_name) values(@au_id,@au_name)"
cmd=new sqlcommand(str,con)
cmd.parameters.add("@au_id","1")

'How to get max au_id field value from table

cmd.parameters.add("@au_name",txtauname.text)
cmd.executenonquery()
con.close()

end sub

 
Old March 29th, 2007, 02:32 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You need to execute the select statement that you provided in your initial post, however, you need to use ExecuteReader() instead of ExecuteNonQuery()

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 29th, 2007, 02:53 PM
Authorized User
 
Join Date: Feb 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default


this code is working properly only i want to know,
If in a table there are 1-5 au_id then how to get 5(max) au_id

 
Old March 29th, 2007, 03:02 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

SELECT max(au_id) From [table]

Like i said, execute the command using ExecuteReader() (although, since you just want a number you would be better off using ExecuteScalar() )

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old June 17th, 2007, 12:50 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hellow Manisha,Try this code.
private sub Button1_click(......) Handles btninsert.click
Dim con As new SqlConnection
con.ConnectionString = "Data Source = ....; Database=dbname;Integrated
                       Security=ISPI"
Dim cmd as New SqlCommand
cmd.CommandText="Select max(sno) from Students"
cmd.CommandType=CommandType.Text
cmd.Connection = con
con.Open()
MsgBox(cmd.ExecuteScalar)
con.Close()
End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Maximum Users Brendan Bartley Access 1 March 15th, 2007 02:34 PM
Maximum Number of Controls? Aaron Edwards Visual Studio 2005 0 March 3rd, 2007 06:04 PM
parameter: 6 selections maximum krema Reporting Services 0 February 20th, 2006 05:07 AM
Row Maximum bmains SQL Server 2000 2 October 7th, 2004 08:39 PM
Finding the maximum and using it outside the IF jrwarwick XSLT 2 October 17th, 2003 03:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.