Henris blogg

 

delegate find

ModulSamling m = modulsamling.Find(delegate(ModulSamling ms) { return ms.ID== item.Tag; });

Instead of:

ModulSamling m=FindModul(modulsamling, item.Tag);

privat ModulSamling FindModul(List<ModulSamling list, string tag)

{

      foreach(ModulSamling m in list)

      {

            if(m.ID==tag)

                  return m;

      }

      return null;

}

Comments [0]

Convert a list from one type to another using Predicate and ConvertAll

List<Variabel> lv = new List<Variabel>();

lv.AddRange(liste);

List<string> lst = lv.ConvertAll<string>(delegate(Variabel g) { return g.Navn; });

Converts a list of objects with type Variabel,

to a list of strings.

Variabel contains a string property called Navn, and this is what gets transelted to the list of strings,

{ return g.Navn)}

Comments [0]

Visual Studio: Design view missing for Form

Recently I had a problem opening my form in Design View within Visual Studio. (C# / 2005).

Seems the problem was a corrupt project file .csproj

My form was defined in the project file as follows:

    <Compile Include="frmOppgaver.cs">

    </Compile>

    <Compile Include="frmOppgaver.Designer.cs">

      <DependentUpon>frmOppgaver.cs</DependentUpon>

    </Compile>

…..

    <EmbeddedResource Include="frmOppgaver.resx">

      <SubType>Designer</SubType>

      <DependentUpon>frmOppgaver.cs</DependentUpon>

    </EmbeddedResource>

Somehow VS has removed a line telling it that this is a form.

To correct this I had to check out the .csproj file and open it in a text-editor.

The correct values with my addition in bold:

<Compile Include="frmOppgaver.cs">

      <SubType>Form</SubType>

    </Compile>

    <Compile Include="frmOppgaver.Designer.cs">

      <DependentUpon>frmOppgaver.cs</DependentUpon>

    </Compile>

    <EmbeddedResource Include="frmOppgaver.resx">

      <SubType>Designer</SubType>

      <DependentUpon>frmOppgaver.cs</DependentUpon>

    </EmbeddedResource>

This has happened a couple of times. I suspect it is when I move my project to a different place,

or use a copy of an existing project for start of another which sums up to the same :)

Brgds Henri

Comments [0]

Sort a List of custom objects

fakta.AddRange(retur.Fakta);

FaktaCompare c = new FaktaCompare();

fakta.Sort(c);

    public class FaktaCompare : IComparer<FaktaEntitet>

    {

        #region IComparer<FaktaEntitet> Members

        public int Compare(FaktaEntitet x, FaktaEntitet y)

        {

            return String.Compare(x.Key, y.Key);

        }

       

Comments [0]

wse3policyCache.config and Visual Studio test-project

Trying to test a project that used Webservices with WSE3

 I got an exception: " Failed parsing the policy document."

while trying to set the policy.

 

Ie:

MyWebServiceWse ws = new MyWebServiceWse ();

ws.UseDefaultCredentials=true;

ws.SetPolicy("WebServicePolicy");

 

 

Looking closer, Innerexception told me that:

Could not find file 'C:\\Something\\MyProject\\TestResults\\username_MYPC 2009-03-25 16_40_25\\Out\\wse3policyCache.config' and more..

 

Which seems to mean that the Testproject is not finding the wse3policyCache.config where it's currently running.

 

To solve it I had to edit my localtestrun.testrunconfig, so that it copied this file to the current test-directory when it started testing.

 

I.e: Menu - Edit test run config - Local test run - Deployment - Add file and select the aforementioned wse3policyCache.config  file.

 

Btw, if you are like me and only select the test from Test results and try to run or debug it,

it still doesn't work…. I had to select my testproject in Solution Explorer ,

go to menu Test and

Start selected test project (with or without Debugger)

 

Finally my testproject was executing with my policies…

Now only to get my bugs fixed… :)

 

HTH somebody

 

Brgds

Henri

 

Comments [0]

Winform-CAB/Smart Client Software Factory: _presenter.OnViewReady NullReferenceException

When developing a module recently using Smart Client Software Factory

I got an exception I could not understand.

 

SCSF is using MVP- design and has some special attributes to create different objects, insert dependencies etc.

 

The problem was with a view and presenter using a well-used design , straight out from the wizard.

 

