Contents tagged with .NET
-
The configuration section 'system.web.extensions' cannot be read because it is missing a section declaration
After upgrading an ASP.NET application from .NET Framework 3.5 to .NET Framework 4.0 I ran into the following error message dialog when trying to view any of the modules in IIS on the server.
What happened is during the upgrade, the web.config file was automatically converted for .NET Framework 4.0. This left behind an empty system.web.extensions section:
It was an easy fix to resolve, just remove the unused system.web.extensions section.
ERROR DIALOG TEXT:
---------------------------
ASP
---------------------------
There was an error while performing this operation.
Details:
Filename: \\?\web.config
Line number: 171
Error: The configuration section 'system.web.extensions' cannot be read because it is missing a section declaration
---------------------------
OK
--------------------------- -
VB.NET IF() Coalesce and “Expression Expected” Error
I am trying to use the equivalent of the C# “??” operator in some VB.NET code that I am working in.
This StackOverflow article for “Is there a VB.NET equivalent for C#'s ?? operator?” explains the VB.NET IF() statement syntax which is exactly what I am looking for... and I thought I was going to be done pretty quickly and could move on.
But after implementing the IF() statement in my code I started to receive this error:
Compiler Error Message: BC30201: Expression expected.
And no matter how I tried using the “IF()” statement, whenever I tried to visit the aspx page that I was working on I received the same error.
This other StackOverflow article Using VB.NET If vs. IIf in binding/rendering expression indicated that the VB.NET IF() operator was not available until VS2008 or .NET Framework 3.5. So I checked the Web Application project properties but it was targeting the .NET Framework 3.5:
So I was still not understanding what was going on, but then I noticed the version information in the detailed compiler output of the error page:
This happened to be a C# project, but with an ASPX page with inline VB.NET code (yes, it is strange to have that but that is the project I am working on). So even though the project file was targeting the .NET Framework 3.5, the ASPX page was being compiled using the .NET Framework 2.0. But why? Where does this get set? How does ASP.NET know which version of the compiler to use for the inline code?
For this I turned to the web.config. Here is the system.codedom/compilers section that was in the web.config for this project:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5" />
<providerOption name="WarnAsError" value="false" />
</compiler>
</compilers>
</system.codedom>Keep in mind that this is a C# web application project file but my aspx file has inline VB.NET code. The web.config does not have any information for how to compile for VB.NET so it defaults to .NET 2.0 (instead of 3.5 which is what I need).
So the web.config needed to include the VB.NET compiler option. Here it is with both the C# and VB.NET options (I copied the VB.NET config from a new VB.NET Web Application project file).
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5" />
<providerOption name="WarnAsError" value="false" />
</compiler>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="OptionInfer" value="true"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>So the inline VB.NET code on my aspx page was being compiled using the .NET Framework 2.0 when it really needed to be compiled with the .NET Framework 3.5 compiler in order to take advantage of the VB.NET IF() coalesce statement. Without the VB.NET web.config compiler option, the default is to compile using the .NET Framework 2.0 and the VB.NET IF() coalesce statement does not exist (at least in the form that I want it in). FYI, there is an older IF statement in VB.NET 2.0 compiler which is why it is giving me the unusual “Expression Expected” error message – see this article for when VB.NET got the new updated version.
EDIT (2011-06-20): I had made a wrong assumption in the first version of this blog post. After a little more research and investigation I was able to figure out that the issue was in the web.config and not with the IIS App Pool. Thanks to the comment from James which forced me to look into this again.
-
How to get an indexed item of an IEnumerable object (Linq)
If you have an IEnumerable<T> collection of some objects T and you need to get a particular item out of that collection, you cannot use the standard indexing that you would normally use with brackets ([index]). If you try you will get an error such as:
Cannot apply indexing with [] to an expression of type ‘System.Collections.Generic.IEnumerable<T>
But there is the extension method ElementAt(index) (in the System.Linq namespace) for IEnumerable<T> that will allow you to get at that particular indexed item:
MyItem = MyIEnumerableExpression.ElementAt(index);
From the MSDN Documentation: http://msdn.microsoft.com/en-us/library/bb299233.aspx
-
Line-breaks and carriage returns (\r \n) are invalid in email subject
I received this exception when sending an email using System.Net.Mail.MailMessage:
"The specified string is not in the form required for a subject."
System.ArgumentException: The specified string is not in the form required for a subject.
at System.Net.Mail.Message.set_Subject(String value)
at System.Net.Mail.MailMessage.set_Subject(String value)The subject line of the email I was sending had a carriage return and line break in it so it made sense that this would be an invalid subject line.
And I was able to confirm that a check is made on the subject line to make sure there are not Carriage Returns or Line Feeds when the MailMessage.Subject property is set, but also that these are the only characters checked for.
To remove the line breaks and carriage returns I just did a simple find and replace:
subject= subject.Replace("\n", " ").Replace("\r", " ");
Technorati Tags: System.Net.Mail.MailMessage.Subject -
How to change the Target Framework Version for a Visual Basic Project
Today I received the following warning message from Visual Studio while adding a reference in a Visual Basic project:
The Target Framework version for the project ‘XXXX’ is higher than the current Target Framework version.
Would you like to add this reference to your project anyway?This warning message makes complete sense since the Visual Basic project I was working on is targeting the .NET Framework 2.0 and the project that was being referenced targeted the .NET Framework 3.5. The only issue was that I was not able to figure out how to change the Target Framework Version in a Visual Basic project.
In a C# project it is pretty easy; right-click on the project and choose properties and in the Application tab (vertical tab) you will see the Target Framework dropdown box:
But in Visual Basic it is hidden. Here is where I found how to change the Target Framework version after searching for a bit:
Right-click on the project and choose properties and then select the Compile tab (vertical tab).
At the bottom of the Project > Compile tab you will find an Advanced Compile Options… button.
In this dialog called the Advanced Compiler Settings is where you will find how to change the Target Framework version for Visual Basic.
-
GDI+ Error when converting Tiff to Jpeg: Parameter is not valid
One of the web applications that I work on is Instant Church Directory, a website that helps churches create a church photo directory. Since the main focus of the application is to create a photo directory, customers are uploading all sorts of images into Instant Church Directory and the application needs to correctly convert and store the images and insert them in the final output, which is a PDF.
Since the web application was launched in September 2008 we have had an issue with some Tiff images that are uploaded and throw a System.ArgumentException when the image is converted to a Jpeg.
System.ArgumentException: Parameter is not valid. at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(Stream stream, ImageFormat format)
Today I was finally able to track it down to the Tiff image being created (or modified) by Adobe Photoshop and Adobe Photoshop is adding extra Property Items (meta data tags) into the image.
With the help of Bob Powell’s tool: Discovering the Property Items present in an image, I was able to see that the Adobe Photoshop Tiff images included several additional property items that were not present in other Tiff images that worked. Removing the tags from the original image prior to converting it to a Jpeg resolved the issue.
Here is a list of all of the Microsoft Windows GDI+ image property tags in numerical order.
The PropertyTagICCProfile is id 0x8773 and the other tag that I removed to resolve this issue is id 0x935C (this Property Item is not defined in the list above but it has a header in it with “Adobe Photoshop Document Data Block”). Property Items 0x02BC and 0x8649 are also not defined in the GDI+ list above and also are added by Adobe Photoshop but they did not cause any issues so I just left them (although if removing the first two do not work for you then try removing the other two also).
Here is the code that I used to remove the offending Property Items from the Tiff image before converting it to a Jpeg in order to prevent the GDI+ Parameter is not valid exception:
// Check if this is a Tiff file
if (bmp.RawFormat.Guid.Equals(ImageFormat.Tiff.Guid))
{
//loop through all of the Properties
//looking for the offending property items added by Photoshop
foreach (PropertyItem pItem in bmp.PropertyItems)
{
switch (pItem.Id)
{
//ICC Profile tag (PropertyTagICCProfile)
//Remove this tag for Tiff to be converted to Jpeg.
case 0x8773:
bmp.RemovePropertyItem(0x8773);
break;//Unknown tag added by Adobe Photoshop
//"Adobe Photoshop Document Data Block".
//Removing this tag for Tiff to be converted to Jpeg
case 0x935C:
bmp.RemovePropertyItem(0x935C);
break;default:
break;
}
}
}//convert the image to jpeg
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
bmp = new Bitmap(ms); -
nvarchar(max) parameters need the size set to -1
SQL Server 2005 supports a new data type nvarchar(max). This is one of the new max datatypes that are to replace ntext, text, and image in a future version of SQL Server (according to SQL Server Books Online) but you should start using them now.
I recently used the nvarchar(max) data type for the first time in a stored procedure and I had some difficulty setting the parameter size in my C# code. I tried leaving off the size or setting it to the size of the string that was being passed to the stored procedure but none of these worked. Eventually I figured out that you have to set the size to -1 to get it to work.
Here is an example of how to create the SqlClient.SqlParameter:
System.Data.SqlClient.SqlParameter param;
param = new System.Data.SqlClient.SqlParameter();
param.ParameterName = "@Message";
param.SqlDbType = System.Data.SqlDbType.NVarChar;
param.Size = -1;
cmd.Parameters.Add(param);