Pages

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
Steps:


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"];

OR 

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


3. Now using reference you can change the value of crystal report control .
          to.Text=newvalue;








Thursday, March 10, 2011

Send mail through gmail smtp


Following snippet is for sending mail through gmail smtp. create 4 textboxes for to, from,subject & message

            System.Net.Mail.MailMessage mailmessage=new System.Net.Mail.MailMessage();
            mailmessage.Body = txtBody.Text;
            mailmessage.From = new System.Net.Mail.MailAddress( txtFrom.Text);
            mailmessage.To.Add(txtTo.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("username@gmail.com", "pwd");
            smt.Host = "smtp.gmail.com";
            smt.Port = 587;
            smt.EnableSsl = true;

            smt.Send(mailmessage);

Wednesday, February 23, 2011

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

Problem:
I am using javscript to open a alert window when a button is clicked.
Here is the code in cs file :
Response.Write("<script>alert('hai')</script>");

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

I have searched on it google & after that i got solution





Solution
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>");

Disable back using javascript

After login we come to j\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 history is maintained. Dont forget to call setTimeout method
It works on Firefox , IE & Chrome  also
 <script type = "text/javascript" >
function disableback()
{
window.history.forward();
}
setTimeout("disableback()", 0);
</script>

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......

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 & we want particular textbox contains cursor & after pressing enter button particular button gets clicked then do the following things
in asp.net 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

Encrypt & Decrypt Connection string in web.config

first import System.web.configuration namespace then add folowing 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");
        appsetting.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
        config.Save();

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");
        appsetting.SectionInformation.UnprotectSection();
        config.Save();

This Snippet will help to Encrpt & Decrypt  connection String

Wednesday, February 9, 2011

Freeze the columns in Gridview

STEP 1: Add the Following CSS class to the .aspx file
<style type="CSS/Text">
td.freezecolumn{
text-align: left;
border-width:0;
position:relative;
cursor: default;
left:inherit;
}
</style>


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

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[1].CssClass = "freezecolumn";
}