2009/08/25

MSSQL- use runas to login to sql server with windows authentication

For some situation, you will need to use remote computer to connect to different server and login the sql server by windows authentication. But good news, you can use the following technique so that you can just login to the specific sql server from local machine.

- Open cmd
- Type:
runas /user: domain\loginname "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe"

or create a short cut with the following path:
C:\WINNT\system32\runas.exe /user:domain\login "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe"

So you save your time by without having to connect to your remote computer ;)

Reporting Services- Thousand separator

With decimal:
Format(First(Fields!IRTotal.Value, "TDec"), "#,###0.00")

Without decimal:
Format(First(Fields!IRTotal.Value, "TDec"), "#,###0")

2009/08/21

Reporting Services- Error [BC30494]

I am working on Report and I got this error:

error [BC30494] Line is too long.

Problem\Solution: If your table is too big will causes this error. There is a simple solution: rename all your text boxes to a short name, for example: textbox1234 - > t1234

If the above solution doesn't help, try to split the table into two.

Enjoy

2009/08/07

SQL- Stored procedure: list out all columns of a table

Sometimes, you need to create a temp table or whatever, you need to list out all the columns. If just only 2 or 3 columns sure that won't bother you, but if the table has 100 columns, its gonna kill you if you have to type everyone of it. So, here is the stored procedure for you. It will list out all columns of a table in one line and separate the columns with comma.


Usage: exec SP_ColName 'TABLENAME'

===
IF EXISTS (SELECT * FROM [sysobjects] WHERE [id] = OBJECT_ID(N'[dbo].[SP_ColName]'))
DROP PROCEDURE [SP_ColName]
GO
-- Usage: SP_ColName
-- It prints out the column names of the given table in one line delimited by ', '
CREATE PROCEDURE [SP_ColName] @TableName sysname
AS
BEGIN
DECLARE @ColumnList nvarchar(max)
DECLARE @ColumnName nvarchar(max)
SET @ColumnList = ''
DECLARE cur CURSOR FOR
SELECT [name] FROM [syscolumns] WHERE [id] = OBJECT_ID(@TableName) ORDER BY [colid]
OPEN cur
DECLARE @OutStr varchar(max)
SELECT @OutStr = ''
PRINT UPPER(@TableName)
FETCH NEXT FROM cur INTO @ColumnName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @OutStr = @OutStr + @ColumnName + ', '
FETCH NEXT FROM cur INTO @ColumnName
END
IF LEN(@OutStr) >= 2 PRINT LEFT(@OutStr,LEN(@OutStr)-2)
CLOSE cur
DEALLOCATE cur
END
GO

C# Is null or string is empty

I just found that there is a "new" thing to check the string is null or empty (asp.net 2.0)

old way:
if (var == string.Empty && var == null)

new way:
if (String.IsNullOrEmpty(var))

It looks cool right? Enjoy!

2009/08/04

C#- Using Nullable Types

Nullable types can represent all the values of an underlying type, and an additional null value. Nullable types are declared in one of two ways:

System.Nullable variable

-or-C

T? variable

T is the underlying type of the nullable type. T can be any value type including struct; it cannot be a reference type.

For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies "undefined". In many programming applications, most notably database interactions, variables can exist in an undefined state. For example, a field in a database may contain the values true or false, but it may also contain no value at all. Similarly, reference types can be set to null to indicate that they are not initialized.

This disparity can create extra programming work, with additional variables used to store state information, the use of special values, and so on. The nullable type modifier enables C# to create value-type variables that indicate an undefined value.

Any value type may be used as the basis for a nullable type. For example:

C#
int? i = 10;
double? d1 = 3.14;
bool? flag = null;
char? letter = 'a';
int?[] arr = new int?[10];

SQL- List out all the columns in a table

Assume that you wish to check for rows in a table where all values are NULL. There are many columns in the table and you want to avoid hand coding them. Instead, you can create a script to do the work for you:

Select column_name + ' IS NULL AND'
FROM INFORMATION_SCHEMA.columns
where table_name = 'whatever'

Enjoy! this query is very useful ;)