Browsing 7239 questions and answers with Jon Skeet

Insert date time value into SQL Server database

I'm trying to insert date time picker value into a DATETIME column in my database. Here's my code.. myconstr = "Data Source=wind;Initial Catalog=TestDB;Integrated...
Jon Skeet
people
quotationmark

Your code is equivalent to: var parameter = new SqlParameter("@mydate", SqlDbType.DateTime); var value = MyDTP01.Value; parameter.Value = value; cmd.Parameters.Add(value); You want to add the parameter, not the value.... more 7/29/2014 8:06:24 AM

people

StringBuilder.ToString() throws OutOfMemoryException

I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException. StringBuilder SB = new...
Jon Skeet
people
quotationmark

What is the reason for the OOM while creating a new string Because you're running out of memory - or at least, the CLR can't allocate an object with the size you've requested. It's really that simple. If you want to avoid the errors,... more 7/29/2014 7:59:33 AM

people

Why am I getting a System.InvalidCastException for my own types?

My method ImplementingInterface below, gets all of my own classes implementing my custom interface, IInstaller. private static IEnumerable<Assembly> MyAssemblies { get...
Jon Skeet
people
quotationmark

You're searching for types - so your result is an IEnumerable<Type>. Those Type objects don't implement your interface - instances of the types would implement the interface. So your code is trying to do something a little... more 7/29/2014 6:04:10 AM

people

Get num of days between two dates, keep num of days the same when date is changed

User wants to edit start date. Days between start date and end date must always be the same. For example Start Date is originally 28/07/2014 and End Date is 01/08/2014 The...
Jon Skeet
people
quotationmark

There are two aspects to this: Working out the number of days between two dates Personally, I'd use my Noda Time API for this, but you could still use the BCL's DateTime type - subtract one DateTime from the other to get a TimeSpan, and... more 7/28/2014 3:05:50 PM

people

Java copying a variable's value instead of referencing it

I know this is a basic Java question yet I have never seen a full solution to this problem. Basically, I want to copy another variable's value into another variable and then have...
Jon Skeet
people
quotationmark

Here, if tmpCommand's value will change to modelItem.getName() value since it references commandName. No, it won't. The value of tmpCommand is just the value of commandName at the time of the assignment. That's really simple to... more 7/28/2014 2:25:14 PM

people

Cast return type in postsharp

I want to use PostSharp follow code: class Program { [PostModerator] public static ReturnState<Person> GetPerson() { return new...
Jon Skeet
people
quotationmark

Yes, that's because you're creating a ReturnState<object>, which isn't a ReturnState<Person>. Options: Change your PostModerateAttribute to find out the correct return type, and create an instance of that instead. Hard-code... more 7/28/2014 11:46:07 AM

people

"Does Not Contain" dynamic lambda expression

The code below does "Contain" expression: private static Expression<Func<T, bool>> Contains<T>(string property, string value) { var obj =...
Jon Skeet
people
quotationmark

A "does not contain" expression is exactly the same as a "does contain" expression - but wrapped with a unary negation expression. So basically you want: // Code as before var doesNotContain = Expression.Not(contains); return... more 7/28/2014 10:19:47 AM

people

XML Root Element Parsing

I am parsing an xml file using the following which works well but my question is how do I get the value from the tag <themename> because I am using oXmlNodeList for my loop....
Jon Skeet
people
quotationmark

I'd use LINQ to XML to start with. Something like this: var doc = XDocument.Load(...); var themeName = doc.Root.Element("themename").Value; Guid themeGuid = Guid.NewGuid(); foreach (var element in... more 7/28/2014 10:11:50 AM

people

Get input from console for enum type

How do you get input from console for enum types in java? I have this class: class enumTest { public enum Color { RED, BLACK, BLUE } BufferedReader input = new...
Jon Skeet
people
quotationmark

Every enum has an automatically generated static valueOf method which parses a string. So you can use: String colorName = input.readLine(); Color color = Color.valueOf(colorName); However, this will throw an exception if the given name... more 7/28/2014 9:45:55 AM

people

Setting property doesn't work

I have a class Banner public class Banner { public virtual int Id { get; protected set; } public virtual string Url { get; set; } public virtual string Path { get;...
Jon Skeet
people
quotationmark

The problem isn't the fact that you're setting the property - it's that you're trying to use the result of setting the property as the input for setting banner itself. You just want: banner.Disabled = false; more 7/28/2014 8:43:26 AM

people