0

I have these methods in an asp.net web form:

public partial class currencies : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

public class Rate
{
    public string to { get; set; }
    public string from { get; set; }
    public double rate { get; set; }
}

double ConvertedAmount { get; set; }

public void Convert(object sender, EventArgs e)
{
    DateTime theDate = DateTime.UtcNow;
    string todayDateTime = theDate.ToString("f");
    double amount = 0d;
    if (double.TryParse(txtAmount.Text.Trim(), out amount))
    {
        string url = string.Format("http://rate-exchange.appspot.com/currency?from={0}&to={1}", ddlFrom.SelectedItem.Value, ddlTo.SelectedItem.Value);
        WebClient client = new WebClient();
        string rates = client.DownloadString(url);
        Rate rate = new JavaScriptSerializer().Deserialize<Rate>(rates);
        ConvertedAmount = amount * rate.rate;
        Label1.Text = ddlFrom.SelectedItem.Value + ": " + amount;
        Label2.Text = ddlTo.SelectedItem.Value + ": " + ConvertedAmount;
        Label3.Text = "Rate as at " + todayDateTime + " is:";
        Label4.Text = " 1 " + ddlFrom.SelectedItem.Value + " = " + rate.rate + " " + ddlTo.SelectedItem.Value;

    }
    else
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Invalid amount value.');", true);
    }
}


protected void Button1_Click(object sender, EventArgs e)
{   
    Clipboard.SetText(ConvertedAmount.ToString());
}
}

when I click on button 1 to copy the result to clipboard I get the exception: Current thread must be set to single thread apartment (STA) mode before calling OLE. It is the first time I deal with something like this. What I am supposed to do to solve this problem? Thanks for helping.

FeliceM
  • 4,163
  • 9
  • 48
  • 75
  • I can't think of any scenario where it would be useful for a **server-side** web-application to use the clipboard... – Marc Gravell Nov 17 '13 at 14:09

1 Answers1

1

As MSDN states,

The Clipboard class can only be used in threads set to single thread apartment (STA) mode.

Setting STA in ASP.NET should not be required. But are you sure you want to operate on clipboard on the server side? Do you understand that it will operate on the server clipboard, not the client's one?

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • What I would like to achieve is to copy that value so that the user can paste it in another control in the website or on his document. Any suggestion? – FeliceM Nov 17 '13 at 13:16
  • The best way to accomplish that is to just return the data in a textbox and then use javascript to select it so it can be easily copied. You won't be able to access the clipboard from the server and on the client you will still need Flash to have any chance of doing it crossbrowser. – Karl-Johan Sjögren Nov 17 '13 at 13:20
  • Automatic copying to clipboard without user's knowledge is some kind of security issue so it is not so easy. Refer for example [this question](http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript). – Konrad Kokosa Nov 17 '13 at 13:21
  • It is not automatic copy, the user has to click the button – FeliceM Nov 17 '13 at 13:22
  • Click or not to click, the user will not see what is being copied to the clipboard, that's why I see it as some kind of _automatic_ opearation. Nevertheless, look for example at [zClip](http://www.steamdev.com/zclip/) library. – Konrad Kokosa Nov 17 '13 at 13:25
  • @FeliceM no matter *how* it happens : to do that, you need to code *at the client*. The C# you are writing runs on the server. – Marc Gravell Nov 17 '13 at 14:10