Sunday, August 18, 2013

Audio player (Embed audio) in asp.net

 <object id="AudioPlayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"
                            >
                            <param name="AutoStart" value="True" />
                            <param name="URL" value="<%=filePath %>" />
                            <param name="uiMode" value="full" />
                            <param name="volume" value="100" />
                            <!-- Turn off the controls -->
                            <embed autostart="1" height="100%" name="AudioPlayer" showcontrols="1" src='<%=filePath %>'
                                type="application/x-mplayer2" volume="10" width="100%"></embed>
                        </object>

Months using dropdown list in asp.net

DateTime month = DateTime.Parse("1/1/1990");
        for (i = 0; i < 12; i++)
        {
            DateTime nmonth = month.AddMonths(i);
            string dmonth = nmonth.ToString("MMMM");
            DropDownList1.Items.Insert(i, dmonth);
        }



OR


 string str = DateTime.Now.ToShortDateString();
        DateTime cmonth = DateTime.Parse(str);
        string mon = cmonth.ToString("MMMM");
        if (DropDownList1.SelectedItem.Text != mon)
        {
            DropDownList1.ClearSelection();
            DropDownList1.Items.FindByText(mon).Selected = true;
        }

Custom Validator in ASP.NET

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.ToString()=="hai")
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;

        }
    }

Crystal Report in Asp.Net


Steps

1.Create a new website and right click on solution explorer > add new Item > Select Crystal Report
In the dialog box choose blank report



2.Now click on CrystalReports Menu in VS and select DataBase Expert


3.In database expert dialog box expend create new connection > OLEDB(ADO) section

4.Now select SQL Native client and enter you SQL server address , username , password and pick database name from the dropdown.

5.In next screen Expend your database objects in left pane and add the tables you want to use in right pane

6.Link your tables based on Primary keys (If any)

7.Click ok to finish the wizard.
Right click on Field Explorer and select Group Name Fields  > Insert Group

8.In next box select the field you to report to be grouped (in my case it's ProjectsName)

9.Click on OK to finish
Now design the report , drag and fields from Database fields in field explorer and which you want to show in report and drop them in Section3(Details), and preview the report,

10.Go to default.aspx page and drag and drop CrystalReportViewer from the toolbox, click on smart tag and choose new report source.

11.Choose you report from the dropdown menu and click ok to finish.
Now when you build and run the sample , it asks for the database password everytime.

12.To fix this we need to load the report programmatically and provide username and password from code behind .
Now run the report , it should look like this

13. the code is

protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
        crystalReport.SetDatabaseLogon
            ("amit", "password", @"AMIT\SQLEXPRESS", "TestDB");
        CrystalReportViewer1.ReportSource = crystalReport;
    }



Html



<form id="form1" runat="server">
<div>
  <CR:CrystalReportViewer ID="CrystalReportViewer1"
                          runat="server" AutoDataBind="True"
                          Height="1039px"
                          ReportSourceID="CrystalReportSource1"
                          Width="901px" />
  <CR:CrystalReportSource ID="CrystalReportSource1"
                          runat="server">
            <Report FileName="CrystalReport.rpt">
            </Report>
   </CR:CrystalReportSource>
   
    </div>
    </form>