Question : VB.Net - Database connection.

Hi all,

I am new to VB.Net and i have managed to write the code below. I am using VB.NET 2010 my question is how do I pull the email address down from the SQL table tbl_users into txtEmailAddress.text.

Thanks in advanced.

Regards


Dim sqlConn As SqlClient.SqlConnection
        Dim sqlCmd As SqlClient.SqlCommand
        Dim strConnection As String
        ' Dim sqlDR As SqlDataReader


        'Get connection string from App.Config (Must import system.configuration and add reference system configuration
        strConnection = ConfigurationManager.ConnectionStrings("hoh_schoolwear_v1._0.My.MySettings.pitl_hoh_schoolwear").ConnectionString

        sqlConn = New SqlConnection(strConnection)
        sqlCmd = New SqlCommand("Select * FROM tbl_users WHERE username ='" & txtUsername.Text & "'", sqlConn)
        sqlConn.Open()
        '  sqlCmd.ExecuteReader()
        sqlCmd.ExecuteNonQuery()
        'sqlDR = sqlCmd.ExecuteReader


       txtEmailAddress.Text = (I NEED CODE HERE TO RETRIVE DATA FROM A TABLE.

        sqlConn.Close()

Answer : VB.Net - Database connection.

You can use a datareader
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Dim sqlConn As SqlClient.SqlConnection
        Dim sqlCmd As SqlClient.SqlCommand
        Dim strConnection As String

        'Get connection string from App.Config (Must import system.configuration and add reference system configuration
        strConnection = ConfigurationManager.ConnectionStrings("hoh_schoolwear_v1._0.My.MySettings.pitl_hoh_schoolwear").ConnectionString

        sqlConn = New SqlConnection(strConnection)
        sqlCmd = New SqlCommand("Select * FROM tbl_users WHERE username ='" & txtUsername.Text & "'", sqlConn)
        sqlConn.Open()

Dim dr As SqlClient.SqlDataReader
dr = sqlCmd .ExecuteReader
While dr.Read()
txtEmail.Text = dr("email")
txtUsername.Text = dr("username") 
...

End While
Catch
End Try
dr.Close()
sqlCon.Close()
Random Solutions  
 
programming4us programming4us