Translate

Pages

Tuesday, 14 May 2013

Simple Report Using Crystal Reports


Let's start by creating a new website in VS2010. See the following screen:
Figure 1
As per figure 1, create a new website in VS2010 and name it as per your choice. Now let me show you the database table structure.
Figure 2
The above figure shows the db table structure. And the below figure (Figure 3) will show you some sample data in the table:
Figure 3
If you want to run this sample project directly, then you can download the database script from the link at the top.
Now we have to create an xsd file as a blank data source as we are going to use strong data type. Here I will divide this tutorial in 5 sub sections as mentioned below:
·         Simple report using Crystal Reporting Tool
·         Group report
·         Chart report
·         Sub report
·         Cross tab report
Simple Report using Crystal Report
The below figure shows you the process to create an XSD file.
For adding an XSD file, click on Solution Explorer -> Right Click on Project -> click on Add new Item and then it will show you the below screen.
Figure 4
Click on the ok button, so it will ask for confirmation to put that file in App_Code folder. Just click ok and that file will open in the screen as a blank screen.
Now we will add one blank datatable to that XSDfile. Just right click on the file and select Add -> Datatable. It will add one DataTable1 to the screen. Figure 5 shows how to add datatable to XSD file.
Figure 5
Now datatable1 is added to XSD file. Now we will add data column to the datatable1 as per figure 6. Remember whatever fields (columns) we add here, it will be available to show on the report. So add column which you want to display in your reports one by one here.
Figure 6
Remember to give the exact same name for data column as in database and also select data type which is the same as database, otherwise you will get an error for field and data type mismatch.
Once we add all the required columns in datatable, then set property for the datacolumn as it has in database. The below figure will show you how to set property for data columns. Default datatype for all the columns is string here so if datatype is other than string then only change it manually.
Just right click on the datacolumn in datatable and select property and from property window, select appropriatedatatype from DataType Dropdown for that datacolumn.
Figure 7
That's it. XSD file creation has been done. Now we will move to create Crystal report design.
Just click on the Solution Explorer -> Right click on the project name and select crystal reports. Name it as per your choice and hit the add button.
Figure 8 will show you the creation process of Crystal reports.
Figure 8
Click on the add button and one .rpt file will be added to the solution. And also, it will ask for the report creation type of how you want to create the report. Figure 9 will show you a screenshot.
Figure 9
Just click ok button to proceed. It will lead you to figure 10:
Figure 10
Under project data, expand ADO.NET Datasets and select DataTable1 and add to the selected table portion located at the right side of the windows using > button.
Now click on the Finish button and it will show the next screen (Figure 11):
Figure 11
Once report file is added, you can see Field Explorer on the left side near server explorer.
Expand Database Fields, under that you will be able to find Datatable that we have created earlier. Just expand it and drag one by one filed from Field Explorer to the rpt file under detail section.
Now the report design part is over. Now we have to fetch the data from database and bind it to dataset and then bind that dataset to the report viewer.
Let's go step by step.
First Drag a CrystalReportViewer control on aspx page from tool box as per below screen:
Figure 12
Now we will fetch the data, pass data to the dataset and then add that dataset to the Crystal Report. Below is the C# code which will do the job:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Below is the final code for reports:

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument rptDoc = new ReportDocument();
    dsSample ds = new dsSample(); // .xsd file name
    DataTable dt = new DataTable();

    // Just set the name of data table
    dt.TableName = "Crystal Report Example";
    dt = getAllOrders(); //This function is located below this function
    ds.Tables[0].Merge(dt);

    // Your .rpt file path will be below
    rptDoc.Load(Server.MapPath("../Reports/SimpleReports.rpt"));

    //set dataset to the report viewer.
    rptDoc.SetDataSource(ds);
    CrystalReportViewer1.ReportSource = rptDoc;
}

