CASE (Transact SQL)

  • 0

CASE (Transact SQL)

Evaluates a list of conditions and returns one of multiple possible result expressions.

CASE has two formats:

· The simple CASE function compares an expression to a set of simple expressions to determine the result.

· The searched CASE function evaluates a set of Boolean expressions to determine the result.

Examples:

A. Using a SELECT statement with a simple CASE function

Within a SELECT statement, a simple CASE function allows for only an equality check; no other comparisons are made. The following example uses the CASE function to change the display of product line categories to make them more understandable.

USE AdventureWorks;
GO
SELECT   ProductNumber, Category =
CASE ProductLine
WHEN ‘R’ THEN ‘Road’
WHEN ‘M’ THEN ‘Mountain’
WHEN ‘T’ THEN ‘Touring’
WHEN ‘S’ THEN ‘Other sale items’
ELSE ‘Not for sale’
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO

B. Using a SELECT statement with a searched CASE function

Within a SELECT statement, the searched CASE function allows for values to be replaced in the result set based on comparison values. The following example displays the list price as a text comment based on the price range for a product.

USE AdventureWorks;
GO
SELECT   ProductNumber, Name, ‘Price Range’ =
CASE
WHEN ListPrice =  0 THEN ‘Mfg item – not for resale’
WHEN ListPrice < 50 THEN ‘Under $50’
WHEN ListPrice >= 50 and ListPrice < 250 THEN ‘Under $250’
WHEN ListPrice >= 250 and ListPrice < 1000 THEN ‘Under $1000’
ELSE ‘Over $1000’
END
FROM Production.Product
ORDER BY ProductNumber ;
GO


Bookmark and Share


  • 0

Paging in SQL Server

Visit: http://www.zonixsoft.com (our official Website)

Web Application Performance (Paging in Sql Server)

As a Web developer, you know by now that using the default paging capabilities of ASP.NET Webcontrols like DataGrid and GridView cause the Performance issue when we have thousands and thousands of records in our database; because with every roundtrip to the data-server, you get ALL the records ALL of the time. This is fine perhaps for very small databases. But in web application, you always have to count on its Performance.

One alternate approach to this scenario is to serve “On Demand” records. That is; for example; retrieving 1-100 records, then 101-200 records result set based on some event rather than retrieving all the records all of the time. I am talking about “Paging in Sql Server”.

My-SQL

The simplest solution is provided by My-SQL using LIMIT keyword. Check out the below Query:

Select * From Product Limits 15,5

It will retrieve results from 16 to 20 records. In above Query, 15 if the offset from where you want the result set records and 5 is the number of records you want to retrieve.

Sql Server 2005

Unfortunately, SQL Server does not have an equivalent to “Limit” keyword. Its nearest is TOP N, which returns the first N rows. Still there are two alternative than we can use for “Paging”.

Using ROW_NUMBER()

Sql 2005 includes the ROW_NUMBER() function, which adds an integer field to each record. In other words, it adds the record’s position within the result set as an additional field so that the first record has a 1, the second a 2, etc.

To ensure the numbering is consistent, however, SQL Server needs to know how to sort the data. Because of this, ROW_NUMBER() must immediately be followed by the OVER() function. OVER() has one required parameter, which is an ORDER BY clause. The basic syntax is:

SELECT ROW_NUMBER() OVER(ORDER BY [Name]) as RowNum,

[Name], [SKU], [ManufacturerPartNumber]

FROM Product

The Product data in result set will be appeared sorted by Name, and it has an extra column indicating each record’s position within the results.

If we want to limit the results displayed to a certain range, we need to nest this SELECT inside another one. To limit our results to records 5 to 10, we can use the following query:

SELECT * FROM

(SELECT ROW_NUMBER() OVER(ORDER BY [Name]) AS RowNum,

[Name], [SKU], [ManufacturerPartNumber] FROM Product) AS Product1

WHERE RowNum >= 5 AND RowNum <= 10

With Nested Queries

There is one another solution that consists only of 1 sql-statement, and so is efficient and quick, even with large databases. The good news is : it will work ! Always ! it’s looking rather a bit complicated; but its not!