The following code is created by the Add view (with presenter) package/wizard (this is from the View):

 

 

 

       /// <summary>

        /// Sets the presenter. The dependency injection system will automatically

        /// create a new presenter for you.

        /// </summary>

        [CreateNew]

        public VisXMLPresenter Presenter

        {

            set

            {

                _presenter = value;

                _presenter.View = this;

            }

        }

 

        protected override void OnLoad(EventArgs e)

        {

            _presenter.OnViewReady();

        }

 

 

As the comments from the wizard so beautifully states: The dependency injection system will automatically

        /// create a new presenter for you

 

 

However, when running the module the OnLoad function was called before the _presenter was set,

leaving the _presenter at null, and generating the exception.

 

Which is very strange considering the framework/factory is handling the internal workings of generating the _presenter due to the [CreateNew] attribute. Add to that that this has been tested and used in many other modules by me and a lot others.

 

 

What was the problem? As usual, Google is my friend.

It seems that the Webbrowsercontrol I had dragged onto my form was the culprit.

This made the call to the OnLoad-function before the framework was finished doing it's magic.

 

The solution: add a Panel to the View/form first and add the Webbrowser to the panel.

This solves the issue until the Webbrowsercontrol itself starts behaving… :)

 

 

And here's the link I found using Googie: http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=7559

 

 

 

Happy coding ;)

Comments [0]

Sql/sqlserver/sp/stored procedure: Ignore optional parameters in update + CRUD functionality in SP

You have a Stored Procedure which can do updates.

One or more of the parameters are optional.

How do you express this?

 

 

The first solution that springs to mind is IFs.

If param1 is null

      Update without param1

else

      Update with param1

 

 

Another more elegant solution could be this:

      Update SomeDB

      SET

      SomeColumn=@SomeParam,

      Antall = ISNULL(@Param1,Antall) --If @Param1 is null, use the exisiting column value, i.e no update

 

Of course if you want to set the column to NULL this solution can't be used.

 

 

Below full stored procedure with full CRUD functionality and optional parameters:

 

 

 

USE [SOMEDB]

GO

 

SET ANSI_NULLS ON

GO

 

SET QUOTED_IDENTIFIER ON

GO

 

 

CREATE PROCEDURE [dbo].[HendelserVarslet_siud]

      @FraDato DateTime = null,

      @Tildato DateTime =  null,

      @HendelseTypeID smallint=NULL,

      @HendelseInformasjonID int=NULL,

      @RaadgiverNr nvarchar(50)=NULL,   

      @Antall int=NULL,

      @Funksjon Char(1)='L'

AS

BEGIN

 