public DataTable getAllOrders()
{
    //Connection string replace 'databaseservername' with your db server name
    string     sqlCon = "User ID=sa;PWD=sa; server=databaseservername;INITIAL CATALOG=SampleDB;" +
                        "PERSISTSECURITY INFO=FALSE;Connect Timeout=0";
    SqlConnection Con = new SqlConnection(sqlCon);
    SqlCommand    cmd = new SqlCommand();
    DataSet ds = null;
    SqlDataAdapter adapter;
    try
    {
        Con.Open();
        //Stored procedure calling. It is already in sample db.
        cmd.CommandText = "getAllOrders";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = Con;
        ds = new DataSet();
        adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ds, "Users");
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        cmd.Dispose();
        if (Con.State != ConnectionState.Closed)
            Con.Close();
    }
    return ds.Tables[0];
}
Now just save everything and run report. It will look like the below figure:
Figure 13
Grouping in Crystal Report
Here we will see only report design and rest of the things, you can refer from Section 1. Here we will group Customer,Product, Order and Quantity. For details, just go through Figure 14.
Just add a report (.rpt) file to the solution. Select the appropriate dataset from popup window. Once it's done, then select grouping option like figure 14.
Figure 14
Now, right click on the report, select Report -> Group Experts and the resulting window will look like figure 15:
Figure 15
Now we want to group like Customer Name and Product name so first add Customer to the right Panel. Then move product name to the right panel like figure 16:
Figure 16
This time Crystal report design will be different than the previous time. See figure 17.
GroupHeaderSection1 and GroupHeaderSection2 are added to the report designer. Here Group #1 Name refers to Customer Name and Group #2 Name refers to Product Name.
And also GroupFooterSection1 and GroupFooterSection2 are added below if you want to add something to group footer.
Figure 17
Now under every group, we want to show the number of orders per customer and productwise, so for that, we have to add summary to the GroupFooterSection2. Refer to Figure 18.
Figure 18
Right Click on the GroupFooterSection select Insert -> Summary. It will show you the next screen (Figure 19). And I have also added Order_ID and Product_Qty field to the detail (section 3) part.
Figure 19
In summary window, select the column which you want to summarize in the first dropdown.
Select Sum (First option) from the calculate drop down.
Summary Location is already set to the report footer. So just click ok to place that summary field to the report.
By default, Summary field is added to the Report Footer section so move it to the groupFooterSection2 if you want to group product wise, move it to the GroupFooterSection1 if you want to group Customer wise or keep it at original place if you want to sum all ordered products. I have moved to the FooterSection1 so it will show Customer Wise Total Quantity. Refer to Figure 20.
Figure 20
Now save the report and run it finally. It looks like figure 21.
Figure 21
Chart in Crystal Report
Chart is the most important and visible part of the reporting tool. Crystal has very powerful feature to add chart in report. Let's see how to add chart in CR. Here also, we will see only designing of the chart for other thing. Please refer to Section 1.
Here we will show customer wise product ordered quantity in chart. X portion will display Customer name and Y portion will display customers total ordered quantity.
First add charts to the report design.
Right click on the .rpt file and select Insert->Chart. Refer to figure 22.
Figure 22
Once you add chart to the report, it will not show chart on the report file but with mouse pointer you can see one blank rectangle is moving. So just click on the Report header. It will open popup for chart style and other options. Refer to figure 23.
Figure 23
Now from type tab, select type of the charts like bar chart, line chart, pie chart, etc. form the left side. Select sub type from the right pane like side by side chart, percentage bar chart, etc. I am not going into the detail of it. I am leaving it for you to practice work.
And also select vertical or horizontal radio button from the below section if you want to change the chart style vertically or horizontally. Check Use depth effect check box if you need shadow effect on the graph. Refer to figure 23.
Figure 24
As per figure 24, move to the next tab data. There are three boxes, available fields, on change of and show values. So move Customer Name from available fields to on changes of box, and move Product Quantity filed to the show value box and click ok.
Now you can see chart is added to the report header section as per figure 25.
Figure 25
Now, just save the report and run it. You can see a Report as a chart on the screen.
Report Inside Report (Sub Report)
Crystal reports provide reports inside report feature which are normally known as a subreport feature.
Let me explain it in detail. Here also, we will design only sub report design. For rest of the things, refer to Section 1.
Add new report to the solution. Then add Report->Group and select only Customer name because we want to design report for each customer and sub report product wise. So there will be only one group header inside theCustomergroup header as per figure 26.
Figure 26
Now right click on Detail section and select Insert->Subreport. Refer to figure 27.
Figure 27
Once we add subreport, it will show screen like figure 28.
Figure 28
As per figure 28, by default, choose a Crystal Report in project is selected if you want to add report from the project, then otherwise select create a subreport with the report wizard. Once we select create a subreport with Report Wizard (3rd radio button), we have to click on the Report Wizard button to select report type and data source just do as Part - 1 first. Then click on ok button so like chart report it will show a moving rectangle around mouse, click on the detail section where you want to show subreport.
Now to edit the sub report, refer to figure 29.
Figure 29
Click on the edit subreport option and format the report as per your need. Here I will suggest add product name and product quantity or you can add chart also for sub report. When you click on the subreport button, it will open subreport designer, actually CR will create a separate .rpt file but it will remain hidden inside the main .rpt file so we can't see it..
Now run the report and you can see the result, report inside report like figure 30.
Figure 30
Here number 1 is the main report and number 2 is the subreport it's showing title as Product wise.
Cross Tab Report in Crystal Report
First, let me make it clear as to what is a Cross tab report. Normally, we generate report row wise like first we show customer name, then product wise, etc. But suppose we want to see report as column wise like product name should be displayed as column in report, then cross tab report comes into the picture. See result of Cross Tab report in figure 31.
Figure 31
Here also, I will show how to design cross tab report only, for rest of the things, refer to Section 1.
First add .rpt file to the solution. Then add cross report to the Report Header section as per the below figure (Refer to figure 32).
Remember we can add cross tab report only in Report header or report footer section.
Figure 32
Once we click on cross tab report options, it will show moving rectangle around mouse pointer just place it to the report header section.
As we click on header section, it will lead to the figure 33.
Figure 33
As per the figure, move Customer name field to the Rows section, Product name we want to show as a Column so move it to the Columns fields, and we want to show product total so move it to the summarized fields. That's it. Just run the report and you can see the output as shown in figure 31.
It's as simple as that. If you have any queries regarding any type of the above mentioned reports, please let me know by way of comments. I will try my level best to fulfill your request.
Let's enjoy reporting!