Let us see the Example directly to limit our results to records 6 through 10,

SELECT P.[Name], P.[SKU], P.[ManufacturerPartNumber] FROM (

SELECT TOP 5 [ProductID],[Name] FROM (

SELECT TOP 10 [ProductID],[Name]

FROM Product

ORDER BY Product.[Name] ASC) as Product1

ORDER by Product1.[Name] DESC) as Product2

INNER JOIN Product P ON Product2.ProductID = P.ProductID

ORDER by Product2.[Name] ASC

Where 5 is the number of rows you want to retrieve and 10 is the offset.

The innermost Sql Statement will fetch 10 records (1 to 10) Order by Product Name in Ascending Order.

This means that if you have a table with 10,000 records, and you want to have the last 10 records, the innermost SELECT statement will indeed retrieve 10,000 records. This of course has it’s ramifications on performance, but since it’s only the primary key fields (which are indexed) and optionally some sort fields, the impact will be minimal.

The middle Sql Statement will fetch 5 records (10 to 6) Order by Product Name in Descending Order. And Finally the Outer Sql Stateement is simply select statement that will fetch the result in Ascending Order (6 to 10). In Innermost and Midddle Sql Statement only primary key fields or the fields which are indexed are used.

After looking at this in Query Analyzer, it appears that the extra nesting does not add very much to the load. The SQL statement plays with the sorting orders to limit the records, thus resulting in paged recordsets. This means that there should be at least ONE field to be sorted. If there isn’t any, sort on the primary key field(s) !


Bookmark and Share


  • 2

Using TRY-CATCH to Rollback a Transaction in the Face of an Error

Visit : http://www.zonixsoft.com (our official website)

Error Handing in Sql Server 2005.
The release of Sql Server 2005 has provided us somany features over its predecessor. No doubt that more preference is given to the tasks performed by the administrator. But there are some new development features added to make your Sql Code more powerful and error resistance, specially; Stored Procedures.
The Feature – “TRY…CATCH”
The Most impressive functionality improvement added for developers is “Exceptional Handling” technique. There is no beneficial reason if you are not writing your code in “Try.. Catch” block.
• A TRY Block – the TRY block contains the code / script that might cause an exception
• A CATCH Block – if an exception occurs from one of the statements in the TRY block, control is branched to the CATCH block, where the exception can be handled, logged, and so on.
Checking @@ERROR – the “sql 2000” Way of Handling Errors in Stored Procedure
Just have a look at below Store Procedure example.
CREATE PROC usp_AccountTransaction
@AccountNum INT,
@Amount DECIMAL
AS
BEGIN
BEGIN TRANSACTION –beginning a transaction..
UPDATE MyChecking SET Amount = Amount – @Amount
WHERE AccountNum = @AccountNum
IF @@ERROR != 0 –check @@ERROR variable after each DML statements..
BEGIN
ROLLBACK TRANSACTION –RollBack Transaction if Error..
RETURN
END
ELSE
BEGIN
UPDATE MySavings SET Amount = Amount + @Amount
WHERE AccountNum = @AccountNum
IF @@ERROR != 0 –check @@ERROR variable after each DML statements..
BEGIN
ROLLBACK TRANSACTION –RollBack Transaction if Error..
RETURN
END
ELSE
BEGIN
COMMIT TRANSACTION –finally, Commit the transaction if Success..
RETURN
END
END
END
GO
Yes!.. This is what we used to code a Stored Procedure in Sql 2000; Check for @@ERROR after every DML (Data Manipulation) Statements and Commit / RollBack the transaction.
While working with SQL Server 2000, detecting errors could only be handled by checking a global error variable, @@ERROR. Because the @@ERROR variable value is reset after each SQL statement, this leads to rather bloated stored procedures, as the variable must be checked after each statement with code to handle any problems.
The TRY…CATCH block in SQL Server 2005 offers a much more readable syntax and one that developers are more familiar with. And yes, SQL Server 2005 still supports to @@ERROR Approach. In this article we’ll look at the new TRY…CATCH block and examine how it can be used to rollback a transaction in the face of an error. Lets move on to it!
Handling Errors With SQL Server 2005’s TRY…CATCH Blocks
In Fact, there is really nothing new to be describe and discuss on TRY…CATCH Block; as we all know with any programming languages, TRY…CATCH block executes a number of statements in the TRY block. If there are no errors in any of the statements, control proceeds to after the CATCH block. If, however, one of the statements causes an error, control branches immediately to the start of the CATCH block.
Basic Syntax is,
BEGIN TRY
Try Statement 1
Try Statement 2

