Monday, May 26, 2008

Crystal Report Programming

Hi friends ,

Here Some work with crystal report programming


· To total a report field with a formula field

EvaluateAfter ({@DrOpBalance});
{@DrOpBalance} + Sum ({Debit});

Where @DrOpBalance is a formula field and Debit is a report field.

· Conditional statement

1)

EvaluateAfter ({@CrSideTotal});
If {@DrSideTotal} >= {@CrSideTotal} Then
{@DrSideTotal} - {@CrSideTotal}
Else
0.00;

2)

WhilePrintingRecords;
NumberVar DrTotal;

If {XMLRBSheet.AMOUNT} > 0 Then
DrTotal := DrTotal + {XMLRBSheet.AMOUNT};

If {XMLRBSheet.AMOUNT} > 0 Then
{XMLRBSheet.AMOUNT}
Else
0.00;

· Declaring variables

NumberVar DrTotal;

· Variable assignment

Dabit := 1000

· Accessing final/latest value of a variable

EvaluateAfter ({@ClStock});
NumberVar CrTotal;
NumberVar CurrentRecord;

CurrentRecord := RecordNumber();
CrTotal := CrTotal + {@ClStock};

Practical Approach

Visual Basic Dot Net – Practical Approach


· To start a application in a new thread

System.Windows.Forms.Application.Run(New FrmMainMDI)

· Database Handling

1) Opening a new connection

Try
Dim CnString As String
Dim ActiveDBConnection = New SqlClient.SqlConnection

CnString = “Put your connection string here”

ActiveDBConnection.ConnectionString = CnString
ActiveDBConnection.Open()

Catch e As SqlClient.SqlException

ActiveDBConnection = Nothing

End Try

2) Executing a non select query

Dim RowAffected As Integer
Dim TSQLString As String
Dim DBCommand As SqlClient.SqlCommand

DBCommand = New SqlClient.SqlCommand(TSQLString, _
ActiveDBConnection)

RowAffected = DBCommand.ExecuteNonQuery()

If RowAffected = 0 Then
Return False
End If

3) Executing a select query for DataSet

Dim DBCommand As SqlClient.SqlCommand = New _
SqlClient.SqlCommand(TSQLString)

Dim DBDataAd As SqlClient.SqlDataAdapter = New _
SqlClient.SqlDataAdapter
Dim TempDS As New DataSet

DBCommand.Connection = ActiveDBConnection
DBDataAd.SelectCommand = DBCommand
DBDataAd.Fill(TempDS)
Return TempDS

· Getting value of a particular field in a DataSet

DataSet.Tables(0).Rows(“Row Index”)(“Column Name/Index”)

· Getting value of a particular field in a DataRow

DataRow.Item(“Column Name/Index”)

· Function to check whether a MDI Child is active or not

Private Function CheckActiveMDIChild(ByVal MDIChildName As
String) As Boolean
Dim X As Short
X = 0
Do While X <= Me.MdiChildren.Length - 1
If Me.MdiChildren(X).Name = MDIChildName Then
Return True
Exit Do
End If
X = X + 1
Loop
Return False
End Function

· Creating a new form object and assigning it to MDI form

Dim TempFrmProduct As New FrmProduct
TempFrmProduct.MdiParent = Me
TempFrmProduct.Show()
‘ Or to show form as dialog (Does not work in case
of a MDI child)

TempFrmProduct.ShowDialog()

· Checking data type of a column in a DataSet

If DataSet.Tables(0).Columns("Name/Index").DataType.FullName =
_ "System.Decimal"
End If

· Reading a text file

Dim ConnectionStringText As String
Dim FS As New System.IO.FileStream(FileName, _
IO.FileMode.Open, IO.FileAccess.Read)

Dim R As New System.IO.StreamReader(FS)

ConnectionStringText = R.ReadLine
R.Close()

· Handling Crystal Reports

First add the crystal report (.rpt) file to project. Visual Basic automatically creates the .VB file for that particular report. This file is basically contains the class for the report added. We can use this class to call the particular report (By making a object of the class).

For example you have a report named “RBalanceSheet”. Now do the following to call the report through code.

Dim TReportDocument As New RBalanceSheet

‘ Passing values to formula fields defined in the
‘ report

TReportDocument.DataDefinition.FormulaFields.Item("OpeningText").Text = "’To Opening Balance’


‘ Setting DataSet (Data source) for the report
TReportDocument.Database.Tables.Item("XMLRBalanceSheet").SetDataSource(ReportDS.Tables(0))

‘ Calling the report through Crystal Report Viewer

CrystalReportViewer1.ReportSource = TReportDocument

· Handling DataGrid KeyPress and mouse click events

Add following event handler on form load event:

AddHandler ProductColumnStyle.TextBox.KeyPress, AddressOf
ProductColumn_KeyPress

AddHandler ProductColumnStyle.TextBox.DoubleClick, AddressOf ProductColumn_DoubleClick

Now add following event procedure:

Private Sub ProductColumn_KeyPress(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs)