SET NOCOUNT ON;

 

      IF(@Funksjon='L' OR @Funksjon='S')

            -- List records

            BEGIN

                  -- set default dates

                  IF @FraDato IS NULL

                        SET @FraDato = dbo.StartOfday(GETDATE())

                  IF @Tildato IS NULL

                        SET @Tildato= dbo.EndOfday(@FraDato)    

           

                  -- Get records

                  SELECT

                        V.HendelseInformasjonID, I.HendelseTypeID,

                        T.HendelseType, V.RaadgiverNr, V.Antall,

                        V.LoggetDato,  I.Beskrivelse, I.RegistrertDato

                  FROM   HendelseVarslet AS V

                        INNER JOIN

                             HendelseInformasjon AS I

                             ON V.HendelseInformasjonID=I.HendelseInformasjonID

                        INNER JOIN

                             HendelseType T

                             ON T.HendelseTypeId = I.HendelseTypeId

             

                  WHERE

                        V.LoggetDato>=@FraDato

                        AND

                        V.LoggetDato<=@TilDato

                        AND        

                        (I.HendelseTypeID=@HendelseTypeID OR @HendelseTypeID IS NULL)

                        AND

                        (V.HendelseInformasjonID=@HendelseInformasjonID OR @HendelseInformasjonID IS NULL)

                        AND

                        (V.RaadgiverNr=@RaadgiverNr OR @RaadgiverNr IS NULL)

                       

                       

                  RETURN  @@ERROR        

           

            END

      ELSE

      IF(@Funksjon='I')

      -- Insert record

            BEGIN

                  IF    (@RaadgiverNr IS NOT NULL AND @HendelseInformasjonID IS NOT NULL AND @Antall IS NOT NULL)

                        AND

                        NOT EXISTS ( SELECT HendelseinformasjonId, RaadgiverNr FROM [PFA].[dbo].HendelseVarslet

                                   WHERE

                                         RaadgiverNr=@RaadgiverNr AND HendelseInformasjonID=@HendelseInformasjonID)

                       

                        AND

                        EXISTS ( SELECT HendelseinformasjonId, RaadgiverTil FROM [PFA].[dbo].Hendelse

                                   Where (RaadgiverTil=@RaadgiverNr OR Eier=@RaadgiverNr)

                                         AND HendelseInformasjonID=@HendelseInformasjonID )

                            

                        BEGIN

                             INSERT INTO [PFA].[dbo].[HendelseVarslet]

                             (

                                   HendelseInformasjonID,

                                   RaadgiverNr,

                                   Antall,

                                   LoggetDato

                             )

                             VALUES

                             (

                                   @HendelseInformasjonID,

                                   @RaadgiverNr,

                                   @Antall,

                                   getdate()

                             )    

                             RETURN  @@ERROR  

                        END

                  ELSE

                        BEGIN

                             RAISERROR(' Invalid parameters or these values already exists in db',11,1)

                             RETURN @@ERROR

                        END

            END

            ELSE

      IF(@Funksjon='U')

      -- update record

            BEGIN

                  IF    (@RaadgiverNr IS NOT NULL AND @HendelseInformasjonID IS NOT NULL)

                        AND

                        (EXISTS ( SELECT HendelseinformasjonId, RaadgiverNr FROM [PFA].[dbo].HendelseVarslet

                                   WHERE

                                         RaadgiverNr=@RaadgiverNr AND HendelseInformasjonID=@HendelseInformasjonID)

                        )

                        BEGIN

                             UPDATE [PFA].[dbo].[HendelseVarslet]

                             SET

                                   HendelseInformasjonID=@HendelseInformasjonID,

                                   RaadgiverNr=@RaadgiverNr,

                                   Antall = ISNULL(@Antall,Antall) --If @Antall IS NULL, don't update Antall column

                             RETURN  @@ERROR

                        END

                            

                  ELSE

                        BEGIN

                             RAISERROR(Invalid parameters or this combination of HendelseInformasjonId and rådgiver is non-existant ',11,1)

                             RETURN @@ERROR

                        END        

            END

           

           

      ELSE

      IF(@Funksjon='D')

      -- Delete record

            BEGIN

                  IF    EXISTS ( SELECT HendelseinformasjonId, RaadgiverNr FROM [PFA].[dbo].HendelseVarslet

                                   WHERE

                                         (RaadgiverNr=@RaadgiverNr AND HendelseInformasjonID=@HendelseInformasjonID)

                        )

                        AND @RaadgiverNr IS NOT NULL AND @HendelseInformasjonID IS NOT NULL

                       

                        BEGIN

                             DELETE FROM [PFA].[dbo].[HendelseVarslet]

                             WHERE

                                   HendelseInformasjonID=@HendelseInformasjonID

                                   AND

                                   RaadgiverNr=@RaadgiverNr

 

                             RETURN  @@ERROR

                        END

                            

                  ELSE

                        BEGIN

                             RAISERROR('Invalid parameters or this combination of HendelseInformasjonId and rådgiver is non-existant,11,1)

                             RETURN @@ERROR

                        END        

            END

      ELSE

      -- Unknown function

            BEGIN

                  RAISERROR('Function unknown,11,1)

                  RETURN @@ERROR

            END

 

     

     

END

 

 

GO

 

 

 

Comments [0]

SQL/Sqlserver/SP/Stored procedure: Funksjon som støtter Select, Insert, Update og Delete

Etter inspirasjon fra grensesnittet på en SP noen databasekolleger har laget

 kommer her en Stored Procedure som har støtte for CRUD-operasjoner.

Eller SIUD på databasisk(...nytt ord:)

 

Ps Jeg har ingen databasekompetanse, jeg bare lager det jeg trenger akkurat der og da…

De som kan det kan helt sikkert forbedre dette :)

Enjoy:

 

 

USE [DINDATABASE]

GO

 

SET ANSI_NULLS ON

GO

 

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[HentConfig]

      @Valg Varchar(100) = NULL,

      @Verdi Varchar(MAX) =  NULL,

      @Funksjon Char(1)='L'

AS

BEGIN

 

SET NOCOUNT ON;

 

      IF(@Funksjon='L')

            -- List records

            BEGIN

                  SELECT

                        Valg, Verdi

                  FROM

                        HendelseVarselKonfig

                  WHERE

                        (Valg=@Valg OR @Valg IS NULL)

                 

                  RETURN @@ERROR

           

            END

      ELSE

      IF(@Funksjon='I')

      -- Insert record

            BEGIN

                  IF NOT EXISTS (SELECT Verdi FROM HendelseVarselKonfig

                                         WHERE Valg=@Valg )                

                        BEGIN

                             INSERT INTO HendelseVarselKonfig

                                   (Valg, Verdi)

                              VALUES

                                   (@Valg, @Verdi)

                                                                

                             RETURN @@ERROR         

                        END

                  ELSE

                        BEGIN

                             RAISERROR('Denne verdien finnes allerede',11,1)

                             RETURN @@ERROR

                        END

            END

            ELSE

      IF(@Funksjon='U')

      -- update record

            BEGIN

                  IF EXISTS (SELECT Verdi FROM HendelseVarselKonfig

                                         WHERE Valg=@Valg )                

                        BEGIN

                             UPDATE HendelseVarselKonfig

                                   SET Verdi=@Verdi

                             WHERE

                                   Valg=@Valg

                                                                

                             RETURN @@ERROR         

                        END

                  ELSE

                        BEGIN

                             RAISERROR('Denne verdien finnes ikke',11,1)

                             RETURN @@ERROR

                        END

            END

           

           

      ELSE

      IF(@Funksjon='D')

      -- Slett record

            BEGIN

                  IF EXISTS (SELECT Verdi FROM HendelseVarselKonfig

                                         WHERE Valg=@Valg )                

                        BEGIN

                             DELETE FROM HendelseVarselKonfig                                

                             WHERE

                                   Valg=@Valg

                            

                             RETURN @@ERROR         

                        END

                  ELSE

                        BEGIN

                             RAISERROR('Denne verdien finnes ikke',11,1)

                             RETURN @@ERROR

                        END

            END

      ELSE

      -- UKJENT FUNKSJON

            BEGIN

                  RAISERROR('Denne funksjonen finnes ikke',11,1)

                  RETURN @@ERROR

            END

 

     

     

END

 

GO

 

 

 

Comments [0]

Sql/sqlserver/sp/stored procedure: Finne alle records laget en dag

Å finne start /slutt på en dag er et forholdsvis vanlig behov.

Her er to sqlscript for å lage to funksjoner som returner start og slutt på inndato.

(2 ms oppløsning på sluttdato…)

 

Lenger ned kan du se de i bruk:

 

CREATE  function dbo.StartOfDay

(

@Date DATETIME

)

RETURNS DATETIME

AS

BEGIN

RETURN DATEADD(d,DATEDIFF(d,0,@Date),0)

END

 

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

CREATE function dbo.EndOfDay

(

@Date DATETIME

)

RETURNS DATETIME

AS

BEGIN

RETURN DATEADD(ms,-2,DATEADD(d,1,dbo.StartOfDay(@Date)))

END

 

 

 

Denne prosedyren finner default alle poster laget i dag.

Bruker funksjonene over for å finne dagen i dag sin start og slutt:

 

USE [ENDATABASE]

GO

 

SET ANSI_NULLS ON

GO

 

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[HentHendelserVarslet]

      -- Add the parameters for the stored procedure here

      @FraDato DateTime = null,

      @Tildato DateTime =  null,

      @HendelseTypeID smallint = null

     

 

AS

BEGIN

IF @FraDato IS NULL

      SET @FraDato = dbo.StartOfday(GETDATE())

 

IF @Tildato IS NULL

      SET @Tildato= dbo.EndOfday(@FraDato)

 

      SET NOCOUNT ON;

 

    SELECT

            V.HendelseInformasjonID, I.HendelseTypeID,

            T.HendelseType, V.RaadgiverNr, V.Antall,

            V.LoggetDato,  I.Beskrivelse

      FROM   HendelseVarslet AS V

            INNER JOIN

                  HendelseInformasjon AS I

                  ON V.HendelseInformasjonID=I.HendelseInformasjonID

            INNER JOIN

                  HendelseType T

                  ON T.HendelseTypeId = I.HendelseTypeId

 

      WHERE

            V.LoggetDato>=@FraDato

            AND

            V.LoggetDato<=@TilDato

            AND        

            (I.HendelseTypeID=@HendelseTypeID OR @HendelseTypeID IS NULL)

                     

END

 

GO

 

 

Comments [0]

Visual Studio/c#: Åpne asmx.cs fil i code editor

På min Visual Studio 2008 åpner web-service codebehind-filer (asmx.cs)

seg i Design Mode når jeg dobbeltklikker de.

 

Det er garantert IKKE det jeg ønsker.

 

For å få de til å åpne seg i Code View:

 

 i Visual Studio høyreklikk filen og velg Open With

 

velg CSharp Editor og klikk [Set as Default] og velg OK.

 

Nå kan du dobbeltklikke i vei… :)

 

 

Comments [0]