Try Statement M
END TRY
BEGIN CATCH
Catch Statement 1
Catch Statement 2

Catch Statement N
END CATCH

The following system functions are available in the CATCH block and can be used to determine additional error information:
Function                           Description
ERROR_NUMBER()             Returns the number of the error.
ERROR_SEVERITY()           Returns the severity.
ERROR_STATE()                 Returns the error state number.
ERROR_PROCEDURE()       Returns the name of the stored procedure  where the error occurred.
ERROR_LINE()                   Returns the line number inside the routine that caused the error.
ERROR_MESSAGE()            Returns the complete text of the error message.

Take a look at below example,
BEGIN TRY
SELECT GETDATE()
SELECT 1/0–Evergreen divide by zero example!
END TRY
BEGIN CATCH
SELECT ‘There was an error! ‘ + ERROR_MESSAGE()
RETURN
END CATCH;
Using TRY…CATCH to Rollback a Transaction in the Face of an Error
As you saw in earlier example, one of the downsides of the @@ERROR variable approach is that to implement Transaction; we must check this variable after each and every DML SQL statement to determine if an error occurred and, if so, to rollback the transaction. With SQL Server 2005’s TRY…CATCH block, however, these types of scripts are greatly simplified.
Lets Alter the Previous Example!
ALTER PROC usp_AccountTransaction
@AccountNum INT,
@Amount DECIMAL
AS
BEGIN
BEGIN TRY –Start the Try Block..
BEGIN TRANSACTION — Start the transaction..
UPDATE MyChecking SET Amount = Amount – @Amount
WHERE AccountNum = @AccountNum
UPDATE MySavings SET Amount = Amount + @Amount
WHERE AccountNum = @AccountNum
COMMIT TRAN — Transaction Success!
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRAN –RollBack in case of Error
— you can Raise ERROR with RAISEERROR() Statement including the details of the exception
RAISERROR(ERROR_MESSAGE(), ERROR_SEVERITY(), 1)
END CATCH
END
GO
Just look at the simplicity and line of code than previous example!
In the TRY block a transaction is started and the two UPDATE statements are performed. If both UPDATEs succeed, the COMMIT will be reached and the transaction committed. If, however, either one produces an error, control will be execute CATCH block where the transaction will be rolled back.
Also, you can “re-raises” the error (using RAISERROR) so that the error information will be passed up to your .Net application from where you are calling the Stored Procedure, in case if you want to use the error information to process further steps anyhow.


Bookmark and Share


  • 0

Passing lists to SQL server stored procedures

Visit : http://www.zonixsoft.com (our official wbsite)

Article is about:

The ability to pass “a list of values” from .Net as a parameter to a T-SQL based stored procedure.

Scenarios:

There are lots of scenarios where we need to pass a list of values to save in database. Here’s a couple of obvious ones:

· INSERT a list of values into the database in one “chunky” call (e.g. some IDs from a CheckBoxList)

· SELECT rows where IDs are IN (<list of IDs>)

Some general Approaches:

Taking the INSERT statements as an example, there are various general approaches that we adopt to achieve the desired result:

· Use dynamic / Inline SQL!  But ideally say, dynamic / Inline SQL is rarely the ideal solution for obvious reasons.

· Make a stored proc call for each ID to insert. This is the most common approach we see in various projects, mainly because it is the easiest to implement. The drawback of course is if we were to insert 60 values, it would result in 60 “chatty” calls to the database.

