Bc30311 visual basic and vb.net value of type cannot be converted to string.

I saw these 2 string examples on this link.

https://www.dotnetperls.com/convert-list-string-vbnet

I doing the first one using String.Join

I created a new page called Default.aspx and put a label called Label1 on the page

Then on this page Default.aspx.vb in the page load event I put this code.
Then when I run the page it works fine and the string is displayed on my label.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Create list of three strings. Dim vals As List(Of String) = New List(Of String) vals.Add("dot") vals.Add("net") vals.Add("perls") ' Use string join function that receives IEnumerable. Dim value As String = String.Join(",", vals) 'Show String On Label Label1.Text = value End Sub

Open in new window

I have a .net 3.5 ASP.NET Web Forms application using VB.

In the page load event of my page I put the same code.

But I get this error message:

Bc30311 visual basic and vb.net value of type cannot be converted to string.

Any idea of why I'm getting this error message saying Listing(Of String) cannot be converted to String?

  1. Aug 3rd, 2020, 12:52 PM #1

    Bc30311 visual basic and vb.net value of type cannot be converted to string.

    Thread Starter

    New Member

    Bc30311 visual basic and vb.net value of type cannot be converted to string.


    Value of type 'String' cannot be converted to 'MailMessage'.

    Hi all

    I can't solve this problem: Value of type 'String' cannot be converted to 'MailMessage'.

    Here is my code:

    Code:

    Sub send_mail() Dim email As New MailMessage() Dim strBody As String = "" Dim emails As String = "" Try Dim SMTP As New SmtpClient("mail.xxx.xo") SMTP.UseDefaultCredentials = True SMTP.Credentials = New System.Net.NetworkCredential("", "xxxxxx") email.From = New MailAddress("") If String.IsNullOrEmpty(emails) Then emails = "" Else emails = TextBox4.Text End If emails = TextBox4.Text email.To.Add(emails) email.Subject = TextBox2.Text strBody = TextBox1.Text & TextBox5.Text & TextBox6.Text & TextBox7.Text & TextBox8.Text strBody = "bla bla bla" & vbCrLf strBody += "bla " strBody += TextBox1.Text & vbCrLf strBody += TextBox2.Text & vbCrLf strBody += TextBox5.Text & vbCrLf strBody += TextBox6.Text & vbCrLf strBody += TextBox7.Text & vbCrLf strBody += TextBox8.Text & vbCrLf email.Body = strBody SMTP.Send(emails) MsgBox("mail send") Catch ex As SmtpException MsgBox(ex.StatusCode & vbCrLf & ex.Message & ex.ToString, vbCritical, "SMTP Error!") End Try End Sub

    When I compile, here is my problem : SMTP.Send(emails)

    Last edited by si_the_geek; Aug 3rd, 2020 at 01:03 PM. Reason: added Code tags


  2. Aug 3rd, 2020, 01:03 PM #2

    Re: Value of type 'String' cannot be converted to 'MailMessage'.

    That is because you have a typo... and the fact you have two variables with similar names means it wasn't obvious.

    These are the relevant variables:

    Code:

    Dim email As New MailMessage() Dim emails As String = ""

    You were accidentally using the wrong one. Instead of this:
    ...it should have been:


  3. Aug 3rd, 2020, 01:04 PM #3

    Re: Value of type 'String' cannot be converted to 'MailMessage'.

    This is a perfect example of why you should be using descriptive and easily distinguishable variable names.

    emails is defined as a string variable (one that shouldn't even need to exist based on your code, by the way)

    email is defined as a MailMessage


  4. Aug 3rd, 2020, 08:13 PM #4

    Re: Value of type 'String' cannot be converted to 'MailMessage'.

    Bc30311 visual basic and vb.net value of type cannot be converted to string.
    Originally Posted by OptionBase1
    Bc30311 visual basic and vb.net value of type cannot be converted to string.

    This is a perfect example of why you should be using descriptive and easily distinguishable variable names.

    emails is defined as a string variable (one that shouldn't even need to exist based on your code, by the way)

    email is defined as a MailMessage

    Couldn't agree more. If email refers to a MailMessage object then the only sensible use for emails would be for an array or collection of MailMessage objects. If that emails variable is actually a String containing email addresses then, at the very least, it should be emailAddresses. Other options would be things like recipients.


  5. Aug 4th, 2020, 01:08 AM #5

    Bc30311 visual basic and vb.net value of type cannot be converted to string.

    Thread Starter

    New Member

    Bc30311 visual basic and vb.net value of type cannot be converted to string.


    Re: Value of type 'String' cannot be converted to 'MailMessage'.

    Stupid mistake
    Thank you for all your help!