Results 1 to 12 of 12

Thread: Help required

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Exclamation Help required

    Hi All,

    I am creating a project using VB, and I am new to VB, I am still a beginner, I need help in retrieving email address from database when I enter the username, I have added the code below, please review it and let me know, basically when I enter the username it has pull the email address and display in the label
    vb.net Code:
    1. Private cn As Odbc.OdbcConnection  'this is the connection object
    2.     Private rs As ADODB.Recordset   'this is the recordset object
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.  
    6.         Dim con As New OleDb.OleDbConnection
    7.         Dim dbProvider As String
    8.         Dim dbSource As String
    9.         Dim ds As New DataSet
    10.         Dim da As OleDb.OleDbDataAdapter
    11.         Dim sql As String
    12.        
    13.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    14.         dbSource = "Data Source = C:\auto_email\test.mdb"
    15.  
    16.         con.ConnectionString = dbProvider & dbSource
    17.  
    18.         con.Open()
    19.  
    20.         sql = "Select Email_address from test where Uname= & uname"
    21.         da = New OleDb.OleDbDataAdapter(sql, con)
    22.         MsgBox("Database is now open")
    23.  
    24.     End Sub
    In the above I am stuck at the sql statement as I have no idea how to refer the text box as the username i.e "& uname" it is the name of the text box, is it the right way, also then as I informed I need to paste the email address in the label and the name is label2, I am trying it as " label2.text=da" but I get a error message, I am using Visual basic professional 2010.

    Please help me in this.....
    Looking forward for your reply.
    Regards,
    Srinirao
    Last edited by Hack; Nov 18th, 2011 at 07:17 AM. Reason: Added Highlight Tags

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Help required

    Welcome to VBForums

    First of all this questio nis better suited in the .Net section of VBForums (I'll report that to the Mods, they can move this one).

    If you have a TextBox called "uname", you can access its contents by using uname.text
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Help required

    Thread moved from the 'Office Development' forum to the 'VB.Net' (VB2002 and later) forum.

    Thanks opus

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Exclamation Re: Help required

    Hi Opus,
    Thanks for your reply, uname.text worked, now I am not able paste it in a label as I have no idea to how to transfer the email to a string as if I transfer it I am getting the query it self, Please find the code after modifications.

    Private cn As Odbc.OdbcConnection 'this is the connection object
    Private rs As ADODB.Recordset 'this is the recordset object
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim pri As String 'defined to transfer the email address which was retrieved from sql query

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = C:\auto_email\test.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open()

    sql = "Select Email_address from test where uname=" &uname.Text
    da = New OleDb.OleDbDataAdapter(sql, con)
    pri = sql 'assigning the values of sql to pri
    Label2.Text = pri 'printing the email address

    MsgBox("Database is now open")

    End Sub

    But I am getting the sql query at the label i.e Select Email_address from test where uname= srinirao

    Looking forward for your reply.

    Regards,
    Srinirao

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help required

    Try something like
    vb.net Code:
    1. Private cn As Odbc.OdbcConnection 'this is the connection object
    2. Private rs As ADODB.Recordset 'this is the recordset object
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5. Dim con As New OleDb.OleDbConnection
    6. Dim dbProvider As String
    7. Dim dbSource As String
    8. Dim ds As New DataSet
    9. Dim da As OleDb.OleDbDataAdapter
    10. Dim sql As String
    11. Dim pri As String 'defined to transfer the email address which was retrieved from sql query
    12.  
    13. dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    14. dbSource = "Data Source = C:\auto_email\test.mdb"
    15.  
    16. con.ConnectionString = dbProvider & dbSource
    17.  
    18. con.Open()
    19.  
    20. sql = "Select Email_address from test where Uname = @UName "
    21. con.Parameters.AddWithValue("@UName", uname.Text)
    22.  
    23. Dim reader As OleDbDataReader = con.ExecuteReader()
    24. If reader.HasRows = True Then 'make sure there is data returned
    25.     Label2.Text = reader.GetString(0)
    26. Else
    27.     MessageBox.Show("No records matched your search criteria.")
    28. End If
    29. End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Exclamation Re: Help required

    Hi Hack,

    Thanks for your suggestion.

    I tried to use the code that u provided but I got error messages for
    Code:
     
    con.Parameters.AddWithValue("@UName", Badge.Text)
     Dim reader As OleDb.OleDbDataReader = con.ExecuteReader()
    error message where

    Parameters is not a member of system.data.oledb.oledb.connection
    ExecuteReader is not a member of system.data.oledb.oledb.connection


    Please help

    Regards,
    Srinirao

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help required

    Thats because I free typed all that from memory and I missed a step - sorry.

    vb.net Code:
    1. 'change
    2. sql = "Select Email_address from test where Uname = @UName "
    3. con.Parameters.AddWithValue("@UName", uname.Text)
    4.  
    5. Dim reader As OleDbDataReader = con.ExecuteReader()
    6.  
    7. 'to
    8. sql = "Select Email_address from test where Uname = @UName "
    9. Dim command As New OleDbCommand(sql, con)
    10.  
    11. command.Parameters.AddWithValue("@UName", uname.Text)
    12.  
    13. Dim reader As OleDbDataReader = command.ExecuteReader()

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Re: Help required

    Hi,

    That was awesome.... That really worked, now I am stuck at another part I need to provide condition to check if the database is being connected or not also I need help in sending out a email to the same email address using yahoo or gmail server properties, please help me in this

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Exclamation Re: Help required

    Hi,

    I have tried the code below, please let me know if it is the right one to send a email because I get a error message which I have taken a screen shot and I have attached in the post.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim con As New OleDb.OleDbConnection
            Dim dbProvider As String
            Dim dbSource As String
            Dim ds As New DataSet
            'Dim da As Boolean
            Dim sql As String
            'Dim pri As String 'defined to transfer the email address which was retrieved from sql query
           
            dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
            dbSource = "Data Source=C:\auto_email\Test.mdb"
            con.ConnectionString = dbProvider & dbSource
            con.Open()
            sql = "Select Email_address from Delhi where uname = @uname "
            If uname.Text = "" Then
                Label2.Text = ""
                MsgBox("Please enter a username to continue")
            Else
                Dim command As New OleDbCommand(sql, con)
                command.Parameters.AddWithValue("@uname", uname.Text)
                Dim reader As OleDbDataReader = command.ExecuteReader()
                If reader.Read = True Then 'make sure there is data returned
                    Label2.Text = reader.GetString(0)
                    Try
                        Dim SmtpServer As New SmtpClient()
                        Dim mail As New MailMessage()
                        SmtpServer.Credentials = New  _
              Net.NetworkCredential("[email protected]", "password")
                        SmtpServer.Port = 587
                        SmtpServer.Host = "smtp.gmail.com"
                        mail = New MailMessage()
                        mail.From = New MailAddress("[email protected]")
                        mail.To.Add("@label2.text")
                        mail.Subject = "Test Mail"
                        mail.Body = "This is for testing SMTP mail from GMAIL"
                        SmtpServer.Send(mail)
                        MsgBox("mail send")
                    Catch ex As Exception
                        MsgBox(ex.ToString)
                    End Try
                Else
                    Label2.Text = ""
                    MessageBox.Show("No records matched your search criteria or it is a invalid username please check and insert it once again.")
                End If
            End If
        End Sub
    Attached Images Attached Images  

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Help required

    Using this line:
    Code:
    mail.To.Add("@label2.text")
    I think you are setting the to-address to "@label2.text" and not to the string of @label2.text. Try it without the "".
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Exclamation Re: Help required

    Hi Opus,

    I tried it without the codes, I got a different exception error, I have attached the same.

    Regards,
    Srinirao
    Attached Images Attached Images  

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    13

    Question Re: Help required

    Hi,

    I did not get any reply, I am trying to sending email thro outlook, so please help me in doing this, also I am using Visual Studio 2010, please find the code below and let me know is any changes are required, or if u have a different solution that would be appreciated...

    Code:
       
    Imports System.Net.Mime.MediaTypeNames
    Imports Outlook = Microsoft.Office.Interop.Outlook
    Imports System.Net.Mail
    
    
    Public Class Email
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Namespace OutlookAddIn2
            Class Sample
    
                Shared Sub SendEmailFromAccount(ByVal application As Outlook.Application, _
                    ByVal subject As String, ByVal body As String, ByVal recipients As String, ByVal smtpAddress As String)
    
                    ' Create a new MailItem and set the To, Subject, and Body properties.
                    Dim newMail As Outlook.MailItem = DirectCast(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
                    newMail.To = recipients
                    newMail.Subject = subject
                    newMail.Body = body
    
                    ' Retrieve the account that has the specific SMTP address.
                    Dim account As Outlook.Account = GetAccountForEmailAddress(application, smtpAddress)
                    ' Use this account to send the e-mail.
                    newMail.SendUsingAccount = account
                    newMail.Send()
                End Sub
    
                Shared Function GetAccountForEmailAddress(ByVal application As Outlook.Application, ByVal smtpAddress As String) As Outlook.Account
    
                    ' Loop over the Accounts collection of the current Outlook session.
                    Dim accounts As Outlook.Accounts = application.Session.Accounts
                    Dim account As Outlook.Account
                    For Each account In accounts
                        ' When the e-mail address matches, return the account.
                        If account.SmtpAddress = smtpAddress Then
                            Return account
                        End If
                    Next
                    Throw New System.Exception(String.Format("No Account with SmtpAddress: {0} exists!", smtpAddress))
                End Function
    
            End Class
        End Namespace
    End Sub
    End Class
    Regards,
    srinirao
    Last edited by srinirao88; Nov 25th, 2011 at 02:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width