CodeCopy

January 3, 2012

Error while copying a Database

Filed under: c# — mazzoo @ 08:07

If you receive a “Microsoft.SqlServer.Dts.Tasks.TransferObjectsTask.TransferObjectsTask.TransferDatabasesUsingSMOTransfer()” or “End of package execution.” while trying to copy a database on the MSSQL Server.

The reason can be that a file with the new Database name already exist on the filesystem. We encountered this when we renamed Database X to X_Old, and tried to copy database Y to X. This cannot be done, because database X_Old is still associated with the filename X.

Either delete the conflicting database, or rename the file on the file system.

December 15, 2011

WP7 Pinch and Pan Zoom an Image

Filed under: c# — mazzoo @ 14:56

Here’s how to implement Pinch to Zoom and Pan on an Image Control – using the WP7 toolkit. ( Thanks to Alvaro Peon for this great solution  )

Remember allways to add CacheMode=”BitmapCache” to you Image for performance Open-mouthed smile

XAML:


<Image HorizontalAlignment="Left" Name="photo" Stretch="Uniform" VerticalAlignment="Top" Source="Image.png" CacheMode="BitmapCache">
<Image.RenderTransform>
<CompositeTransform x:Name="ImageTransformation" ScaleX="1" ScaleY="1" />
</Image.RenderTransform>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener PinchStarted="GestureListener_PinchStarted" PinchDelta="OnPinchDelta" DragDelta="Image_DragDelta" />
</toolkit:GestureService.GestureListener>
</Image>

C# Codebehind:


private Point Center;
private double InitialScale;

private void GestureListener_PinchStarted(object sender, PinchStartedGestureEventArgs e)
{
// Store the initial rotation angle and scaling
InitialScale = ImageTransformation.ScaleX;
// Calculate the center for the zooming
Point firstTouch = e.GetPosition(Image_LargeImage, 0);
Point secondTouch = e.GetPosition(Image_LargeImage, 1);

Center = new Point(firstTouch.X + (secondTouch.X - firstTouch.X) / 2.0, firstTouch.Y + (secondTouch.Y - firstTouch.Y) / 2.0);
}

private void OnPinchDelta(object sender, PinchGestureEventArgs e)
{
// If its less that the original  size or more than 4x then don’t apply
if (InitialScale * e.DistanceRatio > 4 || (InitialScale != 1 && e.DistanceRatio == 1) || InitialScale * e.DistanceRatio < 1)
return;

// If its original size then center it back
if (e.DistanceRatio <= 1.08)
{
ImageTransformation.CenterY = 0;
ImageTransformation.CenterY = 0;
ImageTransformation.TranslateX = 0;
ImageTransformation.TranslateY = 0;
}

ImageTransformation.CenterX = Center.X;
ImageTransformation.CenterY = Center.Y;

// Update the rotation and scaling
if (this.Orientation == PageOrientation.Landscape)
{
// When in landscape we need to zoom faster, if not it looks choppy
ImageTransformation.ScaleX = InitialScale * (1 + (e.DistanceRatio - 1) * 2);
}
else
{
ImageTransformation.ScaleX = InitialScale * e.DistanceRatio;
}
ImageTransformation.ScaleY = ImageTransformation.ScaleX;
}

private void Image_DragDelta(object sender, DragDeltaGestureEventArgs e)
{
// if is not touch enabled or the scale is different than 1 then don’t allow moving
if (ImageTransformation.ScaleX <= 1.1)
return;

double centerX = ImageTransformation.CenterX;
double centerY = ImageTransformation.CenterY;
double translateX = ImageTransformation.TranslateX;
double translateY = ImageTransformation.TranslateY;
double scale = ImageTransformation.ScaleX;
double width = Image_LargeImage.ActualWidth;
double height = Image_LargeImage.ActualHeight;

// Verify limits to not allow the image to get out of area
if (centerX - scale * centerX + translateX + e.HorizontalChange < 0 && centerX + scale * (width - centerX) + translateX + e.HorizontalChange > width)
{
ImageTransformation.TranslateX += e.HorizontalChange;
}

if (centerY - scale * centerY + translateY + e.VerticalChange < 0 && centerY + scale * (height - centerY) + translateY + e.VerticalChange > height)
{
ImageTransformation.TranslateY += e.VerticalChange;
}

return;
}

June 22, 2011

Get ClientID in a Repeater

Filed under: c# — mazzoo @ 08:23

Use <%# Container.FindControl("Control_Name").ClientID %> to get each ClientId in a Repeater’s Itemlist

February 9, 2011

