Tuesday, March 20, 2012

Problem of vale setting

I have this .NET (VB) code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim myConnection As New SqlConnection(ConnectionString)
myConnection.Open()

Dim CommandText As String = "SELECT firstName FROM students Where Students.ID='" & IDinput & "' "

Dim myCommand As New SqlCommand(CommandText, myConnection)

'****this the problem, somthing is mising here******
Dim name As String = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

end sub

The SQL qurey return only one record each time (only one student name with specific ID number)

I want that variable "name" will get the value of the "firstName" filed.

How to do it??Use ExecuteScalar instead of ExecuteReader. ExecuteScalar is designed for situations like this where you want the query to return a single value.

No comments:

Post a Comment