Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET clipboard copy
Copying the label text using LinkButton to clipboard says "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."
Here is my asp tag..
<asp:Label ID="label" runat="server" Text="text to be copied"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server"  OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
Code-behind: C#
protected void LinkButton1_Click(object sender, EventArgs e)
        { 
            Clipboard.SetText(label.Text);
        }
While click this LinkButton it shows error as I said above.. I could not use javascript / jquery in my application.. It is purely the server side web application. Please do post possible solution in c# itself.. If you all suggest me javascript clipboard, please provide me the solution that works out in all browser.
Posted 19-Mar-13 2:29am
Edited 19-Mar-13 20:04pm
v3
Comments
_Amy at 20-Mar-13 2:54am
   
Did you tried with separating threads? See my answer below.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try creating a separate thread for that.
Try like this:
private static string _Val;
public static string Val
{
    get { return _Val; }
    set { _Val = value; }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{            
    Val = label.Text;
    Thread staThread = new Thread(new ThreadStart (myMethod));
    staThread.ApartmentState = ApartmentState.STA;
    staThread.Start();
}
public static void myMethod()
{
    Clipboard.SetText(Val);
}
 

--Amit
  Permalink  
v2
Comments
Janani Muthaiyan at 20-Mar-13 2:56am
   
I tried.. It is saying that "staThread.ApartmentState" have been deprecated..
_Amy at 20-Mar-13 3:06am
   
Try my updated solution. Working for me, and should work for you.
Janani Muthaiyan at 20-Mar-13 3:11am
   
ya, tried. but again the same problem exists..
_Amy at 20-Mar-13 3:13am
   
Check
Thread.SetApartmentState Method[^]
Thread.ApartmentState Property[^]
May be this could help you.
 
--Amit
Janani Muthaiyan at 20-Mar-13 3:19am
   
It worked.. I used like this.. Thanks alot...
 
public ApartmentState ApartmentState { get; set; }
private static string _Val;
public static string Val
{
get { return _Val; }
set { _Val = value; }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Val = label.Text;
Thread newThread = new Thread(new ThreadStart(myMethod));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
}
public static void myMethod()
{
System.Windows.Forms.Clipboard.SetText(Val);
}
_Amy at 20-Mar-13 5:00am
   
Welcome. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You do realize that even if that worked, it would copy to the clipboard in the Server, not the Client? Which is not going to help anyone, since you have no idea how many different users are simultaneously trying to do the same thing...
 
You can do it in Javascript: http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript[^] but I tend to view apps that corrupt my clipboard with deep suspicion, personally!
  Permalink  
Comments
Janani Muthaiyan at 19-Mar-13 7:48am
   
@OriginalGriff.. I will be using session variable to maintain individual copies to each of the client. I just want to copy to the servers clipboard. It will be preferable if the error regarding [STA attribute] is solved...
OriginalGriff at 19-Mar-13 8:18am
   
The server only has the one clipboard - so if you copy anything to it from one client page, the next will overwrite it...and there is no guarantee that it will be preserved at all even if there is only one client at any time - anybody with access to the keyboard can overwrite it.
 
What are you doing that you think this is a good idea?
Janani Muthaiyan at 19-Mar-13 8:20am
   
I will be generating a random number which will be displayed in the label.. As soon it created it has to be copied when the user clicks the LinkButton.. He can paste it anywhere..
OriginalGriff at 19-Mar-13 9:27am
   
Not like that he can't - C# code is executed at the Server, not the Client, and saves to the Server clipboard, not the Client clipboard.
You need to run code on the Client to save to his clipboard - hence the Javascript link I gave you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The ClipBoard is in Client Side so you can't copy it through Code Behind.
 
You have to use a javascript. This would work only in IE.
 
if your textbox id is txtEmail
 
var email = document.getElementById('txtEmail');
 
var emailValue = email.value;
 
//copy to clipboard
window.clipboardData.setData('Text' , emailValue );
 
Please note you can't change the 'Text' keyword.
 
// get the clipboard data
var emailText = window.clipboardData.getData('Text');
 
//clear clipboard data
window.clipboardData.clearData();
 
for more info visit http://msdn2.microsoft.com/en-us/library/ms535220.aspx
  Permalink  
Comments
Janani Muthaiyan at 19-Mar-13 7:42am
   
@Rohit Kumar. I am in situation to not use javascript or jquery in my application
Rohit Kumar Mumbai at 19-Mar-13 7:46am
   
Try adding:
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
 
Immediately before your call to SetText.
Janani Muthaiyan at 19-Mar-13 8:48am
   
It throws exception like this "Failed to set the specified COM apartment state."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100