Howard Hee
Howard Hee

Reputation: 929

Get SQL single value from query as String

I have the following code in my code behind:

Dim name As String
name.text= Staff.LoadName(StaffID)

The following query in my Class:

Public Function LoadName(ByVal ID As String)
    Dim ds As New DataSet
    Dim SQL As String = ""
    SQL="select name from Staff where StaffID='" & ID & "' " 
    ds = Common.QueryDataByDataset(SQL)
    ds.Tostring()
    Return ds
End Function

But the name.Text doesn't show the value. How to I get the single value and convert it to string to display? Thanks

Upvotes: 0

Views: 390

Answers (1)

Doug Lamb
Doug Lamb

Reputation: 475

I am guessing the Common.QueryDataByDataset is from your library or some third party. Assuming it is executing the query and populating the dataset, you should be able to change the last two lines of the LoadName function to this:

String name = ds.Tables.Item("Staff").Rows(0)("name").ToString()
return name

You should add error handling in this method. For example, check to make sure the query returns exactly one result. Also, this method appears to be susceptible to SQL injection: http://en.wikipedia.org/wiki/SQL_injection

Upvotes: 2

Related Questions