Sort generic list

by Marre 20. January 2011 13:39

To sort a generic list, the sort function must have something to compare. This is easily done like this:

List<ListItem> myList = new List<ListItem>();
myList.Sort((x, y) => string.Compare(x.Name, y.Name));

(reference: user Randy in thread: http://stackoverflow.com/questions/222572/sorting-a-dropdownlist-c-asp-net)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET | C#

Set new password without the old one

by Marre 19. May 2010 11:57

You can set a new password via the default membership provider without having the old one. It looks like this:

 

MembershipUser user = Membership.GetUser("username");
user.ChangePassword(user.resetPassword(), newPassword);

 

The clue is to retrieve the "old" password by using the resetPassword() functionality. To be able to do it like this, enablePasswordReset should be set to "true" in the .config file

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | ASP.NET

Log errors with SQL

by Marre 10. May 2010 10:25
How do I get the error information if f ex an insert fails? Well, I did it this way:
 
Declare @error_number int;
Declare @error_desc nvarchar(max);
 
set @error_number = @@error;
if @error_number <> 0
begin
set @error_desc = (select substring(description,1,60) from master.dbo.sysmessages where error = @error_number);
insert into my_log values ('This is where I log errors with error number, error description, a date, procedure name and where in the procedure');
end
 
 
Not that hard :) 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SQL

If exists with linq

by Marre 4. May 2010 14:28

Recently I wanted to insert a row in a database from a flat file. But I needed to check if db already contained that data. So how to do that with Linq? With SQL it would have been simple. Just use the exists statement. But it´s not that straight forward with linq. Or...is it?

Check this out:

if(!db.tblPerson.Any(p=>p.DateOfBirth == DateOfBirth))
//Do the insert dance :) 

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Linq

SQL - data not in both tables

by Marre 24. February 2010 15:34

I have two tables with a lot of data. I have to compare them and find out if there are data in table 1 that does not exist in table 2.

Well. I could use this script:

SELECT
name
FROM
table1
WHERE
name NOT IN (SELECT name FROM table2) 

 But since it´s a lot of data in those tables, it will take a while. This script will do it faster: 

SELECT
tbl2.name
FROM
table1 tbl1
RIGTH OUTER JOIN table2 tbl2 ON tbl1.name = tbl2.name
WHERE
tbl1.name IS NULL

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SQL

Supreme software - Avast

by Marre 27. January 2010 10:39
Avast, the free anti virus program, is a state of the art product. It keeps my computer clean. It´s awesome!

www.avast.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Supreme software

Is the current user authenicated?

by Marre 17. December 2009 13:17

There is a very simple way to determin if the current user is logged in/authenticated. It looks like this:

HttpContext.Current.User.Identity.IsAuthenticated

You will find
HttpContext directly under System.Web.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Get countries with the Globalization namespace

by Marre 7. December 2009 10:35

I wanted to fill up a drop down list with countries and country codes and found out that I could use CultureInfo and RegionInfo to do this. Here is the code:

CultureInfo[]cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
SortedDictionary<string,string> regionLookup = new SortedDictionary<string, string>(StringComparer.Ordinal);

foreach (CultureInfocultInfo in cultures)
{
  RegionInfo regionInfo = newRegionInfo(cultInfo.LCID);
  if(!regionLookup.ContainsKey(regionInfo.EnglishName))
  {
    regionLookup.Add(regionInfo.EnglishName,regionInfo.TwoLetterISORegionName);
  }
}

ddlCountries.DataTextField = "Key";
ddlCountries.DataValueField = "Value";
ddlCountries.DataSource= regionLookup;
ddlCountries.DataBind(); 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

HTML Color Codes

by Marre 7. December 2009 09:27

If you need to get color codes to use on your website, you must check this out: http://html-color-codes.info/

Wunderbar! :) 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Supreme website

A different if else in .net

by Marre 30. November 2009 09:41

As a programmer I always try to find ways to cleaner code. This is an example:

The code can be a mess when using if else if else if else if (well, I think you know what I mean :) )

Instead of writing my code like this:

if (countryCode == "SE")
    country= "Sweden";
else if (countryCode== "NO")
    country= "Norway";
else if (countryCode== "DK")
    country= "Denmark";
else
    country = "";

 

I can write it like this:

 

country =(countryCode == "SE") ? "Sweden" : (countryCode == "NO") ? "Norway" : (countryCode == "DK") ? "Denmark": "";

 

 

Nice? Well, I think so :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Powered by BlogEngine.NET 1.4.5.0
Theme by Supremelink Development

Martin Andersson

I´m currently working as senior consultant for Capgemini Norway AS.

Curriculum vitae

I use this blog when I find something useful that I want to be able to get hold of wherever I need. But who knows...Maybe someone else will find it useful as well.


About

Supremelink is the name of where I collect my spare time projects. The idea is to learn more about new techniques and areas that I´m not usually is working with or that can be nice to know before future projects at work.

To see the benefits with new technologies and to achieve as much knowledge about those areas, I usually have a goal/project to work with.

This site run´s on BlogEngine.NET. It´s a full featured blog that is using XML as data source. No database is required.

Supreme Software

When I find software that I like and of whitch I can see the benefits to use, I write about it under the category "Supreme Software".

Beware of the soon coming updates in this category!

Tag cloud