Tuesday, September 22, 2009

Crystal Report Example in asp.net

This code shows how to create Reports using ASP.NET Crystal Reports


Imports System
Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\vb.net projects\dept_mstr.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = ConnString

Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim sql As String = "select distinct dept_no,dept_name from DEPARTMENT"
myConnection.Open()

Dim com As New OleDbCommand
Dim dbread
com = New OleDbCommand(sql, myConnection)
dbread = com.ExecuteReader
While dbread.read
DropDownList1.Items.Add(dbread.getVALUE(0))
DropDownList2.Items.Add(dbread.getstring(1))
End While

End Sub

Protected Sub CrystalReportViewer1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Init

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Fltr
CrystalReportViewer1.ReportSource = "E:\WebSite2\crystalreport1.rpt"
Fltr = "{DEPARTMENT.dept_no}='" & Me.DropDownList1.Text & "' and '" & Me.DropDownList2.Text & "'"
CrystalReportViewer1.SelectionFormula = (Fltr)
End Sub
End Class

Use of Culture in asp.net

This sample code shows use of culture in asp.net


Design:

<form id="form1" runat="server">
<div>
Cultures: <asp:DropDownList id="ddlCulture" DataTextField="DisplayName"
DataValueField="Name" DataSourceID="GetCultureInfo" Runat="server" />
<asp:ObjectDataSource id="GetCultureInfo" TypeName="System.Globalization.CultureInfo"
SelectMethod="GetCultures" Runat="server">
<SelectParameters>
<asp:Parameter Name="types" DefaultValue="SpecificCultures" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Button id="btnSelect" Text="Select" Runat="server" onclick="btnSelect_Click"></asp:Button><br />
Date:<asp:Label id="lblDate" Runat="server" /><br />
Price : <asp:Label id="lblPrice" Runat="server" />
</div>
</form>



Web.Config:

<profile>
<properties>
<add
name="UserCulture"
defaultValue="en-US" />
<add
name="UserUICulture"
defaultValue="en" />
</properties>
</profile>


Code Behind:

protected override void InitializeCulture()
{
Culture = Profile.UserCulture;
UICulture = Profile.UserUICulture;
}

protected void btnSelect_Click(object sender, EventArgs e)
{
Profile.UserCulture = ddlCulture.SelectedValue;
Profile.UserUICulture = ddlCulture.SelectedValue;
//Response.Redirect(Request.Path);
}

void Page_PreRender()
{
lblDate.Text = DateTime.Now.ToString("D");
lblPrice.Text = (512.33m).ToString("c");
}