· Pass comma separated values via a VARCHAR (or similar) parameter. This works fine but has messy “string splitting” in the stored procedure to extract the IDs and then build the SQL statement in the procedure itself. Prone to SQL injection and not the best performance.

· Pass the list as an XML parameter. This is nicer and is the point of this article.

Coming to the main Point, Using XML:

Using XML for “list passing” has a number of benefits, in particular the ability to pass lists of more “complex types” rather than just single values.

Lets take an example. Suppose we are having 2 CheckedListBox; one is list of Users and another is the list of tasks / roles that can be assigned to Users. We want to store the values in Table which is having Fields UserID and TaskID. The Stored Procedure will accept Parameter with XML Datatype as,

CREATE PROCEDURE [dbo].[usp_InsertUserTask]
@UserTaskXML XML
AS
BEGIN
INSERT INTO UserTasks (UserID,TaskID)
SELECT
UserTaskTab.UserTaskCol.value(‘UserID[1]’,’int’) AS UserID,
UserTaskTab.UserTaskCol.value(‘TaskID[1]’,’int’) AS TaskID
FROM @UserTaskXML.nodes(‘//UserTaskList/UserTaskData’) AS UserTaskTab(UserTaskCol)
END

To call this in Stored Procedure, you would have something like this:

EXEC    [dbo].[usp_InsertUserTask]
@UserTaskXML = ‘<UserTaskList>
<UserTaskData>
<UserID>1</UserID>
<TaskID>100</TaskID>
</UserTaskData>
<UserTaskData>
<UserID>2</UserID>
<TaskID>200</TaskID>
</UserTaskData>
</UserTaskList>’

In your application, your C# calling code could be:

SqlConnection sqlCN = new SqlConnection();
sqlCN.ConnectionString = ConfigurationManager.AppSettings[“DBConn”].ToString();
string strQuery = “usp_InsertUserTask”;
SqlParameter[] sqlParams = new SqlParameter[1];
sqlParams[0] = new SqlParameter(“@UserTaskXML”, GetStudyDataXMLString());
SqlHelper.ExecuteNonQuery(sqlCN, CommandType.StoredProcedure, strQuery, sqlParams);
if (sqlCN.State == ConnectionState.Open)
sqlCN.Close();
sqlCN.Dispose();

which calls the method below to translate the UserID and TaskID from CheckBoxLists into an XML String:

private string GetUserTaskListXML()
{
try
{
StringBuilder XMLString = new StringBuilder();
XMLString.AppendFormat(“<UserTaskList>”);
for (int iUserCount = 0; iUserCount < UserCheckBoxList.Items.Count; iUserCount++)
{
if(UserCheckBoxList.Items[iUserCount].Selected)
{
for (int iTaskCount = 0; iTaskCount < TaskCheckBoxList.Items.Count; iTaskCount++)
{
if(TaskCheckBoxList.Items[iTaskCount].Selected)
{
XMLString.AppendFormat(“<UserTaskData>”);
XMLString.AppendFormat(“<UserID>{0}</UserID>”, UserCheckBoxList.Items[iUserCount].value);
XMLString.AppendFormat(“<TaskID>{0}</TaskID>”, UserCheckBoxList.Items[iUserCount].value);
XMLString.AppendFormat(“</UserTaskData>”);
}
}
}
}
XMLString.AppendFormat(“</UserTaskList>”);
}
catch (Exception Ex)
{
throw Ex;
}
return XMLString.ToString();
}

Here, StringBuilder is used for the xml concatenation as in this case I think it fits the bill but purists might prefer an XmlTextWriter approach. In summary, it performs very well and is adaptable for various lists of objects and more complex structures.


Bookmark and Share


  • 0

New in C-Sharp 3.0

Visit: http://www.zonixsoft.com

New in C-Sharp 3.0

New in C# 3.0
This article discusses the following major new enhancements expected in C# 3.0:
• Implicitly typed local variables
• Anonymous types
• Extension methods
• Object and collection initializers
• Lambda expressions
• Query expressions
• Expression Trees

Implicitly typed local variables:
C# 3.0 introduces a new keyword called “var”. Var allows you to declare a new variable, whose type is implicitly inferred from the expression used to initialize the variable.
Syntax: var i=1;
The preceding line initializes the variable i to value 1 and gives it the type of integer. It is not an object or variant.
Anonymous types:
C# 3.0 gives you the flexibility to create an instance of a class without having to write code for the class beforehand. So, you now can write code as shown below:
new {StudentID=1, StudentName=”XYZ”, Marks=90}
The preceding line of code, with the help of the “new” keyword, gives you a new type that has three properties: StudentID, StudentName, and Marks. Behind the scenes, the C# compiler would create a class that looks as follows:
class __Anonymous1
{
private int _StudentID = 1;
private string _StudentName = “XYZ”;
private int _Marks = 64;
public int StudentID {get { return _StudentID; } set { _StudentID = value; }}
public string StudentName {get { return _StudentName; } set { _StudentName = value; }}
public int Marks {get { return _Marks; } set { _Marks = value; }}
}

Extension methods:

Extension methods enable you to extend various types with additional static
methods. Extension methods can be declared only in static classes and are identified by
the keyword “this” as a modifier on the first parameter of the method.

The following is an example of a valid extension method:
public static int ToInt32(this string s)
{
return Convert.ToInt32(s) ;
}
Object and collection initializers:
C# 3.0 is expected to allow you to include an initializer that specifies the initial values of the members of a newly created object or collection. This enables you to combine declaration and initialization in one step.
For instance, if you defined a CoOrdinate class as follows:
public class CoOrdinate
{
public int x;
public int y;
}

You then could declare and initialize a CoOrdinate object using an object initializer, like this:

var myCoOrd = new CoOrdinate{ x = 0, y= 0} ;
You should easily be able to give values to collections in a rather concise and compact manner in C# 3.0.
In C# 2.0 Code we write :

List<string> animals = new List<string>();

animals.Add(“monkey”);
animals.Add(“donkey”);
animals.Add(“cow”);
animals.Add(“dog”);
animals.Add(“cat”);

It can be written in  C# 3.0 shortened to simply.
List<string> animals = new List<string> {“monkey”, “donkey”, “cow”, “dog”, “cat” } ;
Lambda expressions:
C#(1.x) allowed you to write code blocks in methods, which you could invoke easily using delegates.Delegates are definitely useful, and they are used throughout the framework, but in many instances you had to declare a method or a class just to use one. Thus, to give you an easier and more concise way of writing code, C# 2.0 allowed you to replace standard calls to delegates with anonymous methods.
In C# 2.0, using anonymous methods, you could rewrite the code as follows:
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = delegate()
{
Console.Writeline(“Hiya!!”);
};
myDelegate();
}
}
The above code can now be replaced with the following code in C# 3.0:
class Program
{
delegate void DemoDelegate();
static void Main(string[] args)
{
DemoDelegate myDelegate = () => Console.WriteLine(“Hiya!!”) ;
myDelegate();
}
}