Monday, 13 May 2013

SQL SERVER – Collate – Case Sensitive SQL Query Search


Case Sensitive SQL Query Search
If Column1 of Table1 has following values ‘CaseSearch, casesearch, CASESEARCH, CaSeSeArCh’, following statement will return you all the four records.
SELECT Column1FROM Table1WHERE Column1 'casesearch'
To make the query case sensitive and retrieve only one record (“casesearch”) from above query, the collation of the query needs to be changed as follows.

SELECT 
Column1FROM Table1WHERE Column1 COLLATE Latin1_General_CS_AS 'casesearch'
Adding COLLATE Latin1_General_CS_AS makes the search case sensitive.
Default Collation of the SQL Server installation SQL_Latin1_General_CP1_CI_AS is not case sensitive.
To change the collation of the any column for any table permanently run following query.

ALTER TABLE 
Table1ALTER COLUMN Column1 VARCHAR(20)COLLATE Latin1_General_CS_AS
To know the collation of the column for any table run following Stored Procedure.

EXEC 
sp_help DatabaseName
Second results set above script will return you collation of database DatabaseName.

Wednesday, 11 July 2012

Introduction to html


Welcome to HTML...
This is Primer #1 in a series of seven that will calmly introduce you to the very basics of HyperText Mark-up Language. I suggest you take the Primers one at a time over seven days. By the end of the week, you'll easily know enough to create your own HTML home page. No really. You will.
I say that because many people scoff at the notion that they can actually learn this new Internet format. I'm still amazed that the best-selling line of computer books calls its readers "Dummies." And people seem to revel in that title. Some of the smartest people I know love to proclaim themselves "Dummies" regarding every aspect of computers. Strange. I think you'll do a whole lot better at your next cocktail party by handing out your home page address rather than laughing about how dumb you are about the Internet.
You Can Do This!

Let's Get Started
I am assuming at the beginning of this tutorial that you know nothing about HTML. I am assuming, however, some computer knowledge. You wouldn't be looking at this page without having some knowledge. To continue with these Primers, you will need...
1. A computer (obviously)
2. A browser like Mozilla FirefoxNetscape NavigatorMicrosoft Internet Explorer, or Opera. If you're looking at this page, you already have one. If you look up at the title bar at the very top of your screen it will probably say the page title ("Basic HTML: Introduction") and then your browser's name.
3. A word processor. If you have access to Windows "Notepad" or "WordPad" programs or the MAC "Simple Text" program, use that to get started.
If you have those three things, you can write HTML with the best of them. Now here are a few questions you probably have:
Q. I have a MAC (or PC) -- will this work on my computer?
A. Yes. HTML does not use any specific platform. It works with simple text. More on that in a moment...
Q. Must I be logged onto the Internet to do this? More specifically, will learning this throw my cost for on-line way up?
A. Neither. You will write off-line.
Q. Do I need some sort of expensive program to help me write this?
A. No. You will write using just what I outlined above. You can buy those programs if you'd like, but they're not needed. I've never used one.
Q. Is this going to require I learn a whole new computer language like Basic or Fortran or some other cryptic, God-awful, silly-lookin', gothic extreme gobbledygook?
A. Touchy-touchy, aren't we? "No" is the answer. HTML is not a computer language. Allow me to repeat that in bold... HTML is not a computer language!

