Code to export data to excel in asp.net using c#  

Posted by: Quadtechindia in

if (GridView1.Rows.Count > 0)
{

Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

Response.Write("

");
Response.Write("
logo image
Title
");
Response.Write("

");
Response.Write("
");


gvRes.HeaderStyle.Font.Bold = true;
gvRes.HeaderStyle.BackColor = System.Drawing.Color.Black;


System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
HtmlForm frm = new HtmlForm();
this.Controls.Add(frm);

frm.Controls.Add(gvRes);
frm.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

}

How to Enable saving of changes in SQL server 2008  

Posted by: Quadtechindia in

you can get the details by clicking on ? on the dialog box that shows this error

or the flow is

Open the following
Tools > Options > Designers > Table and Database Designers > Prevent saving changes that require table re-creation. Just Turn this option off and you will be able to save the tables again.

and now you can save changes to all tables of any database.

Function to Encrypt-Decrypt Connection String in Web.Config File  

Posted by: Quadtechindia in

Code to Encrypt Connection String in Web.Config File

using System.Configuration;
using System.Web.Configuration;
using System.Web.Security;


public void EncryptConnectionString()
{

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
}


public void DecryptConnectionString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}

A potentially dangerous Request.Form value was detected from the client side  

Posted by: Quadtechindia in

If you face such above mentioned error on inserting a value then that value might be containing anchor tags(< or >). If you want to insert these values you have to write

ValidateRequest="false" in page directive (first line of .aspx file)


Example:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xyx.aspx.cs" Inherits="xyz" ValidateRequest="false" %>

Code to identify sql server version and edition  

Posted by: Quadtechindia in

Write following statement in Query Window

SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')



* The product version (10.0.1600.24)
* The product level (RTM)
* The edition (Enterprise)

Inorder to find out version of sql server write the following statements in query window

SELECT @@VERSION

Code to export data to excel via asp.net  

Posted by: Quadtechindia in

if (GridView1.Rows.Count > 0)
{

Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

Response.Write("Add anything (tag or text) as per requirement");

GridView1.HeaderStyle.Font.Bold = true;
GridView1.HeaderStyle.BackColor = System.Drawing.Color.Black;


System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
HtmlForm frm = new HtmlForm();
this.Controls.Add(frm);

frm.Controls.Add(GridView1);
frm.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

}

What is Literal ?  

Posted by: Quadtechindia in

A Literal Web Server control doesn't have any visual appearance on a Web Form but is used to insert literal text into a Web Form. This control makes it possible to add HTML code directly in the code designer window without switching to design view and clicking the HTML button to edit the HTML.
The notable property of this control is the text property which is the text that is inserted into the Web Form.

Example

The following line of code demonstrates that.

Literal1.Text = "Literal Demo"