Reference Site: http://www.developer.com/net/csharp/article.php/10918_3561756_1


Bookmark and Share


  • 3

Threading

Visit: http://www.zonixsoft.com

C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.A C# program starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multi-threaded by creating additional threads.The CLR assigns each thread its own memory stack so that local variables are kept separate.

Threading enables your C# program to perform concurrent processing so you can do more than one operation at a time. The System.Threading namespace provides classes and interfaces that support multithreaded programming and enable you to easily perform tasks such as creating and starting new threads, synchronizing multiple threads, suspending threads, and aborting threads. The advantage of threading is the ability to create applications that use more than one thread of execution.

How Threading Works

Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. A thread scheduler ensures all active threads are allocated appropriate execution time, and that threads that are waiting or blocked – for instance – on an exclusive lock, or on user input – do not consume CPU time.

On a single-processor computer, a thread scheduler performs time-slicing – rapidly switching execution between each of the active threads.

On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency – where different threads run code simultaneously on different CPUs. It’s almost certain there will still be some time-slicing, because of the operating system’s need to service its own threads – as well as those of other applications.

Killing a Thread:

We can kill a thread by calling the

 

Abort method of the thread.

MyThread.Abort();

Suspend and Resuming Thread:

We can suspend the execution of a thread and once again start its execution from another thread using the Thread object’s Suspend and Resume methods.

  MyThread.Suspend() // causes suspend the Thread Execution.

 

  MyThread.Resume() // causes the suspended Thread to resume its execution.

 

 

