-
October 31st, 2012, 06:24 AM
#1
Send Email -
Hi,
I have the following code;
Code:
Private Function findauthoremail(ByVal name As String) As String
Dim ConString As String = (ConfigurationManager.ConnectionStrings("CCMSConnectionString").ConnectionString)
Dim MySQL As String = "Select Email from dbo.TblUser where user_full_name=@user_full_name"
Dim mijncon As New Data.SqlClient.SqlConnection(ConString)
mijncon.Open()
Dim Cmd As New Data.SqlClient.SqlCommand(MySQL, mijncon)
Cmd.Parameters.AddWithValue("@user_full_name", name)
Dim reader As Data.SqlClient.SqlDataReader = Cmd.ExecuteReader()
Dim Email As String
Email = reader("Email")
Return Email ' where email is the value you obtained from the database as string
End Function
Which I need to read the email address from the database...
this sub which triggers the email submission;
Code:
Using message As New MailMessage()
'Dim msgbody As String = String.Empty
Dim htmlFile As String = Server.MapPath("~/EmailTemplate/ChangeRequest.htm")
Dim sreader As New StreamReader(htmlFile)
Dim msgBody As New StringBuilder(sreader.ReadToEnd)
sreader.Close()
message.From = New MailAddress(findauthoremail(cmbchngreq.SelectedItem.ToString))
message.[To].Add(New MailAddress("hh@hotmail.com"))
message.Subject = "Change Request Enquiry: " + Trim(txtref.Text.ToString())
message.IsBodyHtml = True
message.Body = msgBody.ToString
Dim client As New SmtpClient()
client.Send(message)
sreader.Dispose()
End Using
At run time I receive the error -
System.InvalidOperationException: Invalid attempt to read when no data is present
Any assistance.. Thanks
Last edited by dr223; October 31st, 2012 at 06:34 AM.
-
October 31st, 2012, 11:18 AM
#2
Re: Send Email -
is the error line here? Email = reader("Email")
-
November 1st, 2012, 07:19 AM
#3
Re: Send Email -
Which line do you get the error on?
-
November 1st, 2012, 07:39 AM
#4
Re: Send Email -
Code:
Private Function findauthoremail(ByVal name As String) As String
Dim ConString As String = (ConfigurationManager.ConnectionStrings("CCMSConnectionString").ConnectionString)
Dim MySQL As String = "Select Email from dbo.TblUser where user_full_name=@user_full_name"
Dim mijncon As New Data.SqlClient.SqlConnection(ConString)
mijncon.Open()
Dim Cmd As New Data.SqlClient.SqlCommand(MySQL, mijncon)
Cmd.Parameters.AddWithValue("@user_full_name", name)
Dim reader As Data.SqlClient.SqlDataReader = Cmd.ExecuteReader()
reader.Read()
Dim Email As String
Email = reader("Email")
Return Email ' where email is the value you obtained from the database as string
End Function
Add in the highlighted line.
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
|