Convert VB to C#

Filed under: c# — mazzoo @ 09:37

Just use:

http://www.developerfusion.com/tools/convert/vb-to-csharp/

 

Awesome !

July 15, 2010

SQL paging

Filed under: SQL — sterndorff @ 08:38

How to fetch from a table row number 10 to 20:

SELECT [t1].*
FROM (
   SELECT ROW_NUMBER() OVER (
   ORDER BY [t0].<primarykey>) AS [ROW_NUMBER],  [t0].*
   FROM [dbo].[<tablename>] AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN <fromrow> AND <torow>
ORDER BY [t1].[ROW_NUMBER]

Silverlight Developer runtime not installed

Filed under: c# — mazzoo @ 07:11

 

Download the following.

http://go.microsoft.com/fwlink/?LinkID=188039

The "Silverlight managed debugging package" is part of the developer runtime, not the SDK or Tools.  Make sure you have the latest version of the developer runtime installed (available at http://go.microsoft.com/fwlink/?LinkID=188039)

March 16, 2010

Excecute on UIThread

Filed under: c# — mazzoo @ 21:56

Just use: Execute.OnUIThread(()=>{DoStuff();} );

March 15, 2010

To enable Snapshot Transactions in MS-SQL 2005+

Filed under: SQL — mazzoo @ 09:37

If you have problems with transactions locking your tables, you might want to use Snapshot transactions.

Snapshot transactions work like this:

When the SNAPSHOT isolation level is enabled, each time a row is updated, the SQL Server Database Engine stores a copy of the original row in tempdb, and adds a transaction sequence number to the row.

This ensures that the table is not locked while in tranaction(s).

If a snapshot transaction attempts to commit an update to a row that was changed after the transaction began, the transaction is rolled back, and an error is raised.

To enable it, use following script ( This will not hang like other examples )

declare @sql varchar(8000)

select @sql = ‘
ALTER DATABASE Libomcore SET SINGLE_USER WITH ROLLBACK
IMMEDIATE ;
ALTER DATABASE Libomcore SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE Libomcore SET MULTI_USER;
ALTER DATABASE Libomcore SET ALLOW_SNAPSHOT_ISOLATION ON;

Exec(@sql)

November 27, 2009

Get Highres Thumbnail

Filed under: c# — mazzoo @ 00:11


public Image GetThumbnail(FileInfo from, Size size)
      {
          try
          {
              Image image = Bitmap.FromFile(from.ToString());

              int width = size.Width;
              int height = size.Height;

              if (image.Width > image.Height)
                  height = width * image.Height / image.Width;
              else
                  width = height * image.Width / image.Height;

              if (width > size.Width)
              {
                  width = size.Width;
                  height = width * image.Height / image.Width;
              }
              if (height > size.Height)
              {
                  height = size.Height;
                  width = height * image.Width / image.Height;
              }

              Image returnImage = new Bitmap(size.Width, size.Height);

              Graphics graphics = Graphics.FromImage(returnImage);
              graphics.SmoothingMode = SmoothingMode.HighQuality;
              graphics.CompositingQuality = CompositingQuality.HighQuality;
              graphics.InterpolationMode = InterpolationMode.High;

              graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, returnImage.Width, returnImage.Height);

              graphics.DrawImage(image, new Rectangle((size.Width - width) / 2, (size.Height - height) / 2, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

              return returnImage;
          }
          catch (Exception exception)
          {
          }

          return null;
      }

 

To save it, just use image.save(…); with the result

November 5, 2009

How to remove objects from generic List by property value

Filed under: c# — sterndorff @ 23:59
Tags: , ,

Having two generic lists of type List<MyClass>, how do I remove from List1 all the instances of List2, matching a property?

This is the setup. I have a class:

public class MyClass
{
    public int MyValue { get; set; }
    public int MyOtherValue { get; set; }
}

And two lists:

List<MyClass> list1 = new List<MyClass>();
list1.Add(new MyClass() { MyValue = 1, MyOtherValue = 10 });
list1.Add(new MyClass() { MyValue = 2, MyOtherValue = 20 });
list1.Add(new MyClass() { MyValue = 3, MyOtherValue = 30 });
list1.Add(new MyClass() { MyValue = 4, MyOtherValue = 40 });

List<MyClass> list2 = new List<MyClass>();
list2.Add(new MyClass() { MyValue = 2, MyOtherValue = 50 });
list2.Add(new MyClass() { MyValue = 3, MyOtherValue = 60 });

I want to remove all list2 instances from list1 matching on MyValue property.

(more…)

Next Page »

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.