0

Consider the following code

 Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = @username and UserPass = @password", con)
 cmd.Parameters.AddWithValue("@username", login_username.Text)
 cmd.Parameters.AddWithValue("@password", hash_pass)

 Dim da As New SqlDataAdapter(cmd)

Suppose that there exist a column name status I want to store the result of status in a variable.

P.S I am a beginner with VB.NET so please bear with me

2 Answers 2

1

You can execute the statement on a SqlDataReader or fill DataTable. An example with SqlDataReader is

Dim reader As SqlDataReader
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =@username and UserPass=@password", con)
cmd.Parameters.AddWithValue("@username", login_username.Text)
cmd.Parameters.AddWithValue("@password", hash_pass)
reader = cmd.ExecuteReader()
Dim strStatus as String = ""
If reader.HasRows Then
    reader.Read()
    strStatus = reader.Item("status").ToString
End If

Here is the DataTable version

Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =@username and UserPass=@password", con)
cmd.Parameters.AddWithValue("@username", login_username.Text)
cmd.Parameters.AddWithValue("@password", hash_pass)
Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim dt As DataTable = New DataTable("TableA")
da.SelectCommand = cmd
da.Fill(dt)
Dim strStatus as String = ""
'you can process the DataTable in a for/for each loop or process a single row as follows
If dt.Rows.Count > 0 Then
    strStatus = dt.Rows(0).Item("status").ToString()
End If
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks....so i use Data reader when i want to retrieve column data and data table when i want to check if rows are return ?
No it is not so, both support retrieval and count. Usage depends upon requirement. For more details you may refer c-sharpcorner.com/Blogs/12509/… and mindfiresolutions.com/…
Thanks for the article but could you edit your answer to add the equivalent using Data Table
Check for DataTable version, added to the answer
@haraman....what is the Rows(0).I suppose it means for first data in column status.Am i right ?
|
0

If status returns a single cell value(single row in status column) then you can use following method

Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = @username and UserPass = @password", con)
cmd.Parameters.AddWithValue("@username", login_username.Text)
cmd.Parameters.AddWithValue("@password", hash_pass)
Dim status As String
status = IIf(IsDBNull(cmd.ExecuteScalar), "Not Available", cmd.ExecuteScalar)
        'IsDBNull is used tocheck whether cmd.ExecuteScalar is null or what
        'IIF() is used to handle null here, You can assign custom value for status variable if
        'select returns null value
        'If you dont need to use them its possible to write
        ' status = cmd.ExecuteScalar

See more about ExecuteScalar()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.