Get the data copied after Ctrl+ C operation to a variable - c#

I have some data in notepad and have performed Sendkeys("Ctrl+C") using keyboard library.I want to get the copied data to a variable in C# code.
How to get the data to variable programmatically.
I tried clipboard.GetText() API and it is giving empty string.

Your could get any data using Clipboard.GetDataObject().
See the example at msdn

Related

Set a permanent value of a variable without using a database

I don't know how to describe it thoroughly in the title, but I need to set a permanent value of a variable/flag once a process has return true and maybe set some flag in the program itself the value rather than saving it to database. And once that variable/flag has already have that value then the program won't run the process again and just use the value. Is it possible? I'm using VB.Net. I can't use the database because database can be overridden and change values by using query. Thanks in advance!
You can simply use binary/XML serialization in a file to save the state of that variable through your program. Every time you restart your app you can access the value from that file to get its current state.
You can look at this example - http://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php
Basically, you will not save the value in the database but in a file. Anyways you need to persist the value somewhere.
Some ways below
You did not specify if you are afraid that your application or another one could change the value
How I would do it
My ideas below
1)You could use an xml file for example and zip a copy of it with a strong password. Every time you update the first xml you will update also the encrypted zipped xml.You can use a FileSystemWatcher and capture any file change, so if something/someone has changed the file you just get a new copy from the zip
2)You can store the value in the DB and add a trigger to prevent delete/update
for example
-- delete trigger
CREATE TRIGGER Function_Value_Deleted
ON [dbo].[FunctionsValueTb]
AFTER DELETE
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (
SELECT [Flag] FROM deleted
)
BEGIN
ROLLBACK;
RAISERROR ('Record deletion is not allowed...', 16, 1);
END
END
*You can use also use THROW rather than RAISERROR
**Do the same for the insert and update actions
***You can also store the value into a log table or send an email
I found myself in a situation quite similar to yours a couple of days ago.
In the end, I decided to use the settings functionaly provided by .NET: it is easy to use and maintain, and so far it has given me good results.
Yo can see here what I am talking about:
Best practice to save application settings in a Windows Forms Application
That thread refers to C# but is easily applicable for VB.NET: I just had to follow the same steps in order to add the Settings file:
Right click on the project in Solution Explorer, choose Properties.
Select the Settings tab, click on the hyperlink if settings doesn't
exist. Use the Settings tab to create application settings. Visual
Studio creates the files Settings.settings and
Settings.Designer.settings that contain the singleton class Settings
inherited from ApplicationSettingsBase
And then, from my code, I use the settings like this:
Dim lastExecDate As Date = My.Settings.LastSuccessfulExecution
lastExecDate = lastExecDate.AddDays(1)
// Perform my next execution and do other stuff
My.Settings.LastSuccessfulExecution = lastExecDate
My.Settings.Save()
Next time you retrieve the parameter LastSuccessfulExecution, it will have the updated value.
One more remark, as stated in the post that I linked above:
Note that you need to set the scope property of your settings. If you
select Application scope then Settings.Default.< your property > will
be read-only
Finally, I see that you are using this to store the expiration date of a product, so you don't want the user messing around with it. According to this post, the actual values of the parameters are stored in an Application Data user folder. It is somehow obfuscated since it is not that easy to find and besides it contains a hash on its name... I don't know if that is well hidden enough for you.
If you want the value only to exist in memory when the application is running then you can use the main thread of the application and use:
int slotData = randomGenerator.Next(1, 200);
//to set the data
Thread.SetData(Thread.GetNamedDataSlot("SomeDataKey"), slotData);
//to get the data
int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("SomeDataKey"));
Or you can use the Windows Registry if your app only runs on Windows, if not then you would have to write the value/object to a file and read it from there.

How to get value of textbox in C# when it assign by jquery

I am new to jQuery.
I have a web service which returns some data. I assign this data to a textbox using jQuery but when I want to access this value in C#, it generates an exception incorrect format.
This is my jquery code.
$('#Ltrl_GSalary').html(GrossSalary);
$('#Ltrl_NetSalary').html(GrossSalary-parseInt(TotalDeduction));
This code works fine, it displays data.
I also tried:
$('#Ltrl_GSalary').val(GrossSalary);
$('#Ltrl_NetSalary').val(GrossSalary-parseInt(TotalDeduction));
This code does not display any data.
This is my C# code
salary.NetSalary = Ltrl_NetSalary.Text.ToDecimal();
salary.GrossSalary = Ltrl_GSalary.Text.ToDecimal();
This causes the exception incorrect format.
Try to use Hidden field to capture data
You can use Convert.ToInt32 for this purpose.
It will handle format exception. And check with value in Ltrl_NetSalary.Text
Try to use:
salary.NetSalary = Convert.ToInt32(Ltrl_NetSalary.Text);
salary.GrossSalary = Convert.ToInt32(Ltrl_GSalary.Text);
hope Its Helpful.

Copy object using Clipboard and paste it as object or text

I want to copy object ( List ) to clipboard and use it in my application and outside of it.
In my application, I want to be able to "paste" data without any additional operation. Outside I need CSV data.
I tried to achieve it by using DataObject. I can convert this list to formatted string - so i think it's easy to get CSV. I stored random string in DataObject, (just for test). The problem is storing List object in Clipboard. DataObject contains specific type ( checked using DataObject.GetFormats() ), but when i try to get this object from clipboard, I get null.
I've found this, while it solves my problem, I need to serialize/deserialize.
Is there any way to store and retrieve object the normal way ?
Clipboard.SetDataObject(list);
var deslist = Clipboard.GetDataObject().GetData(list.GetType());

Read Contents of a Sharepoint Multiline Textbox in Visual Studio 2010

I am reading the contents of a list into a number of string variables so as to be able to build the content an HTML email.
The method I am using (and works for single line text boxes) is:
string Manager = item["Manager"].ToString();
However, when I try the same method for a multi-line text box I get the error:
Object reference not set to an instance of an object.
Can someone advise the best way in which to get the content of a multi-line text box into a variable that I can then reference for the other functionality mentioned?
The exception indicates that your item["Manager"] indexer does not find a column by that type. When it then tries to call .ToString() on it (null) it will result in the exception you are seeing.
Make sure you have the correct name (internal name, not display name!) for your notes column.
Or perhaps your instance of SPListItem the item variable is null.
When you try to get a OOB SharePoint Field it's best practice to use the SPBuiltInFieldId class or the ID of the Field instead of the internal name.
item[SPBuiltInFieldId.Title]

What this exception means? C# winforms

I'm trying to display images from a database table to my dataGridView control.
Other cells display image. But some doesn't and throws this exception:
What does this exception means? What should I do to fix it?
In all likelihood, it means that your image data is invalid. Try extracting the image data by another means, like a simple console app, and pass that to Image.FromStream. Or serialize it out and try to open it in an external image editor. You might get a more informative error message that way.
According to MSDN your call will result in this exception if the Stream has an invalid format
If some invalid in bytes entered then it wont display properly.
If you tried with a Insert query , then this problem may come. Try to insert image from code only.Hope it will work fine.
It appears to indicate that there was an error creating the image from a Stream (probably handled within ADO.NET rather than in your code). If it's working for some images and not others then start by double-checking the data in the database, and look for differences between them.
There might be the error from database.Please once check your Code again
If, as you say, the images display in some rows but not others it's the data, not your code - The images have been captured incorrectly.

Categories

Resources