If Asc(e.KeyChar) = Keys.Space Then

‘ Write code here

End If
End Sub

Private Sub ProductColumn_DoubleClick(ByVal sender As
Object, ByVal e As System.EventArgs)

‘ Write code here

End Sub

· Handling Stored Procedures

1) Filling dataset using stored procedure:

Dim TempDS = New DataSet

MyDbCommand = New SqlCommand("sp_Select", MyConnection)
MyDbCommand.CommandType = CommandType.StoredProcedure

MyParamTempID = New SqlParameter("@TempID", “1”)
MyDbCommand.Parameters.Add(MyParamTempID)

MyDbAdapter.SelectCommand = MyDbCommand
MyDbAdapter.Fill(TempDS)

2) Using output parameter of the stored procedure:

Dim MyDbAdpater As New SqlDataAdapter
Dim MyDbCommand As New SqlCommand("sp_OutputValue", MyConnection)

MyDbCommand.CommandType = CommandType.StoredProcedure
Dim TempDs = New DataSet

Dim MyParamTempID1 As New SqlParameter("@TempID1", “1”) Dim MyParamTempID2 As New SqlParameter("@TempID2", “2”) Dim MyParamNameAdd As New SqlParameter("@NameAdd", SqlDbType.VarChar, 50)
MyParamNameAdd.Direction = ParameterDirection.Output

MyDbCommand.Parameters.Add(MyParamTempID1)
MyDbCommand.Parameters.Add(MyParamTempID2)
MyDbCommand.Parameters.Add(MyParamNameAdd)

MyDbCommand.ExecuteScalar()

MsgBox(MyParamNameAdd.Value)

3) Inserting values through stored procedure:

Dim MyDbCommand As New SqlCommand("sp_Insert", MyConnection)
Dim MyParamName As New SqlParameter("@Name", “John”)

MyDbCommand.CommandType = CommandType.StoredProcedure

MyDbCommand.Parameters.Add(MyParamName)
MyDbCommand.ExecuteNonQuery()

4) Using Parameterized query:

Dim TempDOB As Date
TempDOB = "01/05/1980"
Dim MyDBCommand As New SqlCommand("Insert InTo Personal (Name, Age, DOB) Values (@Name, @Age, @DOB)", MyConnection)

Dim MyParamName As New SqlParameter("@Name", “John”)
Dim MyParamAge As New SqlParameter("@Age", 25)
Dim MyParamDOB As New SqlParameter("@DOB", TempDOB)

MyDBCommand.Parameters.Add(MyParamName)
MyDBCommand.Parameters.Add(MyParamAge)
MyDBCommand.Parameters.Add(MyParamDOB)

MyDBCommand.ExecuteNonQuery()

· Binding DataGrid To Dataset

MyDBAdapter.Fill(MyTempDS, "MyTable")

DataGrid1.DataSource = MyTempDS.Tables(“MyTable”)
DataGrid1.DataBindings()

· Binding Combo Box To Dataset

MyDBAdapter.Fill(MyTempDS, "MyTable")
ComboBox1.DataSource = MyTempDS
ComboBox1.DisplayMember = "MyTable.Name"

Monday, May 05, 2008

Some Interview Stuff

· Windows DNA was a programming model or blueprint that companies could use when designing n-tier distributed component-based application for the windows platform.

· Shadow Copy

This is a feature of CLR. When you update a component of ASP .Net and overwrite the old version of the component by the new one into the bin directory or in GAC, ASP .Net detected the changes and automatically loads the new component and use them to process all new web requests not currently executing, while as the same time keeping the older versions of the components loaded until previously active requests are completed.

· The .Net code you write today will also work under 64-bit versions of windows without change.

· The CLR is a runtime for all .Net languages. It is responsible for executing and managing all code written in any language.

· Code Verification is a process that ensures all code is safe to run prior to execution.

· The code produced for an application designed to run under the CLR is called managed code -self-describing code that makes use of and requires the CLR to be present. For managed code, the CLR will: always locate the metadata associated with a method at any point in time, walk the stack, handle exceptions, store and retrieve security information, manages memory and garbage collects.

· Namespaces have two key functions:

1) They logically group related types.
2) They make name collision less likely.

· We can use/declare any native CLR data types in any language, Even though that language does not support that type (does not have a mapping name for the type).

Value types in the CLR are defined as types that derive from System.ValueType. Value types are not instantiated using New (unless you have a parameterized constructor), and go out of scope when the function they are defined within returns.

· In case of boxing/un-boxing the value contained in the type and the created reference types are not associated in any way. If you change the original value type, the reference type is not affected. Boxing (Object = Value Type) occurs implicitly by the compiler/CLR, but you have to un-box (Value Type = Object) explicitly. In .Net structures are always value types.

· MSCoreLib.Dll is the mail CLR system assembly, which contains the core classes for the built-in CLR types, etc.

· Attributes are a feature of CLR compilers that enables you to annotate types with additional metadata. The CLR or other tools can than use this metadata for different purpose, such as containing documentation, information about COM+ service configuration, and so on.