What is HTML?
H-T-M-L are initials that stand for HyperText Markup Language (computer people love initials and acronyms -- you'll be talking acronyms ASAP). Let me break it down for you:
  • Hyper is the opposite of linear. It used to be that computer programs had to move in a linear fashion. This before this, this before this, and so on. HTML does not hold to that pattern and allows the person viewing the World Wide Web page to go anywhere, any time they want.
  • Text is what you will use. Real, honest to goodness English letters.
  • Mark up is what you will do. You will write in plain English and then mark up what you wrote. More to come on that in the next Primer.
  • Language because they needed something that started with "L" to finish HTML and Hypertext Markup Louie didn't flow correctly. Because it's a language, really -- but the language is plain English.

    Beginning to Write
    You will actually begin to write HTML starting with Primer #2. That's tomorrow if you follow the seven-day plan this was written for. Here, I want to tell you how you will go about the process.
    You will write the HTML document on the word processor, or Notepad, WordPad, or Simple Text. When you are finished creating the HTML document, you'll then open the document in a browser, like Netscape Navigator. The browser will interpret the HTML commands for you and display the Web page.
    Now, some people who are already schooled in HTML are going to jump up and down and yell that you should be using an HTML assistant program because it makes it easier. That's true, but it also makes it harder to learn as the program does half the work for you. Take my word for it, use the word processor for a week, then go to the assistant if you still want to use one. You'll be far better off for the effort. I have been writing HTML for six years and I still use Notepad for the bulk of my writing.

    Let's get into the programs you will use to write your HTML document. Keep this in mind: HTML documents must be text only. When you save an HTML document, you must save only the text, nothing else.
    The reason I am pushing NotePad, WordPad, and Simple Text is that they save in text-only format without your doing any additional work. They just do it. But, if you're like me, then you will want to start writing on a word processor, like WORD, or WordPerfect. Maybe you're just more comfortable on it. If so, read this next part carefully.
    The Word Processor
    When you write to the word processor you will need to follow a few steps:
    1. Write the page as you would any other document.
    2. When you go to save the document (Here's the trick), ALWAYS chooseSAVE AS.
    3. When the SAVE AS box pops up, you will need to save the page in a specific format. Look at the SAVE AS dialogue box when it pops up: Usually at the bottom, you find where you will be able to change the file format.
    4. If you have a PC, save your document as ASCII TEXT DOS or just TEXT. Either one will work.
    5. If you have a MAC, save your document as TEXT.
    6. When I started writing HTML, I saved pages by assigning every Web page its own floppy disc. It just helped me keep it all straight, but if you want to save right to your hard drive, do it. I only offer the floppy disc premise as a suggestion.
    Please remember: It is very important to choose SAVE AS EVERY time you save your document. If you don't, the program won't save as TEXT, but rather in its default format. In layman's terms -- use SAVE AS or screw up your document.
    You see, when you save your document in WORD, or some other word processor format other than text, you are saving much more than just the letters on the page. You're saving the margin settings, the tab settings, specific fonts, and a whole lot of other settings the page needs to be displayed correctly. You don't want all of that. You just want the text.
    NotePad, WordPad, and SimpleText already save in text-only format so if you use one of them as your word processor, you'll get the correct format simply by saving your document.

    How To Name Your Document
    What you name your document is very important. You must first give your document a name and then add a suffix to it. That's the way everything works in HTML. You give a name and then a suffix.
    Follow this format to name your document:
    1. Choose a name. Anything. If you have a PC not running Windows 95, you are limited to eight letters, however.
    2. Add a suffix. For all HTML documents, you will add either ".htm" or ".html".
    (".htm" for PCs running Windows 3.x and ".html" for MAC and Windows 95/98 Machines)
    Example:
    I am looking to name a document I just wrote on a PC running Windows 3.11 for workgroups. I want to name the document "fred". Thus the document must be named "fred.htm". If it was MAC or Windows 95/98 I would name it "fred.html". Please notice the dot (period) before .htm and .html. And no quotation marks, I just put them in here to set the name apart.
    Uhhhhhh.... Why Do I Do That?
    Glad you asked. It's a thing called "association." It's how computers tell different file types apart. ".html" tells the computer that this file is an HTML document. When we get into graphics, you'll see a different suffix. All files used on the Web will follow the format of "name.suffix." Always.
    Okay, why .htm for PCs running Windows 3.x and .html for MAC and Windows 95/98?
    Because that's the way the operating systems are made (Windows 3.x, Windows 95/98, and MAC OS are all technically called operating systems). Windows 3.x only allows three letters after the dot. MAC OS and Windows 95/98 allow four, or more. Your browser allows for both suffixes. It acts upon .html and .htm in the same fashion.

    Why do you keep harping on the fact that I must save in TEXT only?
    You're just full of questions! You see, HTML browsers can only read text. Look at your keyboard. See the letters and numbers and little signs like % and @ and *? There are 128 in all (read upper- and lowercase letters as two). That's text. That's what the browser reads. It simply doesn't understand anything else.
    If you'd like to test this theory, then go ahead and create an HTML document and save it in WORD. Then try and open it in your browser. Nothing will happen. Go ahead and try it. You won't hurt anything.
    Remember that if you are using Notepad, Wordpad, or Simple Text, the document will be saved as text with no extra prompting. Just choose SAVE.

    Opening the Document in the Browser
    Once you have your HTML document on the floppy disc or your hard drive, you'll need to open it up in the browser. It's easy enough. Since you're using a browser to look at this Primer, follow along.
    1. Under the FILE menu at the very top left of this screen, you'll find OPEN, OPEN FILE, OPEN DOCUMENT, or words to that effect.
    2. Click on it. Some browsers give you the dialogue box that allows you to find your document right away. Internet Explorer, and later versions of Netscape Navigator, require you to click on a BROWSE button or OPEN FILE button to get the dialogue box. When the dialogue box opens up, switch to the A:\ drive (or the floppy disc for MAC users) and open your document. If you saved the file to your hard drive, get it from there.
    3. You might have to then click an OK button. The browser will do the rest.

    One More Thing
    You easily have enough to keep you occupied for the first day. Don't worry, the Primers get less wordy after this.
    If you are going to start writing HTML, I suggest you make a point of learning to look at other authors' HTML pages. You say you're already doing that, right? Maybe. What I mean is for you to look at the HTML document a person wrote to present the page you are looking at. Don't look at the pretty page, look behind it at the HTML document.
    Why Would I Do That?
    Because you can... but seriously, folks. Let's say you run into a page that has a really neat layout, or a fancy text pattern, or a strange grouping of pictures. You'd like to know how to do it.
    Well, look, I'm not telling you to steal anything, but let's be honest, if you see some landscaping you like, you're going to use the idea. If you see a room layout you like, you will use the idea to help yourself. That's the point of looking at another page's HTML document. I think it's also the best way to learn HTML. In fact, I am self-taught in HTML simply by looking at others' documents. It was the long way around, believe me. You're going to have a much easier time of it with these Primers.
    Here's how you look at an HTML document (known as the "source code"):
    1. When you find a page you like, click on VIEW at the top of the screen.
    2. Choose DOCUMENT SOURCE from the menu. Sometimes it only reads SOURCE.
    3. The HTML document will appear on the screen.
    4. Go ahead. Try it with this page. Click on VIEW and then choose the SOURCE.
    It's going to look like chicken-scratch right now, but by the end of the week, it'll be readable and you'll be able to find exactly how a certain HTML presentation was performed.
    It's A Little Different On AOL
    Those of you who use AOL can also see the source. You can do it by placing your pointer on the page, off of an image, and clicking the right mouse button. MAC users should click and hold. A small menu should pop up. One of the items will allow you the ability to view the source.
    That's the Primer for today. Print it out if you'd like and get ready to delve in and write your first HTML document. See you tomorrow.
  •  
    Twitter Bird Gadget