Pages

Wednesday, November 24, 2010

Crystal Report Example

In Aspx file
  <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" PrintMode="ActiveX" DisplayGroupTree="False"></CR:CrystalReportViewer>

In Aspx.cs  file

ReportDocument rptDocument = null;
protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            rptDocument = new ReportDocument();
            Session["rptDocument"] = rptDocument;
        }
        else
        {
            rptDocument = (ReportDocument)Session["rptDocument"];
            if (rptDocument == null)
            {
                rptDocument = new ReportDocument();
                Session["rptDocument"] = rptDocument;

            }
        }
        if (IsPostBack)
        {
            CreateReport();
        }

    }
private void CreateReport()
    {
       
rptDocument = new ReportDocument();
            string Query = string.Empty;
rptDocument.Load(Server.MapPath("report path"));
using (OdbcConnection OCN = new OdbcConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString))
            {
                OCN.Open();

                OdbcCommand ocmd = OCN.CreateCommand();
                OdbcDataAdapter sadpt = new OdbcDataAdapter(ocmd);
dsReport DSTEMP = new dsReport();
                DSTEMP.Clear();
                ocmd.CommandText = Query;
                sadpt.Fill(DSTEMP, "datatable1");
                rptDocument.SetDataSource(DSTEMP);
                rptDocument.SetParameterValue("",””);//parameter nameAnd val
                CrystalReportViewer1.ReportSource = rptDocument;
                CrystalReportViewer1.Visible = true;
            }
    }
    protected void cmdPrint_Click(object sender, EventArgs e)
    {
        rptDocument.PrintToPrinter(1, false, 0, 0);
    }



Saturday, November 20, 2010

In Aspx
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
        <Services>
            <asp:ServiceReference Path="~/WebServices/AutoComplete.asmx" />
        </Services>
    </asp:ScriptManager>
  <asp:TextBox ID="txtName" TabIndex="3" runat="server" CssClass="TextBox"
                        Width="250px" MaxLength="100"></asp:TextBox>
                    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" Enabled="true"
                        EnableCaching="true" MinimumPrefixLength="1" TargetControlID="txtName"
                        ServiceMethod="GetNames" ServicePath="~/WebServices/AutoComplete.asmx">
                    </cc1:AutoCompleteExtender>
In asmx.cs file
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Data;
[ScriptService()]

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class AutoComplete : System.Web.Services.WebService
{
    [WebMethod]

    [ScriptMethod()]
    public string[] GetNames(string prefixText, int count)
    {

        ArrayList filteredList = new ArrayList();

        MySQLConnection con = new MySQLConnection();
        string qry = "SELECT column_name FROM table_name WHERE column_name LIKE '%" + prefixText + "%' ";
        DataSet ds;
        ds = con.getDataset(qry);

        foreach (DataRow dr in ds.Tables[0].Rows)
        {            if(dr["column_name"].ToString().ToLower().StartsWith(prefixText.ToLower()))
                filteredList.Add(dr["column_name "].ToString());
        }
        con.closeConnection();
       

        return (string[])filteredList.ToArray(typeof(string));
    }

}


Saturday, October 30, 2010

Maxlength for decimal field on textbox


set maxlegth=16 for 13 digit  + 1 .(point) +  2 fraction digit
MaxLength="16" onblur="fnDecMax(this.id,this.getAttribute('MaxLength'));"

javascript function
----------------------

function fnDecMax(id,maxLength)
{

theValue = document.getElementById(id).value;
maxLength = maxLength -1;
rx = /[^0-9.]/;
if(rx.test(theValue))
{
alert("The field can only contain numbers");
return;
}
if(theValue.indexOf(".") != -1)
{
theValue = theValue.substring(0,(theValue.indexOf(".") + 3));
}
lnt = theValue.length;
if(lnt > maxLength )
{
if(theValue.indexOf(".") == -1) {
theValue = theValue.substring(0,maxLength);
}
else
{
theValue = theValue.substring(0,(maxLength+1));
}
lnt = theValue.length;
}
if(lnt > (maxLength-2) && theValue.indexOf(".") == -1)
{
first = theValue.substring(0,(maxLength-2));
second = theValue.substring((maxLength-2));
theValue = first + "." + second;
}
document.getElementById(id).value= theValue;
}
Set Cursor Location from server side