· All windows applications run inside a process. Processes own many resources and threads execute code loaded into a process. If one application runs in its own process and we have multiple applications of same type (i.e. large number of web sites running on IIS) running in their own processes, they consume lots of resources. If we have multiple application are running on same process, although we can save much resources, but when an application crashes it can destroy entire process and all other applications. Application Domains in .Net have same benefits as a process, but multiple application domains can run within the same process. Application domains can be implemented safely within the same process, because the code verification feature of the CLR ensures that the code is safe to run. All the code execution in the application domain is managed by CLR, so the crashing of an application is not typically possible, and when it happens it does not affect the other applications running on other application domains.

· The concept of locating and consuming programmatic functions over the Internet is called web services. Web services are based on an application of XML called SOAP. SOAP defines a standardized format for enveloping the XML payloads exchanged between two entities over standard protocols such as HTTP. The consumers of a web service are completely shielded from any implementation details about the platform exposing the web service - they simply send and receive XML over HTTP. This means that any web service on a windows platform can be consumed by any other platform, such as UNIX.

· You can find information about any name-space, class, interface or any other type in .Net using a utility called WinCV (Windows Class Viewer).

· ASP .Net Page Compilation

Request For A Page >> Parsing Of The Page Into A DLL By Page Compiler >> DLL With Compiled Class Places Into A Temporary Directory >> Render The Page Using The Compiled Class.

The process of compilation of the page occurs only once for a .Aspx page. Every time a request is occurs for the same page, that request is satisfied by instantiating the class generated.

· ASP .Net is implemented as an HTTP handler. Different ASP .Net services (not web services) like state management, security and others, all handles by various HTTP handler/modules. Each HTTP handler is responsible for serving a specific task. To implement your own HTTP runtime handler, you can create a class that supports the HTTP runtime interface, add your extension and class to the web.config file and hence write your own web technologies. When your HTTP runtime handler is hosted in IIS, you must add your extension to the IIS configuration map.

ASP .Net uses the compilers section of the machine.config file to define the mapping of a page’s extension and available languages that can be used in ASP .Net.

· ASP .Net enables you to build the following types of server controls:

1) Custom server control: You can write the server controls in a compiled form, where you develop a class that inherits from one of the ASP .Net server controls classes.
2) User controls: You can declare other ASP .Net pages as controls, and then use those pages to build up other pages.

· Overloads keyword with methods is not required when overloading methods in the same class, but if it is used, it must be used on all overloaded methods.

· Shadows means that the method in the parent class is not available, and allows creation of methods with the different signature than that of the parent. It effectively re-declares the type.

· An interface is the description of the methods and properties a class will expose – it’s an immutable contract with the outside world.

· .Net’s CTS also responsible for cross-language functionality.

· Page Events: Page_Init >> Page_Load >> Control_Event >>
Page_Unload

· The difference between Page_Init and Page_Load is that the controls are only guaranteed to be fully loaded in the Page_Load. The controls are accessible in the Page_Init event, but the ViewState is not loaded, so controls will have their default values, rather than any values set during the postback.

· Sample ASP .Net page format:

<%@ Import Namespace=”System.Data” %>








runat=”server”/>





· Page’s IsPostBack property would be false when the page is loaded first time, and it will be true on subsequent request.

· When a client makes a request to the server for a page, the compilation process creates a class derived from System.Web.UI.Page, and includes all the components (.aspx file being requested, code file for the page, any user control used by the page). This dynamically created page class can than be instantiated to render the requested page. This compilation process is occurs only first time when any user requests the page. On any subsequent requests this compiled class can be used to render the same page again and again.

· Page class has a unique step, known as rendering step, when HTML is actually generated for the output to the client.

· When your code behind contains a class named MyClass then the .aspx page linked with the code behind file like:

<%@ PAGE Inherits=”MyClass” Code=”MyPage.Vb %”>

· To merge a HTML file with the currently rendering page HTML use following WriteFile method of the response object into Page_Load event: Response.WriteFile(“C:\Temp\Content.html”)

· The ViewState contains the state of all user controls on the page. This information is stored as name-value pairs using the Sytem.Web.UI.StateBag object. The ViewState is stored as a string variable that is passed back to the client in the page. String representing ViewState values is stored as a hidden form field. We can enable or disable the ViewState by using EnableViewState property at page or control level.

· Client makes requests to server (round-trips) through an HTTP POST.

· In processing of control events, those events are process first that being happened at client but did not cause a post-back. After that the event that caused a post-back is processed.

· Any in-line script code is run at rendering stage.
· When you have an event of a control that can be handled on either the client or the server, then the server handling of that event will take precedence. For example if a button control have both server (OnServerClick) and client (OnClick) event handler than client side event handler will be ignored.

· ViewState property is a collection of the state that is maintained by the page. This option is available through a state bag object. We can use ViewState property like:

ViewState(“MyName”) = “John” ‘ Adding value

Integer Name As String
Name = ViewState(“MyName”) ‘ Retrieving value