Wednesday, 20 June 2012

rdlc report in asp.net

 How to create RDLC report in asp.net

Step 1:  Create Database

Step 2: Create Asp.net Web Application
Step 3: Add Microsoft Report Viewer in Asp.net Web Application
<%@ Page Language="C#" MasterPageFile=".Master" AutoEventWireup="true" CodeBehind=".aspx.cs" Inherits=".WebForm4" Title="Untitled Page" %>
<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="#333333" 
        Font-Size="8pt" Height="315px" 
        ShowCredentialPrompts="False" ShowDocumentMapButton="False" 
        ShowFindControls="False" ShowPageNavigationControls="False" 
        ShowParameterPrompts="False" ShowPromptAreaButton="False" 
        ShowRefreshButton="False" ShowZoomControl="False" SizeToReportContent="True" 
        Width="710px">
        <LocalReport ReportPath="Customer\Order_History.rdlc">
            <DataSources>
                <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" 
                    Name="DataSet2_order_history" />
            </DataSources>
        </LocalReport>
    </rsweb:ReportViewer>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetData" 
        TypeName="Online Gas Agency System.DataSet2TableAdapters." 
        onselecting="ObjectDataSource1_Selecting">
    </asp:ObjectDataSource>
</asp:Content>
Step 4: Add Report .rdlc  crystal report
Step 5: Add Data Set
 Step 6: Add table from toolbox in report
 Step 7: Design Report as per requirement
Step 8: Code Behind
SqlConnection con =newSqlConnection(@"DataSource=.\sqlexpress;Initial Catalog=DB;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("Select col1,col2,... from table_name", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "DataSet2_order_history";
            rds.Value = dt;         
           ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();

Saturday, 26 May 2012

displaying data in detailsview

       There is difference between grid view and details view where details view represents only single data.
e.g. If roll number of student to be entered in text box, detail view should display all related information about that student only.
        Here I am sharing that, how to display specific record from database to details view. Hope it would be helpful for you.
        Make an adjustment as per your database connection..


1) Designer Code:

 <asp:Label ID="Label1" runat="server" Text="Enter Merit No"></asp:Label>

    <asp:TextBox ID="txtMerit" runat="server" Width="71px"></asp:TextBox>
   

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK"
        Width="65px" Font-Bold="True" />
 

    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
        CellPadding="4" ForeColor="Black" BackColor="#CCCCCC"
    BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellSpacing="2">

        <FooterStyle BackColor="#CCCCCC" />
        <RowStyle BackColor="White" />
        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

    </asp:DetailsView>

2) On Display Button Code:

protected void btnDisplayData_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
            Data Source=StudentDatabase1.mdb;Persist Security Info=True;");

            string strSql = "Select * From [student_merit_record] Where [merit no]='" + txtMerit.Text + "'";
            OleDbCommand cmd = new OleDbCommand(strSql);
            cmd.CommandText = strSql;
            cmd.Connection = con;
            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds, "student_merit_record");
            DetailsView1.Visible = true;
            DetailsView1.DataSource = ds.Tables["student_merit_record"];
            DetailsView1.DataBind();
            con.Close();
        }