Read multiple columns data from SQL query ( 1 row )
Hi all.
I am using an SQL express where I’ve designed a table named User_DB_Main
I have also created a procedure that makes some selections :
@UserId varchar(20),
@UserPassword varchar(30)
AS
/*- Check if UserID exists*/
SELECT COUNT(UID) AS uTrue FROM dbo.User_DB_Main WHERE UId = @UserId
/*- Check if UserPassword matches*/
SELECT COUNT(UID) AS uPass FROM dbo.User_DB_Main WHERE UId = @UserId AND UPass = @UserPassword
/*- Select only required fields*/
SELECT UId AS qUserId, UAIL AS qUAIL, UPE AS qUPE,UPED AS qUPED,UPLUD AS qUPLUD,
UPEDBW AS qUPEDBW, UPRTL AS qUPRTL,ULLI AS qULLI, ULC AS qULC, UType AS qUType
FROM
dbo.User_DB_Main
WHERE UId = @UserId
What I am trying to do is to sore each column value ( from the first row found ) in to a variable / string.
The vb code looks like this :
'Use the connection string found in Web.config file
Dim User_DB_Conn As New SqlConnection(ConfigurationManager.AppSettings("SQLconn_User_DB"))
'Call stored procedure designed for user authentication
Dim SQL_command As New SqlCommand("ValidateUserAccount_User_DB_Main", User_DB_Conn)
SQL_command.CommandType = CommandType.StoredProcedure
'Define strings / values passed in
Dim UserId_String, UserPassword_String As SqlParameter
UserId_String = SQL_command.Parameters.AddWithValue("@UserId", SqlDbType.Char)
UserPassword_String = SQL_command.Parameters.AddWithValue("@UserPassword", SqlDbType.Char)
'Set the direction of the parameters...input, output, etc
UserId_String.Direction = ParameterDirection.Input
UserPassword_String.Direction = ParameterDirection.Input
'Set the value(s) of the parameters to the respective source controls
UserId_String.Value = UsrID.Text
UserPassword_String.Value = UsrPass.Text
'Declare variables that will store values read from SQL
'Main Data
' uTrue - "User is found"
Dim uTrue As Integer
' uPass - "Password is valid"
Dim uPass As Integer
'Open DB
If User_DB_Conn.State = ConnectionState.Closed Then
User_DB_Conn.Open()
End If
'Create SQL Data Reader
Dim SQL_Reader As SqlDataReader = SQL_command.ExecuteReader() '(CommandBehavior.CloseConnection)
'Read data from SQL
'Store values
'Main data
While SQL_Reader.Read
uTrue = SQL_Reader("uTrue")
uPass = SQL_Reader("uPass"
End While
SQL_Reader.Close()
It all works fine if I am trying to sore only 1 value something like :
While SQL_Reader.Read
uTrue = SQL_Reader("uTrue")
' uPass = SQL_Reader("uPass"
End While
A solution is to close the connection after storing 1 value and then executeReader again for storing the second … and so on.
Does anyone have a better idea ?
I am new in .NET.
Any help will be much appreciated.
Thank You