C#: WinForms – Manage Cache state with Webclient CachePolicy

17:49

Marek Śliwiński 1 comment Print This Post  (297)

When using Webclient to retrieve some file from the web you can face problem with caching your files.

For example: You have a xml file with version number inside. After downloading first time by Webclient class, you changed content of the xml file and start process again. Unfortunately you will receive old content downloaded earlier instead newest content of the file.

That happens because of Microsoft Internet Explorer (as far as I know) Cache settings used by Webclient class.

So to disallow for caching your requests you have to change Weblicent CachePolicy property. You can do it as shown below:

string xmlUrl = "http://myserver.com/xmlfile.xml";

WebClient client = new WebClient();

// prevent file caching by windows
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(
System.Net.Cache.RequestCacheLevel.NoCacheNoStore
);

// read content of file
Stream rssStream = client.OpenRead(xmlUrl);
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: WinForms Tags: ,

Freeware program to send emails and attachments automatically

18:43

Marek Śliwiński No comments Print This Post  (55)

Sometimes we need some application which allow us to send some messages, files to recipients automatically based on our settings. It is especially helpful if we set up low-cost scenario. For example: SQL Server Express (it means no SQL Agent and DTS export/import functionality, Database Mail module etc.)

From few freeware programs which I tested I would like to recommend you BEEE (Better Email Enable Everything). It is Free for commercial and personal use.

beee_small

It is not longer supported by producent but it is still available for download from their webpage. Old doesn’t mean weak or poor functionality. This program is really brillant and works like a charm!

BEEE feature list:

- Easily email multiple files and/or entire folders
- Run multiple instances of BEEE at the same time
- Run BEEE in the background (from your system tray)
- Command line options allow for easy integration with most application software that can run executables (e. g. task schedulers)
- Build-in ZIP compression, encryption and password protection of the attachments !
- Email indirectly via MAPI enabled email clients (e.g. Outlook, Outlook Express or Eudora)
- Specify individual email addresses for each file within the file name!
- Send to email lists, e.g. to automate dispatch of newsletters
- Free for private and commercial use !

Runs on Win 95, 98, ME, NT and Win 2000/XP/2003 ! (also Windows Server 2000 and 2003) I didn’t test it on Windows Server 2008 but should also work fine.

BEEE can also monitor particular folder for files (you can define mask for filenames and extension) and after file detection in directory will send files automatically to defined destination.

Application download, more info and free commercial licence key

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: Mail Tags:

C#: How to add programatically meta tag to ASP.NET page

16:41

Marek Śliwiński No comments Print This Post  (74)
// Example: add meta tag with refresh command
    protected void Page_Load(object sender, EventArgs e)
    {
        string strCurrentURL = "http://localhost/test";

        // Add meta tag with refresh command
        string strRefreshSeconds = "3"; // how many seconds

        HtmlMeta metaTags = new HtmlMeta();
        metaTags.HttpEquiv = "refresh";
        metaTags.ID = "EnterHereSomeUniqueName";
        metaTags.Content = strRefreshSeconds + ";URL=" + strCurrentURL;
        Header.Controls.Add(metaTags);
    }
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: ASP.NET Tags: ,

Transact-SQL: How to get current date with start time 00:00:00 and end time 23:59:59

18:31

Marek Śliwiński No comments Print This Post  (129)
--SQL Server stores milliseconds with a precision of 1/300ths of a second.
-- So in other words, milliseconds are stored as:
--.000
--.003
--.007
--.010
--.013
--.017
-- etc.

SET DATEFIRST 1
SET DATEFORMAT ymd

SELECT
    DATEADD(ms, 0, CONVERT(varchar(10), GETDATE(), 120)) as 'StartToday'
    ,DATEADD(ms, -3, CONVERT(varchar(10), GETDATE()+1, 120)) as 'EndToday'

Result:

StartToday              EndToday
----------------------- -----------------------
2008-09-22 00:00:00.000 2008-09-22 23:59:59.997

(1 row(s) affected)
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

C#: Remove item from DropDownList by Value

8:25

Marek Śliwiński No comments Print This Post  (238)
// 1st method, more elegant
ListItem li = dropdownlist.Items.FindByValue(strValue);
if(li != null)
    dropdownlist.Items.Remove(li);

// 2nd method, all in one line but less readable
dropdownlist.Items.RemoveAt(
dropdownlist.Items.IndexOf(dropdownlist.Items.FindByValue(strValue))
);
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: ASP.NET Tags: ,

Transact-SQL: Delete (drop) all user stored procedures from database

19:32

Marek Śliwiński No comments Print This Post  (394)

Microsoft SQL Server 2005/2008

Run this code in your database context in SQL Management Studio:

SELECT
    'DROP PROCEDURE [' + name + ']'
as 'CopyThisColumnAndExecuteToDeleteAllProcedures'
FROM
    sys.procedures
WHERE
    [type] = 'P'
    AND is_ms_shipped = 0
    AND [name] NOT LIKE 'sp[_]%diagram%'
ORDER BY
    [name] ASC

drop_sp1

Copy generated statements and just run them to DELETE these stored procedures.
drop_sp2

foto_56388

Simply the best!

Same method you can use for tables or other objects.

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Regular Expressions: Only numbers, letters, spaces and restricted string length

21:27

Marek Śliwiński No comments Print This Post  (195)

Only numbers, capital, small letters (English) and spaces are allowed. String length must be between 1 to 10 characters.

Regex:

^[A-Za-z0-9 ]{1,10}$

If you would like to add some chars for example: ,.- then just add them like that:

Regex:

^[A-Za-z0-9 ,.-]{1,10}$
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Transact-SQL: Pack of useful Dates functions, calculations (day, week, month, year)

16:19

Marek Śliwiński No comments Print This Post  (479)

Take a look on pack of very nice date functions, conversions, calculations.

Thanks to Pinal Dave(http://www.SQLAuthority.com) and Vivek Jamwal

—-Yesterday
SELECT DATEADD(d,-1,GETDATE()) 'Yesterday'
—-First Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current Week'
—-Last Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) 'Last Day of Current Week'
--First Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) 'First Day of Last Week'

All (much more!) statements you can see in original post on SQLAuthority: here

Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Regular Expressions: Time hh:mm validation 24 hours format

20:11

Marek Śliwiński No comments Print This Post  (1,509)

Check if time is in 24h format hh:mm For example these values are valid: 01:00 (leading zero is mandatory), 05:01, 13:00, 23:59. For example these values are not valid: 24:00, 1:30, 05:60, 12|34 etc.

Regex:

([0-1]\d|2[0-3]):([0-5]\d)
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print

Regular Expressions: Internet email address format validation

18:14

Marek Śliwiński No comments Print This Post  (76)

Valid format: whatever@whatever.whatever

Regex:

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print