locked
Get the receiver e-mail address from a MS Access DB's column ! RRS feed

  • Question

  • User-706647060 posted

    Hi. I am tring to get the receivers' e-mail address from a Ms access (mdb) database but I am failing. I had another thread but it started to get longer and I decided to open another one. Let's make a summary. Below is my VB code;

    Imports System.Net.Mail
    Imports System.IO
    Imports System.Net
    Imports System
    Imports System.Data
    Imports System.Data.Common
    Imports System.Data.OleDb
    Partial Class mail_Default
        Inherits System.Web.UI.Page
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Gonder()
        End Sub
        Public Function GetHtml(ByVal sUrl As String) As String
            Dim oWebRequest As WebRequest = System.Net.HttpWebRequest.Create(sUrl)
            Dim oStreamReader As New StreamReader(oWebRequest.GetResponse().GetResponseStream())
            Dim sResult As String = oStreamReader.ReadToEnd()
            oStreamReader.Close()
            Return sResult
        End Function
        Public Function ReplaceHtml() As String
            Dim body As String = GetHtml(Server.MapPath("HTMLPage.htm"))
            body = body.Replace("FromName", TextBox1.Text)
            body = body.Replace("FromSur", TextBox2.Text)
            body = body.Replace("MesRep", TextBox3.Text)
            Return body
        End Function
        Private Sub Gonder()
    
            Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
            Dim URL As String = Request.ServerVariables("HTTP_REFERER")
    
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MyDatabase.mdb"
            Dim conn As New OleDbConnection(connectionString)
            Dim sql As String = "SELECT * FROM Email"
            Dim cmd As New OleDbCommand(sql, conn)
            conn.Open()
            Dim reader As OleDbDataReader
            reader = cmd.ExecuteReader()
            While (reader.Read())
                Console.WriteLine(reader.Item("EmailsTo"))
                'Console.Write(reader.GetString(0).ToString() & " ,")
                'Console.Write(reader.GetString(1).ToString() & " ,")
                Console.WriteLine("")
            End While
            reader.Close()
            conn.Close()
    
            message.IsBodyHtml = True
            message.From = New System.Net.Mail.MailAddress("info@tugberkugurlu.com")
            message.To.Add(reader("EmailsTo").ToStriing() & ",")
            message.Bcc.Add("info@tugberkugurlu.com")
            message.Subject = "Work & Travel Application Form From " + URL
            message.Body = ReplaceHtml()
            ' message.Attachments.Add(New System.Net.Mail.Attachment("privacy.doc"))
            Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
            ' Try
            smtpClient.Send(message)
            Response.Redirect("proceeded.aspx")
    End Sub


     

    And My page URL for this is http://www.tugberkugurlu.com/mail/ToDB.aspx

    I keep getting this error on this project;

     

    Server Error in '/' Application.

    No data exists for the row/column.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: No data exists for the row/column.

    Source Error:

    Line 56:         message.IsBodyHtml = True
    Line 57:         message.From = New System.Net.Mail.MailAddress("info@tugberkugurlu.com")
    Line 58:         message.To.Add(reader("EmailsTo").ToStriing() & ",")
    Line 59:         message.Bcc.Add("info@tugberkugurlu.com")
    Line 60:         message.Subject = "Work & Travel Application Form From " + URL

    Source File: d:\inetpub\tugberkugurlu.com\www\mail\ToDB.aspx.vb    Line: 58

    Anybody has a idea on thşs. My column, table and databse names are correct but I am getting this error. Thanks a lot ;)

    Wednesday, December 2, 2009 8:33 AM

Answers

  • User-126244515 posted

    Change

    While (reader.Read())   
        Console.WriteLine(reader.Item("EmailsTo"))   
        'Console.Write(reader.GetString(0).ToString() & " ,")   
        'Console.Write(reader.GetString(1).ToString() & " ,")   
        Console.WriteLine("")   
    End While  


    To

    Dim toAdresses As String = ""
    
    While reader.Read() 
        toAddresses &= reader("EmailsTo").ToStriing() & ","
    End While
    
    toAddresses = toAddresses.Substring(0, toAddresses.Length -2)
    
    

    Change

    message.To.Add(reader("EmailsTo").ToStriing() & ",") 


     

    To

    message.To.Add(toAdresses) 


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 2, 2009 9:34 AM