Creating and Starting Threads

Threads are created using the Thread class’s constructor, passing in a ThreadStart delegate – indicating the method where execution should begin.  Here’s how the ThreadStart delegate is defined:

public delegate void ThreadStart();

Calling Start on the thread then sets it running. The thread continues until its method returns, at which point the thread ends. Here’s an example, using the expanded C# syntax for creating a TheadStart delegate:

class ThreadTest {

  static void Main() {

    Thread t = new Thread (new ThreadStart (Go));

    t.Start();   // Run Go() on the new thread.

    ……

  }

  static void Go() { …..}

A thread can be created more conveniently using C#’s shortcut syntax for instantiating delegates:

static void Main() {

  Thread t = new Thread (Go);    // No need to explicitly use ThreadStart

  t.Start();

 

}

static void Go() { … }

In this case, a ThreadStart delegate is inferred automatically by the compiler. Another shortcut is to use an anonymous method to start the thread:

static void Main() {

  Thread t = new Thread (delegate() { Console.WriteLine (“Hello!”); });

  t.Start();

}

A thread has an IsAlive property that returns true after its Start() method has been called, up until the thread ends.A thread, once ended, cannot be re-started.


  • 21

Some very special features of crystal reports

Visit: .comhttp://www.zonixsoft

Create a Watermark

A watermark is a graphical or textual element that is placed behind the rest of yourreport. Examples of useful watermarks are your company logo, the word “Draft,”or other items that you may want to appear right behind the rest of the elements of your report.

 

1. Open the report you wish to add the watermark to. Ensure that the Designtab is selected.

2. Position your mouse over the gray Page Header section name. Right-click.

3. Choose Insert Section Below from the pop-up menu. This will insert a second page header section (you’ll now see Page Header a and Page Header b).

4. Insert the graphic or text you want to appear behind the rest of the report into Page Header b (it’s helpful if the graphic has been lightened with a graphic editing program, or if the text is formatted with a light shade of font color).

5. If you want the watermark to appear more toward the vertical center of the page, make Page Header b taller and move the graphic or text farther down in the section.

6. If you want to repeat the watermark more than once on a page, insert it into Page Header b several times, or copy and paste the original item several times in Page Header b.

7. Position your mouse over the gray Page Header b section name. Right-click.

8. Choose Format Section from the pop-up menu in Crystal Reports 8.5, or Section Expert from the pop-up menu in Crystal Reports 9. This will display the Section Expert.

9. Choose the Underlay Following Sections check box for Page Header b in the Section Expert. Click OK to close the Section Expert.

10. If you wish to underlay items in the existing Page Header a section (field titles, the report title, and so forth), swap Page Header b and Page Header a by holding your mouse button down on either gray page header section name. When the mouse cursor changes to the hand icon, drag and drop the page header on top of the other page header. The sections will swap.

 

Change Graphics According to a Condition

Crystal Reports features the capability to format report objects (database fields, formulas, text objects, and so forth) conditionally, depending on the contents of a field, formula, or other condition. However, you may wish to actually insert several different graphic or picture files on your report and display them only when a certain condition occurs.

 

1. Open or create the report that you wish to add multiple conditional graphics to.

2. Add as many graphics to the report as are required. For example, if you want to display “thumbs up” and “thumbs down” graphics, add both graphic files to the report.

3. Place the graphics side by side, so that you may select each graphic individually.

4. Select the first graphic by clicking on it. Right-click. From the pop-up menu, select Format Graphic. The Format Editor will appear.

5. Select the Common tab. Next to the Suppress check box, click the Conditional Formula button. The Format Formula Editor will appear.

6. Add a Boolean formula to conditionally suppress the graphic. For example, to suppress the graphic if Last Year’s Sales is less than $50,000, enter a formula similar to this:

{Customer.Last Year’s Sales} < 50000

7. Click the Save button to close the Format Formula Editor. Click OK to close the Format Editor.

8. Repeat steps 4–7 for the remaining graphics, using “mutually exclusive” condition formulas, so that only one graphic at a time will display. For example, to suppress another graphic if Last Year’s Sales is greater than or equal to $50,000, enter a formula similar to this:

{Customer.Last Year’s Sales} >= 50000

9. Select each graphic and place it on top of the others, so that all graphics are placed in the same position. Because of the “mutually exclusive” conditional formatting, only one will appear at a time when the report is displayed.

 

Use WingDings and Other Symbol Fonts

By default, Crystal Reports uses a particular font face and size for report objects. While you can change the default font choices in the File | Options dialog box, you can also choose font face and size for individual report objects. You are not limited to standard letter-oriented fonts—you may choose symbol fonts such as Wingdings and Webdings as easily as you can choose letter-oriented fonts.

 

1. Add a database field, text object, formula, or other textual element that you wish to display as a symbol to the report.

2. Ensure that the object returns a character value that will “map” to the proper symbol. For example, to display a smile or frown with theWingdings font, you might create a formula similar to this:

If {Customer.Last Year’s Sales} < 50000 Then

“L”

Else

“J”

3. Select the object you wish to format with a symbol font.

4. Either using the Formatting toolbar or the Format Editor, select the symbol font (such as Wingdings or Webdings) that you’d like to use.

Tip: To determine what the proper character value is for the desired symbol, make use of the Windows Character Map. You may choose the symbol font and character you’d like to use and copy the character to the clipboard. Then, when you paste the character into a Crystal Reports formula or text object, the proper symbol will appear when you format the object with the symbol font. The Character Map is available from the System Tools submenu of the Accessories menu from the Start button Programs list.

 

 

Eliminate Blank Address Lines

Crystal Reports is often used for form letters, envelopes, or other mailing applications. In many cases, there is a need to eliminate blank address or “suite number” lines in an address for certain addresses. This can be accomplished in two ways: by embedding multiple address lines in a text object, or by using multiple report sections.

The text object method, while simpler in approach, will not automatically adjust vertical placement of objects that follow on the report. For example, if the address contains four lines in one form letter and three lines in the next, the remainder of the form letter below the text object will not move up or down automatically depending on the vertical size of the text object. For situations that require automatic vertical adjustment of text that follows the address, you’ll need to use multiple report sections.

 

Text Object

To eliminate blank lines using a text object, follow these steps:

1. Add a text object to your report. You will combine several database fields inside this text object.

2. Display the Field Explorer. Drag desired fields into the text object, separating database field with necessary characters, such as commas and spaces. In particular, drag the first address line (that might contain the street number) into the text object. Press ENTER to add a carriage return. Then, drag the second address line that will not always contain data (this might contain the suite number) into the text object. Press ENTER again. Then, drag in City, State, and Zip Code.

3. End editing by clicking anywhere outside the text object.

4. Select the text object you just created. Right-click and choose Format Text from the pop-up menu. The Format Editor will appear.

5. On the Common tab, click the Suppress Embedded Field Blank Lines options. This will suppress any lines in the text object that contain no data.

 

Shade Every Other Report Line

Although most companies have replaced mainframe “green-bar” or “blue-bar” reports with laser-printed output, there’s still a benefit to shaded sections when heavy textual information is being viewed. Shading lines in alternate colors or shades makes it easier for the eye to follow the line across the page. Crystal Reports enables this technique to be easily used for reports that may be printed on paper or viewed online.

By using the Mod function (which performs modulus arithmetic—returning the remainder of a division operation instead of the quotient), combined with the RecordNumber function (which simply numbers each report record sequentially), you may produce creative banded reports.

 

1. Point to the gray Details section name in the Design tab, or the gray D section name in the Preview tab. Right-click.

2. Choose Format Section from the pop-up menu in Crystal Reports 8.5 or Section Expert from the pop-up menu in Crystal Reports 9. The Section Expert will appear.

3. Ensure that the Details section is selected in the Section Expert. Click the Color tab.

4. Click the Conditional Formatting button. The Format Formula Editor will appear.

5. Enter a formula similar to the following (you may change the formatting color to your desired color):

If RecordNumber Mod 2 = 0 Then crSilver Else crNoColor

6. You may alter the frequency of the banding if you desire. For example, to shade every two lines, you can modify the formula as follows:

If RecordNumber Mod 4 In 1 To 2 Then crSilver Else crNoColor

Tip: Use of the crNoColor color constant instead of crWhite will show the alternating report sections with a certain degree of transparency. This may be helpful for certain reporting situations where you wish background information (such as a watermark) to appear behind the sections.

Courtesy: The Seo Guru, A Software Development Company, Best OOPS Blog Site, Link Submission, Thanks to Shopping  Site for Link Exchanging


