Linq to Sql: Change connectionstring to load from Web.config

Update 20 may 2009: New easy sollution

Set the Connection -> Application Settings property True. This will generate a connection string in your app.config.

 

Copy this connection string to your web.config and your all set!

 

================================================================= 

Old post: 

If you want to use your connectionstring from the web.config with Linq to Sql (dbml) you have to add the following partial class to your project:

[code:c#]

namespace Your.Namespace
{
  partial class yourDataContext
  {
    partial void OnCreated()
    {
       ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["YourConnectionString"];
       if (s != null)
         Connection.ConnectionString = s.ConnectionString;
    }
  }
}

[/code]

Don't forget to change the Your.Namespace, yourDataContext and YourConnectionString to fit your project.

Hope this helps.

Bitmap Extension Methods; for Resize, Crop and save

For Bitmap manipulation I wrote three extension methods. With this method you can resize, crop and save (as jpg) your image.

 

Resize Method

[code:c#]

public static Bitmap resizeImage(this Bitmap input, Size size)

{

int sourceWidth = input.Width;

int sourceHeight = input.Height;

float nPercent = 0;

float nPercentW = 0;

float nPercentH = 0;

nPercentW = ((float)size.Width / (float)sourceWidth);nPercentH = ((float)size.Height / (float)sourceHeight);

if (nPercentH < nPercentW)

nPercent = nPercentH;

else

nPercent = nPercentW;

int destWidth = (int)(sourceWidth * nPercent);

int destHeight = (int)(sourceHeight * nPercent);

Bitmap b = new Bitmap(destWidth, destHeight);

Graphics g = Graphics.FromImage(b);g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(input, 0, 0, destWidth, destHeight);

g.Dispose();

return b;

}

[/code]

 

Crop Method

[code:c#]

public static Bitmap cropImage(this Bitmap input, Rectangle cropArea)

{

Bitmap bmpImage = new Bitmap(input);Bitmap bmpCrop = bmpImage.Clone(cropArea, input.PixelFormat);

return (System.Drawing.Bitmap)(bmpCrop);

}

[/Code]

 

Safe Method

[Code:c#]

public static void saveJpeg(this Bitmap img, string path, long quality)

{

// Encoder parameter for image quality

EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);

// Jpeg image codec

ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg"); if (jpegCodec == null)

return;

EncoderParameters encoderParams = new EncoderParameters(1);

encoderParams.Param[0] = qualityParam;

img.Save(path, jpegCodec, encoderParams);

}

public static ImageCodecInfo getEncoderInfo(string mimeType)

{

// Get image codecs for all image formats

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec

for (int i = 0; i < codecs.Length; i++)

if (codecs[i].MimeType == mimeType)

return codecs[i]; return null;

}

[/Code]

ListView: OnItemDataBound

Get dataItem onItemDataBound (don't forget to replace the bold parts with your objectname):

[code:c#]

protected void MainPostListView_ItemDataBound(object sender, ListViewItemEventArgs e)

{

YourObject item = (YourObject)((ListViewDataItem)e.Item).DataItem;

}

[/code]

 

Findcontrol within the itemtemplate:

[code:c#]

HyperLink hplTitle = (HyperLink)e.Item.FindControl("hyperLink");

[/code]

 

 

String.Format with C#

In this post I will add examples of String.Format or links to sites with good examples. 

 

Double

[code:c#]
</p>
<p>
//&amp;nbsp;just two decimal places<br />
String.Format("{0:0.00}", 123.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123.46"<br />
String.Format("{0:0.00}", 123.4);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123.40"<br />
String.Format("{0:0.00}", 123.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123.00"
</p>
<p>
// max. two decimal places<br />
String.Format("{0:0.##}", 123.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123.46"<br />
String.Format("{0:0.##}", 123.4);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123.4"<br />
String.Format("{0:0.##}", 123.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123"
</p>
<p>
// at least two digits before decimal point<br />
String.Format("{0:00.0}", 123.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "123.5"<br />
String.Format("{0:00.0}", 23.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "23.5"<br />
String.Format("{0:00.0}", 3.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "03.5"<br />
String.Format("{0:00.0}", -3.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "-03.5"
</p>
<p>
String.Format("{0:0,0.0}", 12345.67);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "12,345.7"<br />
String.Format("{0:0,0}", 12345.67);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "12,346"
</p>
<p>
String.Format("{0:0.0}", 0.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "0.0"<br />
String.Format("{0:0.#}", 0.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "0"<br />
String.Format("{0:#.0}", 0.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ".0"<br />
String.Format("{0:#.#}", 0.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ""
</p>
<p>
String.Format("{0,10:0.0}", 123.4567);&amp;nbsp;&amp;nbsp;&amp;nbsp; // "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 123.5"<br />
String.Format("{0,-10:0.0}", 123.4567);&amp;nbsp;&amp;nbsp; // "123.5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "<br />
String.Format("{0,10:0.0}", -123.4567);&amp;nbsp;&amp;nbsp; // "&amp;nbsp;&amp;nbsp;&amp;nbsp; -123.5"<br />
String.Format("{0,-10:0.0}", -123.4567);&amp;nbsp; // "-123.5&amp;nbsp;&amp;nbsp;&amp;nbsp; "
</p>
<p>
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);&amp;nbsp;&amp;nbsp; // "123.46"<br />
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);&amp;nbsp; // "minus 123.46"<br />
String.Format("{0:0.00;minus 0.00;zero}", 0.0);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // "zero"
</p>
<p>
[/code]

Source: http://www.csharp-examples.net/string-format-double/

 

Integer

[code:c#]

String.Format("{0:00000}", 15);          // "00015"
String.Format("{0:00000}", -15);         // "-00015"

String.Format("{0,5}", 15);              // "   15"
String.Format("{0,-5}", 15);             // "15   "
String.Format("{0,5:000}", 15);          // "  015"
String.Format("{0,-5:000}", 15);         // "015  "

String.Format("{0:#;minus #}", 15);      // "15"
String.Format("{0:#;minus #}", -15);     // "minus 15"
String.Format("{0:#;minus #;zero}", 0);  // "zero"

String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551);       // "89-5871-2551"

[/code]

Source:  http://www.csharp-examples.net/string-format-int/

 

DateTime

[code:c#]

// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" – english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" – german (de-DE)

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                      LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"          LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"  LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"                ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"             ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                        MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                     YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"             SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"            UniversalSortableDateTime

[/code]

Source: http://www.csharp-examples.net/string-format-datetime/

 

convert UnixTime to DateTime

In an RSS feed I only had an UnixTime. For presentation I needed to convert this string to datetime. I did this with this code

[code:c#]

DateTime itemDateTime = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(item.unixTime);

[/code]

 

No idea what it does but it works 😀

Regular Expressions and Matchhandlers

For a project I need te get the email adresses out of a string with all kinds of text in it. After a lot of trying I found the matchhandler method. It is quite easy, you just need to adjust the Replace command and add a MatchEvaluator. Then you can use the matchhandler to do anything with the right string. In this case I´m adding it to a generic list.

[code:c#]

Regex.Replace(source, @"([a-zA-Z_0-9.-]+@[a-zA-Z_0-9.-]+.w+)", new MatchEvaluator(MatchHandler));

[/code]

And add the string to the generic list.

[code:c#]

private string MatchHandler(Match m)

{

     col.Add(m.Value);

     return m.Value;

}

[/code]

Asp.Net AJAX controltoolkit javascript funtions

I've search them all up for a 1000 times. Now I'm going to write them down as reference. This post will grow in the next few months.

Remember do not forget to set the behaviorId of the control. 

References:

CollapsiblePanelExtender
Expand:

  • $find('BehaviorId')._doOpen();

Collapse:

  • $find('BehaviorId')._doClose();
  • $find('BehaviorId').collapsePanel();

ModalPopupExtender
Show popup:

  • $find('BehaviorId').show();

Hide popup:

  • $find('BehaviorId').hide();