Monday, September 29, 2025

c# - How to solve problem with convension date into string with char "/" in format mask

This result can be obtained when trying to convert a date to a string using a format mask containing the character '/'.
DateTime dt = DateTime.Now;
MessageBox.Show( dt.ToString( "yyyy/MM/dd", CultureInfo.GetCultureInfo( "de-DE" ) ) );
Output:
2025.09.29
The solution is to change the Culture:
DateTime dt = DateTime.Now;
MessageBox.Show( dt.ToString( "yyyy/MM/dd", CultureInfo.GetCultureInfo( "en-US" ) ) );
Output:
2025/09/29

Wednesday, September 17, 2025

sql server - How to solve problem with CHARINDEX() - value subtraction

If you use this SQL code:
SUBSTRING( a.PackingSlipId, 1, CHARINDEX('-', a.PackingSlipId ) - 1 ) as document,
..you can get this error, even in a situation when CHARINDEX() returns valid values: Output:
Msg 537, Level 16, State 3, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
The solution is to use NULLIF() function:
SUBSTRING( a.PackingSlipId, 1, nullif( CHARINDEX('-', a.PackingSlipId ), 0 ) -1 ) as document,

Tuesday, September 2, 2025

c# - How to add items to combobox with text description from attribute

Define new enum with attributes:
/* types for printing type combobox */
public enum PrintingTypeEnum
{
  [Description( "On screen" ) ]
  TO_SCREEN,
  [Description( "On printer" ) ]
  TO_PRINT
}
Fill the combobox with items:
CPrintingType.Items.Clear();
foreach (  PrintingTypeEnum item in Enum.GetValues( typeof( PrintingTypeEnum ) ) )
{
    string description = GetEnumDescription( item );
    CPrintingType.Items.Add( description );

    /* -- default value */  
    if ( item == PrintingTypeEnum.TO_PRINT )
    {
    	CPrintingType.SelectedIndex = CPrintingType.Items.Count - 1;
    }
}

private string GetEnumDescription( Enum _value )
{
    FieldInfo field = _value.GetType().GetField( _value.ToString() );
    DescriptionAttribute attribute = field?.GetCustomAttribute<DescriptionAttribute>();
    return attribute == null ? _value.ToString() : attribute.Description;
}
Output:

Tuesday, May 13, 2025

c# - EF - How to call storedproc with OUTPUT param

You have this stored proc:
ALTER PROCEDURE [dbo].[TEST]
	-- Add the parameters for the stored procedure here
( @Param1 int, @Message varchar(50) output )
AS
BEGIN
  set @Message = 'xxx';

  return '-1';
END
For calling it use this code:
var param1 = new SqlParameter( "Param1", System.Data.SqlDbType.Int );
param1.Direction = System.Data.ParameterDirection.Input;
param1.Value = iYear;

var message = new SqlParameter( "Message", System.Data.SqlDbType.VarChar, 50 ); 
message.Direction = System.Data.ParameterDirection.InputOutput;
message.Value = "";

var ret = context.Database.ExecuteSqlRaw(
  "EXEC TEST @Param1 = @param1, @Message = @message OUTPUT", param1, message
);

MessageBox.Show( message.Value.ToString() );
MessageBox.Show( ret.ToString() );

c# - EF - How to refresh existing DB into EF model

If you have DB and if you want to refresh it`s model into MODEL directory, call this:
dotnet ef dbcontext scaffold "Server=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=C:\_projects\vs\FioTax\FioTax\db.mdf;
Integrated Security=True;Connect Timeout=30" Microsoft.EntityFrameworkCore.SqlServer 
--output-dir Models --context AppDbContext --force

Tuesday, February 11, 2025

sql server - How to run export script by sqlcmd

In command.sql is stored SQL select script, this calling performs it and save output to output.txt.

The param "-h -1" removes title row.
sqlcmd -S localhost\sqlexpress -d username -U sa -P password -h -1 -m 1 
-i "c:\dir\command.sql" -o "c:\dir\output\output.txt"

Monday, January 13, 2025