Forcing Retrieval of Images in ASP.NET/MVC

Browsers cache images in order to speed up page load times. If an action on a controller generates dynamic images, this can be a problem, since the user will only see the first image fetched by the browser. One way to force the browser to by-pass its cache and actually retrieve the image is to append a random parameter to the URL for the action.
For example: A user uploads an image to the server and the server subsequently returns some HTML showing the image. The user then performs an action which triggers the server to alter the image in some way (for example cropping or resizing), and returns the HTML once again. However, the browser sees that the link to the image is the same, and therefore it simply shows the initial image which it has already cached.

So, instead of generating the image URL with razor syntax like this:

<img src="@Url.Action("GetPhoto")" />

Simply add some random parameter (or just anything the browser has not seen before) to force it to download the image again:

<img src="@Url.Action("GetPhoto", new {ticks = DateTime.UtcNow.Ticks})" />

Extracting Image Files from SQL Server

There are a number of different plugins and tools which can export image data in hex format from an MS SQL server to an actual file on the harddrive. If you have Visual Studio installed, however, the easiest way is to just write a small console program to select the data from the image column and save it to a file. For example:

static void Main(string[] args)
{
try
{
SqlConnection thisConnection = new SqlConnection(@"Server=192.168.1.1,1433;database=MyDb;User id=sa;Password=JdjdUD78!;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT ImageData FROM [MyDatabase].[dbo].[Images] where ID = 1";
byte[] imgBytes = (byte[]) thisCommand.ExecuteScalar();
FileStream fs = new FileStream(@"C:\temp\image.jpg", FileMode.CreateNew, FileAccess.Write);
fs.Write(imgBytes, 0, imgBytes.Length);
fs.Flush();
fs.Close();
thisConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}

Basic SQL Server Query in C#

If you need to access a database on an MSSQL server programmatically (in C#) just to do some quick and dirty data extraction and perhaps manipulation, the following code snippet will get you started. Replace the connection string, table and columns names with whatever is appropriate for your task.

static void Main(string[] args)
{
try
{
SqlConnection thisConnection = new SqlConnection(@"Server=192.168.1.1,1433;database=NorthWind;User id=sa;Password=HIUHFuhf8;");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT [ID], [Name] FROM Customers";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}", thisReader["ID"], thisReader["Name"]);
}
thisReader.Close();
thisConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}

Subversion revision number in AssemblyInfo.cs

Pre-requisites: Tortoise SVN, Visual Studio
  • Exclude the existing “AssemblyInfo.cs” file from Subversion
  • Rename “AssemblyInfo.cs” to “AssemblyInfo.template
  • Add the Subversion revision keyword to the template file. For example: [assembly : AssemblyVersion(“1.0.0.$WCREV$“)]
  • Add SubWCRev.exe from the TortoiseSVN bin directory to the project (or add the Tortoise SVN bin folder to your path)
  • In the pre-build event for your project, add the following line (You may need to adjust it to your own project structure): "$(SolutionDir)\lib\SubWCRev\subwcrev.exe" "$(SolutionDir)." "$(ProjectDir)Properties\AssemblyInfo.template" "$(ProjectDir)Properties\AssemblyInfo.cs"
  • Done! The “AssemblyVersion.cs” file should now be auto-generated on every build and the $WCREV$ keyword is replaced with the current SVN revision number on the solution directory.


Getting Debug Output From Scripts on ASPX Pages

To get some useful output when debugging C# or VB scripts on aspx
pages use the following statement in the script:

System.Diagnostics.Debug.WriteLine("We are here!");

In Visual Studio, attach the debugger to the process serving the
page containing the script (typically an IIS server process).
The debug text, “We are here!”, now appears in the output window
whenever the aforementioned statement in the script is executed!