retrieve first row and display the second column in a textbox

ssfftt

Well-known member
Joined
Oct 27, 2005
Messages
163
Programming Experience
1-3
[solved]retrieve first row and display the second column in a textbox

is it possible to write a query in sqlcommand that retrieves the second column (e.g. CNAME) of the first record of a sql database? plz help
 
Last edited:
Example Table in a database:

Field1 Field2 Field3
Rec1Fld1 Rec1Fld2 Rec1Fld3
Rec2Fld1 Rec2Fld2 Rec2Fld3
Rec3Fld1 Rec3Fld2 Rec3Fld3

Do you...
  1. Want only Rec1Fld2 no matter what
    • SELECT TOP 1 Field2 FROM tblExample
  2. Want Rec#Fld2 based on Field1
    • SELECT TOP 1 Field2 FROM tblExample WHERE Field1 = @paramValue
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim [/COLOR][/SIZE][SIZE=2]sqlConn [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlConnection("Conn String")
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strsql [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "SELECT..."
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlCmd [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlCommand(strsql, sqlConn)
[/SIZE]
[SIZE=2][COLOR=#008000]'Used if you used second sql example
[/COLOR][/SIZE][SIZE=2]sqlCmd.Parameters(0).Value = "Value of Field1 to search"
 
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtText.Text = sqlCmd.ExecuteScalar()[/SIZE]
You may need to do some type conversions depending on what you have in the table.

----------------------
Example done in VB.Net 2003 Enterprise Architect/MS SQL Server 2000
 
Last edited:
Sorry, I got lazy and forgot something.

If you're using the second example, you're going to need to add the parameter to sqlCmd and assign it a value.

It's friday; gimme a break :)
 
Back
Top