|
-
Nov 18th, 2011, 05:24 AM
#1
Thread Starter
New Member
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:
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
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"
da = New OleDb.OleDbDataAdapter(sql, con)
MsgBox("Database is now open")
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
-
Nov 18th, 2011, 05:36 AM
#2
-
Nov 18th, 2011, 05:54 AM
#3
Re: Help required
Thread moved from the 'Office Development' forum to the 'VB.Net' (VB2002 and later) forum.
Thanks opus
-
Nov 18th, 2011, 05:54 AM
#4
Thread Starter
New Member
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
-
Nov 18th, 2011, 07:27 AM
#5
Re: Help required
Try something like
vb.net Code:
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 "
con.Parameters.AddWithValue("@UName", uname.Text)
Dim reader As OleDbDataReader = con.ExecuteReader()
If reader.HasRows = True Then 'make sure there is data returned
Label2.Text = reader.GetString(0)
Else
MessageBox.Show("No records matched your search criteria.")
End If
End Sub
-
Nov 18th, 2011, 07:54 AM
#6
Thread Starter
New Member
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
-
Nov 18th, 2011, 08:02 AM
#7
Re: Help required
Thats because I free typed all that from memory and I missed a step - sorry.
vb.net Code:
'change
sql = "Select Email_address from test where Uname = @UName "
con.Parameters.AddWithValue("@UName", uname.Text)
Dim reader As OleDbDataReader = con.ExecuteReader()
'to
sql = "Select Email_address from test where Uname = @UName "
Dim command As New OleDbCommand(sql, con)
command.Parameters.AddWithValue("@UName", uname.Text)
Dim reader As OleDbDataReader = command.ExecuteReader()
-
Nov 19th, 2011, 12:34 AM
#8
Thread Starter
New Member
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
-
Nov 19th, 2011, 07:11 AM
#9
Thread Starter
New Member
-
Nov 19th, 2011, 01:01 PM
#10
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!
-
Nov 19th, 2011, 09:50 PM
#11
Thread Starter
New Member
-
Nov 25th, 2011, 02:17 PM
#12
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|