Oct 29, 2007

Using Microsoft Report Viewer

Create a Microsoft report:

1. add in a list.
2. add in table
3. drag data from data source to table

Dim voucher As New Voucher
DataGridView1.DataSource = voucher.FillVoucherHeader
DataGridView1.AutoResizeColumns()

DataGridView2.DataSource = voucher.FillVoucherItems
DataGridView2.AutoResizeColumns()

DataGridView3.DataSource = voucher.FillVoucherTotal
DataGridView3.AutoResizeColumns()

Me.VoucherTotalTableAdapter.Fill(Me.cpvdbDataSet.VoucherTotal)
Me.VoucherItemTableAdapter.Fill(Me.dsCheque.VoucherItem)
Me.VoucherHeaderTableAdapter.Fill(Me.dsCheque.VoucherHeader)

Me.ReportViewer1.RefreshReport()

Using VBC Command Line Compiler

Do you ever compile with command line ?

Just recall back Java Learning during university lab.

Let's try:
  1. From the Start menu, click on the Accessories folder, and then open the Windows Command Prompt.

  2. At the command line, type vbc.exe /imports:Microsoft.VisualBasic,System sourceFileName and then press ENTER.

    For example, if you stored your source code in a directory called SourceFiles, you would open the Command Prompt and type cd SourceFiles to change to that directory. If the directory contained a source file named Source.vb, you could compile it by typing vbc.exe /imports:Microsoft.VisualBasic,System Source.vb.

  3. To compile with reference file:
    vbc /reference:metad1.dll,metad2.dll /out:out.exe input.

Oct 28, 2007

Enable Windows Vista Administrator Account

Introduction

If for whatever reason you must login Windows Vista with the Administrator account this guide will show you how it’s done.

Warning: Running as Administrator in Windows Vista bypasses all security (UAC) and is NOT recommended. If you decide to use the Administrator account don’t complain when you start having problems.

Enable the Administrator Account

  1. Open the command prompt with Administrative privileges by opening the Start Menu, type cmd in the search box, and then press Ctrl+Shift+Enter to open cmd with admin rights.
  2. Type the following in the command prompt and press Enter after:

    net user administrator /active:yes

  3. Restart your computer and logon as Administrator.

Note: You might want to set a password for the administrator’s account for at least a little protection.

Disable the Administrative Account

To disable the Administrative account run the Net User command demonstrated above while logged on an account with administrative privileges but not as the Administrator account and replace yes with no.

or

Disable the User Account
To disable the user account run the Net User command demonstrated above while logged on an account with user privileges but not as the Administrator account and replace yes with no.

-------------------------------------------------------------------------------------------

Here is another way!

Remember that cute "Administrator" account you see when you login to safe mode in XP? That's the built-in administrator account that's installed by default, and disabled by default too, after a little digging-in I made this tutorial that'll let you enable and use this account in normal mode, and with a little other tweak, enjoying an XP-like administrator experience, while UAC is left ON (or off, it doesn't matter), but with no prompts or right clicks.


For Windows Vista Ultimate/Business/Enterprise:
1- Click Start, and type "secpol.msc" in the search area and click Enter.
(You may receive a prompt from UAC, approve/login and proceed)

2- In the left list, choose "Local Policies", then "Security Options"

3- Set "Accounts: Administrator account status" to Enabled.

4- Set "User Account Control: Admin Approval Mode for the Built-in Administrator account" to Disabled.


For Windows Vista Home Basic/Home Premium:
1- Click Start, and type "cmd" in the search area, right click on "Command Prompt" and select 'Run as Administrator".

2- In the command prompt type "net users Administrator /active:yes" (Note the capital "A" in Administrator) and press Enter, you will get a confirmation as "The command completed successfully".

3- Click Start, and type "regedit" in the search area and click Enter, navigate to: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]Double click on "FilterAdministratorToken" and set it to "0"

*************************************

Now log-off, and you'll see new account named "Administrator" is available, click on it to login.
Now you are the master of your domain! I recommend if you're going to use this method is to apply it as soon as you do a fresh install of Windows, so you can simply delete whatever administrator you've created in the setup process, and make this one the "real" administrator for your PC, also you can rename this new admin account or change its password like any other account from "User Accounts" in the Control Panel.

A last note/disclaimer:
Please note that disabling UAC and using the built in Adminstrator account will also disable IE7 "Protected Mode", fore more information and a work around please see this post.
Please apply this procedures only if you know what you're doing. Disabling security features in the operating system is not something recommended to the average Joe, and for sure I won't be held accountable for any damaging happens to your system or files resulting from running a full administrator account all the time.


Special thanks to:

- Farstrider for providing the location of the relevant register keys that made applying this method to the home versions of Vista possible!.

- bradavon for his comment/solution of IE7 protected mode.

Oct 19, 2007

Connection Strings

Connection Strings

SQL Server 2005
SQL Native Client ODBC Driver

1. Standard security


Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Are you using SQL Server 2005 Express? Don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2005 Express installation resides.

2. Attach a database file on connect to a local SQL Server Express instance


Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

3. Attach a database file, located in the data directory, on connect to a local SQL Server Express instance


Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.


-----------------------------------------------------------------------------------------------

SQL Native Client OLE DB Provider

