Wednesday, September 30, 2009

Export ASP.NET GridView to Excel


















protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

BindData();

}

}
private void BindData()

{

string constr = @"Data Source=XYZ;Initial Catalog=testdatabase;Persist Security Info=True;User ID=ABC;Password=abcdefg;";

string query = "SELECT * From emp1";

SqlDataAdapter da = new SqlDataAdapter(query, constr);

DataTable table = new DataTable();

da.Fill(table);

GridView1.DataSource = table;

GridView1.DataBind();

}

protected void btnExcelReport_Click(object sender, EventArgs e)

{

Response.Clear();

Response.AddHeader("content-disposition", "attachment; filename=UserRecord.xls");

Response.Charset = "";

Response.ContentType = "application/vnd.xls";

StringWriter stringWriter = new StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

GVExamUserSearchList.RenderControl(htmlWriter);

Response.Write(stringWriter.ToString());

Response.End();

}

Sometimes, when you render any asp.net control dynamically as I am doing with the GridView control in this tutorial, you can get HttpException with the following message:


Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.


The problem can be solved by overriding Page class VerifyRenderingInServerForm method which confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

public override void VerifyRenderingInServerForm(Control control)

{ }


When you will click the Export to Excel button you will see the following dialog box asking you to open or save dynamically generated Excel file.

















Wednesday, September 23, 2009

//*// GridView With Radio Buttion //*//




















"<Columns>
<asp:TemplateField HeaderText='Select'>
<ItemTemplate>

<input name='MyRadioButton' type='radio' value='<%# Eval('ExamId') %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText='CentreCode' ReadOnly='True' DataField='CenterCode' />
<asp:BoundField HeaderText='CentreName' ReadOnly='True' DataField='CenterName' /> <asp:BoundField HeaderText='ExamDate' ReadOnly='True' DataField='ExamDate' />
<asp:BoundField HeaderText='ExamTime' ReadOnly='True' DataField='ExamTime' />
</Columns>"

Tuesday, September 22, 2009

//*// Select serial number with a list of columns //*//


select
(select count(*) from emp where empname <= n.empname) as SRNo,
empname from emp n
order by empname

Monday, September 21, 2009

//*// Add serial or sequential number column in a GridView control //*//

< asp:TemplateField HeaderText=”S/No” >

< ItemTemplate >

< %#Container.DataItemIndex+1 % >

< /ItemTemplate >

< /asp:TemplateField >

//*// Code in Code behind of delete button in GridView //*//


int selectedRowindex =(int)myDataGrid.DataKeys[e.RowIndex].Value;
string FirstcoloumValue = myDataGrid.Rows[e.RowIndex].Cells[0].Text;

//*// Regular Expression For Double Value //*//


[0-9]+(\.[0-9][0-9])?
Ex : 0.11,11,1.11

—————-javascript function for custom validator control——————
function check() {
var regx = /^([-]?)([0-9]+)((.[0-9]{2})?)$/;
var m = regx.test(document.all.txt.value);
if (m == true) {
alert(”right”);
}
else {
alert(”error”);
}
}

//*// Transaction in sql with c# //*//

public int DeleteProblems(int _intProblemId)
{
int icheck = 0;
using (SqlConnection connection = new SqlConnection(_strConnectionString))
{
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = null;

try
{
// BeginTransaction() Requires Open Connection
connection.Open();

transaction = connection.BeginTransaction();

// Assign Transaction to Command
command.Transaction = transaction;

// Execute 1st Command
command.CommandText = “delete from ForumSolution where ProblemId=’” + _intProblemId + “‘”;
icheck=command.ExecuteNonQuery();

// Execute 2nd Command
command.CommandText = “delete from ForumProblem where ProblemId=’” + _intProblemId + “‘”;
icheck=command.ExecuteNonQuery();

transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
return icheck;
}

}