Question

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.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top