All replies

  • User-126244515 posted

    When you try to read the value from the DataReader, it has been closed, and it must be opened for that to happen. You also have a typo in offending line of code,

    message.To.Add(reader("EmailsTo").ToStriing() 

    Change to:

    message.To.Add(reader("EmailsTo").ToString() 

    You loop through the rows/items in the DataReader and displays the values of the EmailsTo column, but you don't do anything with it. Is it so, that you need to get a list of recipients from a single column?

    Wednesday, December 2, 2009 8:53 AM
  • User-706647060 posted

    Hi there ! Thanks for your reply.

    ""Change to:

    message.To.Add(reader("EmailsTo").ToString() ""

     

    I did the above one and corrected the typo but It didn't change. I am getting error still.

    ""You loop through the rows/items in the DataReader and displays the values of the EmailsTo column, but you don't do anything with it. Is it so, that you need to get a list of recipients from a single column?""

     

    Yes, what I want to do is exactly what you indicated. Can you help me about this. Thanks !

     

    Wednesday, December 2, 2009 9:11 AM
  • User-126244515 posted

    Change

    While (reader.Read())   
        Console.WriteLine(reader.Item("EmailsTo"))   
        'Console.Write(reader.GetString(0).ToString() & " ,")   
        'Console.Write(reader.GetString(1).ToString() & " ,")   
        Console.WriteLine("")   
    End While  


    To

    Dim toAdresses As String = ""
    
    While reader.Read() 
        toAddresses &= reader("EmailsTo").ToStriing() & ","
    End While
    
    toAddresses = toAddresses.Substring(0, toAddresses.Length -2)
    
    

    Change

    message.To.Add(reader("EmailsTo").ToStriing() & ",") 


     

    To

    message.To.Add(toAdresses) 


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 2, 2009 9:34 AM
  • User-706647060 posted
        Private Sub Gonder()
    
            Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
            Dim URL As String = Request.ServerVariables("HTTP_REFERER")
    
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MyDatabase.mdb"
            Dim conn As New OleDbConnection(connectionString)
            Dim sql As String = "SELECT * FROM Email"
            Dim cmd As New OleDbCommand(sql, conn)
            conn.Open()
            Dim reader As OleDbDataReader
            reader = cmd.ExecuteReader()
    
            Dim toAdresses As String = ""
            While (reader.Read())
                toAddresses &= reader("EmailsTo").ToStriing() & ","
            End While
            toAddresses = toAddresses.Substring(0, toAddresses.Length - 2)
    
            reader.Close()
            conn.Close()
            message.IsBodyHtml = True
            message.From = New System.Net.Mail.MailAddress("info@tugberkugurlu.com")
            message.To.Add(toAdresses)


     Hi there I did as above what you said but I am still getting error. I cannot open the page now;

     

    Server Error in '/' Application.

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30451: Name 'toAddresses' is not declared.

    Source Error:

    Line 48:         Dim toAdresses As String = ""
    Line 49:         While (reader.Read())
    Line 50:             toAddresses &= reader("EmailsTo").ToStriing() & ","
    Line 51:         End While
    Line 52:         toAddresses = toAddresses.Substring(0, toAddresses.Length - 2)

    Source File: d:\inetpub\tugberkugurlu.com\www\mail\ToDB.aspx.vb    Line: 50

    Wednesday, December 2, 2009 9:43 AM
  • User-126244515 posted

    Yeah, there is typo in the toAddress variable, sorry about that. It was taken off the top of my head... Anyway, please make sure all instances are spelt like this: toAddress

    Wednesday, December 2, 2009 9:46 AM
  • User-706647060 posted

    Hi there. Thanks for all of your help. I corrected the typo and I got this error after postback;

     

    Server Error in '/' Application.

    Public member 'ToStriing' on type 'String' not found.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.MissingMemberException: Public member 'ToStriing' on type 'String' not found.

    Source Error:

    Line 48:         Dim toAddress As String = ""
    Line 49:         While (reader.Read())
    Line 50:             toAddress &= reader("EmailsTo").ToStriing() & ","
    Line 51:         End While
    Line 52:         toAddress = toAddress.Substring(0, toAddress.Length - 2)

    Source File: d:\inetpub\tugberkugurlu.com\www\mail\ToDB.aspx.vb    Line: 50

    Wednesday, December 2, 2009 9:53 AM
  • User-126244515 posted

    Come on, you know what that means... (hint---a previous typo already fixed)  ;-)

    Wednesday, December 2, 2009 9:58 AM
  • User-706647060 posted

    It was just my idiocity Laughing Thanks dude, it works perfectly. I will keep in touch with you. Thanks again Smile 

    Wednesday, December 2, 2009 10:08 AM