Send Email -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Send Email -

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    190

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,952

    Re: Send Email -

    is the error line here? Email = reader("Email")
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: Send Email -

    Which line do you get the error on?

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center