1. Standard security


Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Are you using SQL Server 2005 Express? Don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2005 Express installation resides.

2. Trusted connection


Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;

Equivalent key-value pair: "Integrated Security=SSPI" equals "Trusted_Connection=yes"


3. Encrypt data sent over network


Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;

4. Attach a database file on connect to a local SQL Server Express instance


Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.


5. Attach a database file, located in the data directory, on connect to a local SQL Server Express instance


Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

------------------------------------------------------------------------------------------------

SqlConnection (.NET)

1. Standard Security


Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Use serverName\instanceName as Data Source to connect to a specific SQL Server instance.
Are you using SQL Server 2005 Express? Don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2005 Express installation resides.

2. Standard Security alternative syntax

This connection string produce the same result as the previous one. The reason to include it is to point out that some connection string keywords have many equivalents.

Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;



3. Trusted Connection


Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

4. Trusted Connection from a CE device

Often a Windows CE device is not authenticated and logged in to a domain. To use SSPI or trusted connection / authentication from a CE device, use this connection string.


Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;

Note that this will only work on a CE device.

5. Connect via an IP address


Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

DBMSSOCN=TCP/IP. This is how to use TCP/IP instead of Named Pipes. At the end of the Data Source is the port to use. 1433 is the default port for SQL Server.

6. Attach a database file on connect to a local SQL Server Express instance

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;




Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.


7.Attach a database file, located in the data directory, on connect to a local SQL Server Express instance


Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

8.Using an User Instance on a local SQL Server Express instance

The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server 2005 instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL Server instance to a user with limited administrative rights on the computer.


Data Source=.\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\mydb.mdf;User Instance=true;

To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'. To disable the functionality execute sp_configure 'user instances enabled', '0'.

Using CDO Email Encoding

public sub SendEmail(_
astrRecipientEmailAddress, _
astrCCEmailAddress, _
astrBCCEmailAddress, _
astrSenderEmailAddress, _
astrEmailSubject, _
astrEmailText)

'Dimension local variables
Dim objCDOMail


'Create Server Object
Set objCDOMail = Server.CreateObject("CDO.Message")

objCDOMail.From = astrSenderEmailAddress
objCDOMail.To = astrRecipientEmailAddress
objCDOMail.Subject = astrEmailSubject
objCDOMail.TextBody = astrEmailText

'this is the encoding part if you are using text as mail content
objCDOMail.BodyPart.Charset = "utf-8"
objCDOMail.TextBodyPart.Charset = "utf-8"

'this is the encoding part if you are using html as mail content
'create a proper html tag
'with Content-Type: text/html; charset=utf-8
objCDOMail.BodyPart.Charset = "utf-8"
objCDOMail.HtmlBodyPart.Charset = "utf-8"


'check whether to Copy or BCC this email
if len(astrCCEmailAddress) > 0 then
objCDOMail.CC = astrCCEmailAddress
end if
if len(astrBCCEmailAddress) > 0 then
objCDOMail.BCC = astrBCCEmailAddress
end if

objCDOMail.Send


end sub

MSSQL 2005 Backup and Restore SQL Syntax

RESTORE DB
---------------
RESTORE DATABASE myNewDb
FROM DISK = 'C:\Documents and Settings\Administrator\Desktop\family_backup.bak'
WITH MOVE 'myOldDb_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\myNewDb.mdf',
MOVE 'myOldDb_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\myNewDb.ldf',
REPLACE

**Note:
If you are about to restore the old version sql2000 DB,
you can create a sql2005 db with the same name first,
change the DB Properties > Options > Capability Level > SQL2000 (80),
then you proceed with the above sql syntax


BACKUP DB
--------------
use [myNewDb]
BACKUP DATABASE [myNewDb] TO DISK = N'D:\Database Backup\myNewDb.bak'
WITH NOFORMAT, NOINIT, NAME = N'myNewDb-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO

Oct 18, 2007

Runtime Web.config / App.config Editing




Web.config configuration files and app.config project item files, which get converted to "ExecutableName.exe.config" at build time, both support the convenient appSettings section with its own read method in the System.Configuration.ConfigurationSettngs class. The appSettings section stores element name / value pairs in the format:

You can store as many of these elements as you want, read them out at runtime, and use the values in the application. If you have an item that contains multiple values and you would like to keep them together, you can store them as a single string, delimited with a pipe | or other symbol, read them out at runtime, and call the String.Split() method to parse them into a useable string array.

I often read out my appSetting values into a NameValueCollection at runtime, which provides one-shot acess to the entire collection in memory:

NameValueCollection mySettings = System.Configuration.ConfigurationSettings.AppSettings;
string connStr = mySettings["connString"];

But what about being able to change, add, and save appSettings items while that app is running in response to user input or other actions, instead of just reading them out? Nada, Zippo, Efes! You have to open the config file manually and add them by "hand". Well that kinda stinks, don't you think? So here's my take on a convenient little class that allows you to either modify, add or delete any appSettings element, in either your Executable, Console or ASP.NET web application at runtime, on the fly. Bear in mind of course, that if you modify a web.config on a running ASP.NET app, the ASP.NET worker process will recycle. Users currently using your app aren't exactly guaranteed to have a fun experience when this happens...

Original Source: http://www.eggheadcafe.com/articles/20030907.asp
By Peter A. Bromberg, Ph.D.