  • 1

Code Sample to Upload file to FTP Server

Visit: http://www.zonixsoft.com

check out below code to upload file to FTP Server:

 

/// <summary>
/// Code to upload file to FTP Server
/// </summary>
/// <param name=”strFilePath”>Complete physical path of the file to be uploaded</param>
/// <param name=”strFTPPath”>FTP Path</param>
/// <param name=”strUserName”>FTP User account name</param>
/// <param name=”strPassword”>FTP User password</param>
/// <returns>Boolean value based on result</returns>
private bool UploadToFTP(string strFilePath, string strFTPPath, string strUserName, string strPassword)
{
        try
        {
                //Create a FTP Request Object and Specfiy a Complete Path
                string strFileName = strFilePath.Substring(strFilePath.LastIndexOf(“\”) + 1);
                FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(strFTPPath + @”/” + strFileName);
                //Call A FileUpload Method of FTP Request Object
                reqObj.Method = WebRequestMethods.Ftp.UploadFile;
                //If you want to access Resourse Protected You need to give User Name and PWD
                reqObj.Credentials = new NetworkCredential(strUserName, strPassword);
                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(strFilePath);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                reqObj.ContentLength = fileContents.Length;
                Stream requestStream = reqObj.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
                response.Close();
        }
        catch (Exception Ex)
        {       // report error
                throw Ex;
        }
        return true;
}


