178,284 questions
161
votes
8
answers
339k
views
How do you do Impersonation in .NET?
Is there a simple out of the box way to impersonate a user in .NET?
So far I've been using this class from code project for all my impersonation requirements.
Is there a better way to do it by using ...
-2
votes
1
answer
2k
views
Reach control from another page. ASP.Net
Well, this is the situation...
I have a element (<h2 id="test"></h2>) in Page1.aspx, and I want to change it from Page2.aspx (administration zone for the user...), kind of...
test....
430
votes
14
answers
302k
views
The entity cannot be constructed in a LINQ to Entities query
There is an entity type called Product that is generated by entity framework.
I have written this query
public IQueryable<Product> GetProducts(int categoryID)
{
return from p in db.Products
...
221
votes
9
answers
175k
views
Is it possible to dynamically compile and execute C# code fragments?
I was wondering if it is possible to save C# code fragments to a text file (or any input stream), and then execute those dynamically? Assuming what is provided to me would compile fine within any Main(...
1042
votes
7
answers
655k
views
decimal vs double! - Which one should I use and when? [duplicate]
I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision.
My question is when should a use a double and when should I use a decimal type?
Which type is ...
786
votes
36
answers
522k
views
How do you handle multiple submit buttons in ASP.NET MVC Framework?
Is there some easy way to handle multiple submit buttons from the same form? For example:
<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" value="Send" ...
66
votes
9
answers
536k
views
C# compiler error: "not all code paths return a value"
I'm trying to write code that returns whether or not a given integer is divisible evenly by 1 to 20, but I keep receiving the following error:
error CS0161: 'ProblemFive.isTwenty(int)': not all ...
232
votes
32
answers
547k
views
Reading Excel files from C#
Is there a free or open source library to read Excel files (.xls) directly from a C# program?
It does not need to be too fancy, just to select a worksheet and read the data as strings. So far, I've ...
656
votes
14
answers
562k
views
How can I save application settings in a Windows Forms application?
What I want to achieve is very simple: I have a Windows Forms (.NET 3.5) application that uses a path for reading information. This path can be modified by the user, by using the options form I ...
50
votes
2
answers
11k
views
TextBoxFor displaying initial value, not the value updated from code [duplicate]
I have an MVC application that displays a value. This is the controller:
public ActionResult Index(DataSites DataSiteList)
{
if (DataSiteList.Latitude != null)
{
DataSites test = new ...
1676
votes
24
answers
1.9m
views
How to Sort a List<T> by a property in the object
I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a list of this Order class:
List<Order> objListOrder = new List<Order>();
...
419
votes
18
answers
91k
views
Casting vs using the 'as' keyword in the CLR
When programming interfaces, I've found I'm doing a lot of casting or object type conversion.
Is there a difference between these two methods of conversion? If so, is there a cost difference or how ...
182
votes
5
answers
196k
views
How do I obtain the frequencies of each value in an FFT?
I have an FFT result. These are stored in two double arrays: a real part array and an imaginary part array. How do I determine the frequencies that correspond to each element in these arrays?
In ...
229
votes
11
answers
178k
views
Nesting await in Parallel.ForEach [duplicate]
In a metro app, I need to execute a number of WCF calls. There are a significant number of calls to be made, so I need to do them in a parallel loop. The problem is that the parallel loop exits ...
2374
votes
41
answers
1.2m
views
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding?
I'm going to encrypt the string. I can encrypt it without converting, but I'd still like to know ...
1145
votes
28
answers
1.3m
views
How can I get the application's path in a .NET console application?
How do I find the application's path in a console application?
In Windows Forms, I can use Application.StartupPath to find the current path, but this doesn't seem to be available in a console ...
1655
votes
31
answers
381k
views
When should I use a struct rather than a class in C#?
When should you use struct and not class in C#? My conceptual model is that structs are used in times when the item is merely a collection of value types. A way to logically hold them all together ...
934
votes
13
answers
1.1m
views
Pass Method as Parameter using C#
I have several methods all with the same parameter types and return values but different names and blocks. I want to pass the name of the method to run to another method that will invoke the passed ...
932
votes
21
answers
958k
views
All possible array initialization syntaxes
What are all the array initialization syntaxes that are possible with C#?
541
votes
17
answers
593k
views
How To: Execute command line in C#, get STD OUT results
How do I execute a command-line program from C# and get back the STD OUT results? Specifically, I want to execute DIFF on two files that are programmatically selected and write the results to a text ...
37
votes
2
answers
30k
views
TypeNameHandling caution in Newtonsoft Json
On this link, in remarks section it's mentioned that:
TypeNameHandling should be used with caution when your application deserializes JSON from an external source. Incoming types should be validated ...
953
votes
29
answers
838k
views
How do I get the path of the assembly the code is in?
Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code.
Basically my unit test needs to ...
283
votes
12
answers
1.0m
views
IOException: The process cannot access the file 'file path' because it is being used by another process
I have some code and when it executes, it throws a IOException, saying that
The process cannot access the file 'filename' because it is being used by
another process
What does this mean, and ...
235
votes
8
answers
492k
views
How to parse strings to DateTime in C# properly?
I have date and time in a string formatted like that one:
"2011-03-21 13:26" //year-month-day hour:minute
How can I parse it to System.DateTime?
I want to use functions like DateTime.Parse() or ...
1871
votes
4
answers
124k
views
Is there a reason for C#'s reuse of the variable in a foreach?
When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example:
foreach (var s in strings)
{
query = query.Where(i => i.Prop ...