Archive for the ‘ASP.NET’ Category
Resolving MSB3247 – Found conflicts between different versions of the same dependent assembly
Change the “MSBuild project build output verbosity” to “Detailed” or above. To do this, follow these steps:
1.) Bring up the Tools/Options dialog (Tools->Options…).
2.) In the left-hand tree, select the Projects/Solutions node, and then select Build and Run.
*** Note: if this node doesn’t show up, make sure that the checkbox at the bottom of the dialog (“Show all settings”) is checked.
3) In the tools/options page that appears, set the MSBuild project build output verbosity level to “Detailed” (assuming you’re on VS2010; “Normal” will suffice in VS2008 or older).
4) Build the project and look in the output window.
ASP.NET IIS Error display as garbage
Recently got into the situation that my .NET application displayed garbage instead of the error. Not to pleasant to work like this.
�`I�%&/m�{J�J��t��`$ؐ@������iG#)�*��eVe]f@�흼��{���{���;�N’���?\fdl��J�ɞ!���?~|?”��Ey�’)=��y6����h���ly���,-��E;Oϫ:��’Y�o��N��Y�����]i�h���tRͮ�_|^-���lQ�>�ɼ�e��C��*/.��eU/�R>j����|q8�ʪ~4)����_�zpW�����b��V�G���ջ_��z������w�_�� �7�݃U����,
After some researches, discovered that my app was using Telerik compression, thus the displayed ascii instead of the actual error.
What i did, was to comment the line that added the compression in Web.config (on the dev machine only, since on the production will still be needed)
This key (or other key that adds a compression) may appear more times in web.config, i removed it from the system.webServer node, and finally got the error display correctly.
ASP.NET Export GridView data to Excel
The easiest way in asp.net to export the data from a grid view to excel, is to render the control as html and save the file.
Excel knows how to open a html file and display it correctly. The only drawback to this method is that only one sheet will be available.
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this.grdData.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
GridView RenderControl
When trying to render a gridview as html,
you may encounter the following errors:
1. Control of type ‘GridView’ must be placed inside a form tag with runat=server.
This error occurs because the GridView is already placed in a form (usually the one from the master page)
To fix this, either override the function VerifyRenderingInServerForm like this:
{
// Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time
return;
}
or remove the grid control before rendering it, and add them back after render
int GridIndex = 0;
if (parent != null)
{
GridIndex = parent.Controls.IndexOf(grid);
parent.Controls.Remove(grid);
}
grid.RenderControl(hw);
if (parent != null)
{
parent.Controls.AddAt(GridIndex, grid);
}
Another alternative to avoid the override is to do this:
grid.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in grid.Rows)
{
row.RenderControl(hw);
}
grid.FooterRow.RenderControl(hw);
grid.RenderEndTag(hw);
2. asp.net RegisterForEventValidation can only be called during Render()
in this case, add in the aspx page, in the page tag, the directive: EnableEventValidation =”false”
Like this:
In this case, if you run on a framework >= 3.5, you also need in web.config, the entry: httpRuntime requestValidationMode=”2.0″, in the system.web tag, like this:
............
<system.web>
............
<httpRuntime requestValidationMode="2.0"/>
</system.web>
</configuration>
Alternative to Image.FromFile which doesn’t lock the file
If you get the error that file is in use when using Image.FromFile function,
you can use this simple alternative:
{
FileStream stream = new FileStream(filePath, FileMode.Open);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
stream.Close();
return image;
}
C# Save Resized Image with lower quality (decrease size of file)
public static void SaveBitmapAs(Bitmap bmp1, ImageFormat imageFormat,
string savePath, long qualityLevelCompression)
{
// Get a bitmap.
ImageCodecInfo imageEncoder = GetEncoder(imageFormat);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, qualityLevelCompression);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(savePath, imageEncoder, myEncoderParameters);
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
return codec;
}
return null;
}