Wednesday, July 13, 2011

To access crystal report control in code behind

To access crystal report control in code behind

Following 3 steps to access crystal report control & change value in codebehind

1. Make an object of your crystal report.

    rptMyReport report = new rptMyReport();
2. Using object access control for which you want to do changes . cast control to TextObject

TextObject to =(TextObject) report.PageHeaderSection1.ReportObjects. ["textboxname"];


TextObject to = (TextObject)report.ReportDefinition.Sections["Section2"].ReportObjects["textboxname"];

3. Now using reference you can change the value of crystal report control .

Happy Coding!!

Thursday, March 10, 2011

Send email through gmail

Following snippet is for sending mail through gmail smtp server.

create 4 textboxes for to, from,subject & message. Dot Net has provided us a class System.Net.Mail.SmtpClient. with the help of this class we can send the email.

If your company have an email server you require to change following:
Gmails SMTP port is 587.
Gmails Host address is
Give your username and password("", "pwd") to make following code workable.

            System.Net.Mail.MailMessage mailmessage=new System.Net.Mail.MailMessage();
            mailmessage.Body = txtBody.Text;
            mailmessage.From = new System.Net.Mail.MailAddress( txtFrom.Text);
            mailmessage.Subject = txtSubject.Text;
            mailmessage.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smt = new System.Net.Mail.SmtpClient();
            smt.UseDefaultCredentials = false;

            smt.Credentials = new System.Net.NetworkCredential("", "pwd");
            smt.Host = "";
            smt.Port = 587;
            smt.EnableSsl = true;


Above code snippet will help us to send an email.

Happy Coding !! 

Wednesday, February 23, 2011

Font size changing while loading alert window through Javascript in response.write()

I am using javscript to open a alert window when a button is clicked.
Here is the code in cs file :

Interesting thing is with the click of button, the font size changed (increased) in the original page; refreshing the screen brings back the original font size.

The above problem is caused because the script is coming outside the html in the rendered html page.

.Net Frame work has provide a class to manage client side scripts

Try like this.
// Define the name and type of the client scripts on the page.
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(cstype, "name", "<script>alert('hai')</script>");
This code will help us to resolve our font size increase issue.

Happy Coding!!

Disable back using javascript in

After login we come to home page & after click on back button it should not move to login page
so we use following script to restrict it.

when we traverse through webpages its history is maintained. Don't forget to call setTimeout method
It works on Firefox , IE & Chrome  also
 <script type = "text/javascript" >
function disableback()
setTimeout("disableback()", 0);

Call above method on Body unload method on login page so when u move from login to home page after click on home's back button, we will be on same page that is on home page.
<body onunload="disableback()">
 now your back button problem will be solved......

Happy Coding!!

Tuesday, February 22, 2011

Focus particular textbox in page bydefault (like google) & on enter key press hit button from page(like google search button)

This are very easy things to do,

when we open any page and we want particular textbox contains cursor & after pressing enter button particular button gets clicked then do the following things.

In page contains a form tag, It contains defaultbutton & defaultfocus attributes set it as per your requirement

defaultbutton:= give your buttons name which u want to clicked
defaultfocus:= give control name for which you want focus after page load
<form id="form1" runat="server" defaultbutton="Button1" defaultfocus="TextBox1">
 Here  TextBox1 contains cursor & after press enter Button1's click event gets called.

Happy Coding !!

Encrypt & Decrypt Connection string in web.config

first import System.web.configuration namespace then add following code for encryption
for example on some button i have written following code for encryption
        string path=@"\Projectname";
        Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
        ConfigurationSection appsetting = config.GetSection("connectionStrings");

ProtectSection Method in  SectionInformation encrypts the connection string

Now lets see for how to decrypt, SectionInformation contains method UnprotectSection() for decryption
code as below
        string path = @"\Projectname";
        Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
        ConfigurationSection appsetting = config.GetSection("connectionStrings");

This Snippet will help to Encrpt & Decrypt  connection String

Happy Coding!!

Wednesday, February 9, 2011

Freeze the columns in Gridview

Sometimes you need a way in which you don't want end user to edit some columns of gridview. There is no direct way for this. But you can achieve this with the help of CSS functionality.

STEP 1: Add the Following CSS class to the .aspx file
<style type="CSS/Text">
text-align: left;
cursor: default;

STEP 2: Set the CSSClass property of the cells in the GridView RowDatabound event as shown below.

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
e.Row.Cells[1].CssClass = "freezecolumn";
This way you can freeze the column or you can say non-editable column in gridview.

Happy Coding!!

Tuesday, February 1, 2011

append values into querystring in

If you try to add "&" directly into string , it doesn't work
Ex: WebPage.aspx?Var1=DVal1&Var2=DVal2

if u want to create above querystring & u try to write statement as below
string url="WebPage.aspx?Var1=" + DVal1 + "&Var2=" + DVal2;
DVal1 & DVal2 are strings. It will work for simple cases but it won't work for few cases.
If DVal1 & DVal2 contains space in contents (DVal1="my value") It will show you
to make proper querystring you have to use a Server.UrlEncode

It replaces space with %20 and & with %26
string url="WebPage.aspx?Var1=" + Server.UrlEncode(DVal1) + "&Var2=" + Server.UrlEncode(DVal2);

Use of Server.UrlEncode avoids error and help us to append data to querystring.

Happy Coding!! 

Saturday, January 29, 2011

Maintain Scroll position in div Asp.Net

This javascript is useful for maintaining scroll position in Panel or div tag...
Write following javascript function inside js file and add script reference to this file on page Or write following javascript on the same page inside script tag, it is called as inline javascript.

Following code in Script Tag....
window.onload = function(){
var strCook = document.cookie;
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("divTest").scrollTop = strPos;
function SetDivPosition(){
var intY = document.getElementById("divTest").scrollTop;
document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";

Following code in body

You need to call SetDivPosition() javascript function on div's onscroll event
< div id="divTest" width:150px;height:200px;overflow:auto" onscroll="SetDivPosition()">
--Gridview (or more data)
This way you can maintain the scroll position.

Happy Coding!!

Tuesday, January 11, 2011

checkbox in listbox

Sometimes we require a list of checkbox. How can we achieve this in
1. Add div on your page. Set some height and width to div so it will be fixed in height and width and will look appropriately on the web page.
Set overflow:auto so if your list is increasing it will give you scrollbar.
< div style="BORDER: thin solid; OVERFLOW: auto; WIDTH: 100px; HEIGHT: 100px">
< /div>
2. Insert CheckBoxList control inside div.
< div style="BORDER: thin solid; OVERFLOW: auto; WIDTH: 100px; HEIGHT: 100px">
< asp:checkboxlist id="CheckBoxList1" runat="server">
This way you can get a checkboxlist effect.

Happy Coding!!