  • 0

Icon in the Address bar Textbox in Browsers

Visit: http://www.zonixsoft.com

(Short Cut) Icon in the Address bar Textbox in Browsers

You have to insert this line after the <head> tag in your html / ASPX / Master Page.

<link rel=”shortcut icon” href=”images/MyComp.ico”>

Make sure the image is file type .ico (for icon). Also, after inserting this line, the image may not appear immediately. So, Try to Refresh the page.

Here’s an article for that:

How to Add a Shortcut Icon to a Web Page

 

Other way to achieve the same thing is as follows

 

       Save the icon with the default file name of favicon.ico to the root directory of your domain—for example, www.microsoft.com/favicon.ico. The first time a user visits your Web page, Internet Explorer automatically searches for this file and places the icon in the address bar, next to all favorites linking to your site, and on page tabs. In Internet Explorer 5 and Internet Explorer 6, the icon will appears only after a user adds the site to the Favorites menu.

 

You can use either method, or both. However, if you use the First method, whichever icon you point to in the link tag on each page will be displayed instead of the default favicon.ico file at the root of your domain.


Bookmark and Share


  • 0

Maintaining Fck editor value during postbacks

Here is the code to maintain value of FckEditor value during postbacks,

 

Write in Page_Load event,

 

Page.ClientScript.RegisterOnSubmitStatement(

fckContentPage.GetType(),

“fckContentPage”,

“FCKeditorAPI.GetInstance(‘” + fckContentPage.ClientID + “‘).UpdateLinkedField();”);

 

Here fckContentPage is ID of FckEditor


Bookmark and Share