VB .net code for accessing data from MS SQL server 2000 RRS feed

  • Question

  • Hi. I’m using VB .net 2003 windows form and MS SQL Server 2000 personal edition. I have created a table “candidate” in SQL Server with 14 fields and of which ID and SDATE are two fields. Their data types are as follows.

    ID- varchar(10), not null – contains values 0 to 9 but in string format
    SDATE – datetime, not null – contains server system date in mm/dd/yyyy hr:min Tongue Tiedec format

    The sample table fields are as follows
    ID                         SDATE
    1006200701         6/10/2007 20:54:56
    1006200702         6/10/2007 22:43:23
    1406200701         6/14/2007 12:22:40
    1506200701         6/15/2007 10:45:34
    1506200702         6/15/2007 11:00:02

    I want to read the last ID entered in the table with the help of SDATE and store it in a String type variable in VB net code.

    The query for this is: SELECT ID FROM candidate WHERE SDATE = (SELECT MAX(SDATE) FROM candidate)

    But I want to store the result (ID) in a String variable in VB .net  code using INLINE query. Then what should be the code for that?

    Tuesday, June 19, 2007 7:34 PM


  • You can use the ExecuteScalar Function here..


    Code Snippet

    Dim cmd As New System.Data.SqlClient.SqlCommand()

    cmd.Connection = conn

    cmd.CommandType = CommandType.Text

    cmd.CommandText = "SELECT ID FROM candidate WHERE SDATE = (SELECT MAX(SDATE) FROM candidate)"


    Dim result As String = String.Empty

    result = String.Format("{0}", cmd.ExecuteScalar())



    Wednesday, June 20, 2007 5:29 AM