Javascript function
function setCursorPositionToEnd(elementId)
{ var elementRef = document.getElementById(elementId);
var cursorPosition = document.getElementById(elementId).value.length;
if ( elementRef != null )
{
if ( elementRef.createTextRange )
{
var textRange = elementRef.createTextRange();
textRange.move('character', cursorPosition);
textRange.select();
}
else
{
if ( elementRef.selectionStart )
{
elementRef.focus();
elementRef.setSelectionRange(cursorPosition, cursorPosition);
}
else
{
elementRef.focus();
}
}
}
}

use This code at server side
string myScript = String.Format("setCursorPositionToEnd('txtSearch');"); ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Myscript", myScript, true);
MaxLength for MultiLine textbox
onKeyUp="javascript:Count(this,5000);" onChange="javascript:Count(this,5000);"
function Count(text,long)
{
var maxlength = new Number(long); // Change number to your max length.
if(document.getElementById('txtDescription').value.length > maxlength)
{
text.value = text.value.substring(0,maxlength);
alert(" Only " + long + " chars");
}
}
Page Maximize Javascript
function maximize()
{ window.moveTo(0, 0); window.resizeTo(screen.width, screen.height-25); }

call this function on
body onload="maximize()"
Ajax Progrmming

Ajax(pronounced /'e?d?æks/) (shorthand for Asynchronous JavaScript and XML[1]) is a group of interrelated web development techniques used on the client-side to create interactive web applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. The use of Ajax techniques has led to an increase in interactive or dynamic interfaces on web pages[citation needed]. Data is usually retrieved using the XMLHttpRequest object. Despite the name, the use of XML is not actually required, nor do the requests need to be asynchronous.[2]
Like DHTML and LAMP, Ajax is not a technology in itself, but a group of technologies. Ajax uses a combination of HTML and CSS to mark up and style information. The DOM is accessed with JavaScript to dynamically display, and to allow the user to interact with, the information presented. JavaScript and the XMLHttpRequest object provide a method for exchanging data asynchronously between browser and server to avoid full page reloads.

Monday, October 11, 2010

----get Querystring Value in Javascript-------------

var tablename = getQuerystring('tablename');
function getQuerystring(key, default_)
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}

Saturday, February 13, 2010

Auto Refresh.

Method 1: Response.AddHeader

To Refresh Web page After every 5 Seconds You can add following code,

Response.AddHeader("Refresh", "5");

Cricket Sites, Stock Exchange sites use similar logic :)

Method 2: In body Tag, window.setTimeout

The 1000 = 1 Second...

<body onload="window.setTimeout('window.location.reload()',1000);">


Method 3: In Meta Tag

Theres also a meta tag that you can define on the head, but not sure wheter it refreshes even if the content has not finished loading or if it starts counting when the head section loaded. Code would be something like that:

<meta content="600" http-equiv="refresh">


Method 4: Timer Control : Microsoft ASP.NET 2.0 AJAX Extensions server control

In following example, the timer interval is set to 10 seconds


<%@ Page Language="C#" AutoEventWireup="true" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Timer Example Pagetitle>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
OriginalTime.Text = DateTime.Now.ToLongTimeString();
}

protected void Timer1_Tick(object sender, EventArgs e)
{
StockPrice.Text = GetStockPrice();
TimeOfPrice.Text = DateTime.Now.ToLongTimeString();
}

private string GetStockPrice()
{
double randomStockPrice = 50 + new Random().NextDouble();
return randomStockPrice.ToString("C");
}
script>

head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000" />
<asp:UpdatePanel ID="StockPricePanel" runat="server" UpdateMode="Conditional">
<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
triggers>
<contenttemplate>
Stock price is <asp:Label id="StockPrice" runat="server"/>
<br/>as of
<asp:Label id="TimeOfPrice" runat="server"/>
contenttemplate>
asp:UpdatePanel>
<div>
Page originally created at
<asp:Label ID="OriginalTime" runat="server">asp:Label>
div>
form>
body>
html>

Search This Blog