# Mailto link from database field



## bootsy (May 12, 2008)

Hello,

I am trying to use the mailto function to pull in the email address from a SQL table and I don't know what I am missing as far as the syntax and coding. Here is what I have:

<a href="mailto:dr["Email"]">" + dr["Email"].ToString() + "</a>

Thanks


----------



## V0lt (Sep 14, 2002)

What preprocessor are you using? Where is the rest of your source?


----------



## bootsy (May 12, 2008)

Fox said:


> What preprocessor are you using? Where is the rest of your source?


*Here is the entire code:*


```
using System.Web.UI.HtmlControls;

namespace HSI
{
public partial class ourpeople_workercomp : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadWC();
         }

        private void LoadWC()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            DataTable dt = Utils.GetDataTableForUI("dbo.WorkersCompEmpSEL", null);

            foreach (DataRow dr in dt.Rows)
            {


                sb.Append("<li>" + dr["EmployeeName"].ToString() + ", " + dr["EmployeeTitle"].ToString() + "<br>" + dr["Phone"].ToString() + "<br>" + dr["Email"].ToString() + "<br>" + dr["Location"].ToString() + "</li>");
              
            }
                      

            this.workersUL.InnerHtml = sb.ToString();
        }


      
        }
    }
```


----------



## V0lt (Sep 14, 2002)

Ah, ok, I'm not a .net or ASP guy, but perhaps you need to concatenate the email address variable with the beginning of the tag. What output do you get with the way you have it currently?


----------



## bootsy (May 12, 2008)

What I get now on the page is I get a link and when you hover over it at the bottom right corner of the browser it says mailto:dr[. When you click on the link it opens a new message email and in the To field it says dr[.


----------



## V0lt (Sep 14, 2002)

Yup, that's because you haven't enclosed the variable. 

You should try changing this :


```
<a href="mailto:dr["Email"] ">" + dr["Email"] .ToString() + "</a>
```
To this:


```
<a href="mailto:"+dr["Email"]+">"+dr["Email"].ToString()+"</a>
```
I'm not sure how you have this in your display page (the code quote was for the behind-the-scenes stuff, right?), but I think that's basically how it needs to change in order to work.

Let me know if you have success with this.


----------



## bootsy (May 12, 2008)

I think we are closer but what happens now when I use your updated code is that the a href seems to be missing something. The quotes between the open tag of the a href and closing tags don't match up. This is what I have right now.

*"<br>"<a href="mailto:"+dr["Email"]+">"+dr["Email"] .ToString()+"</a><br>"*

Something seems to be missing in this code.


----------



## bootsy (May 12, 2008)

I figured it out. The code should look like this:

"<br><a href='mailto:" + dr["Email"].ToString() + "'>" + dr["Email"].ToString() 

Fox, thanks for your help on this. I appreciate it.


----------



## V0lt (Sep 14, 2002)

Ah, ok, I think you are missing some quotation marks. You'll need to escape some of them. I do not know how to do it for this language (this is asp, right?), but I would expect it's a \ or something. Also, the " between <br> and <a href seems like it's extraneous.

If the escaped characters work with a slash, it might look something like this:


```
"<br><a href=\"mailto:\""+dr["Email"] +"\">"+dr["Email"] .ToString()+"</a><br>"
```
I know it looks sort of ugly, but it's necessary in order to tell the server which quotations are ending strings for processing, and which ones are going to be sent out into the processed HTML.


----------

