Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 01, 2012 01:12 PM by cms9651
Member
174 Points
571 Posts
May 01, 2012 12:56 PM|LINK
Hello there. First of all I must say that I am a newbie when it comes to net. Here is my problem.
I need call the
protected void ExportToExcel(object sender, EventArgs e)
in the
protected void Page_Load(object sender, EventArgs e)
for your execution.
If you have link for similar task, please give it me.
Your help would be very appreciated.thanks for your time and hints.
using System; using System.IO; using System.Data; using System.Data.Odbc; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _inc_excel : System.Web.UI.Page { private DataTable GetData(OdbcCommand cmd) { DataTable dt = new DataTable(); OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString); System.Data.Odbc.OdbcDataAdapter sda = new OdbcDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = myConnectionString; try { myConnectionString.Open(); sda.SelectCommand = cmd; sda.Fill(dt); return dt; } catch (Exception ex) { throw ex; } finally { myConnectionString.Close(); sda.Dispose(); myConnectionString.Dispose(); } } protected void Page_Load(object sender, EventArgs e) { ExportToExcel(object sender, EventArgs e); } protected void ExportToExcel(object sender, EventArgs e) { string strQuery = "select CustomerID, ContactName, City, PostalCode" + " from customers"; OdbcCommand cmd = new OdbcCommand(strQuery); DataTable dt = GetData(cmd); GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=DataTable.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); for (int i = 0; i < GridView1.Rows.Count; i++) { GridView1.Rows[i].Attributes.Add("class", "textmode"); } GridView1.RenderControl(hw); string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } }
Contributor
3116 Points
764 Posts
May 01, 2012 01:05 PM|LINK
in protected void ExportToExcel(object sender, EventArgs e)
why do u need object and eventArgs?
make you function as following
protected void ExportToExcel()
and in your page load
ExportToExcel();
2346 Points
478 Posts
May 01, 2012 01:08 PM|LINK
cms9651 protected void Page_Load(object sender, EventArgs e) { ExportToExcel(object sender, EventArgs e); }
Need to pass pageloads sender and eventargs.
protected void Page_Load(object sender, EventArgs e) { ExportToExcel(sender, e); }
May 01, 2012 01:09 PM|LINK
hi, thanks for your reply, but:
Method overload 'ExportToExcel' no accepts 0 arguments protected void Page_Load(object sender, EventArgs e) { ExportToExcel(); } protected void ExportToExcel(object sender, EventArgs e) { ... }
May 01, 2012 01:11 PM|LINK
change this
protected void ExportToExcel(object sender, EventArgs e) { ... }
To
protected void ExportToExcel(){...}
i don't see that you need these parameters
May 01, 2012 01:12 PM|LINK
hello Bimalvv!
Thanks you very much for your help I'm really happy for your quickly answer. Good bye
cms9651
Member
174 Points
571 Posts
How to call different protected void in the Page_Load ?
May 01, 2012 12:56 PM|LINK
Hello there.
First of all I must say that I am a newbie when it comes to net.
Here is my problem.
I need call the
in the
for your execution.
If you have link for similar task, please give it me.
Your help would be very appreciated.
thanks for your time and hints.
using System; using System.IO; using System.Data; using System.Data.Odbc; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _inc_excel : System.Web.UI.Page { private DataTable GetData(OdbcCommand cmd) { DataTable dt = new DataTable(); OdbcConnection myConnectionString = new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString); System.Data.Odbc.OdbcDataAdapter sda = new OdbcDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = myConnectionString; try { myConnectionString.Open(); sda.SelectCommand = cmd; sda.Fill(dt); return dt; } catch (Exception ex) { throw ex; } finally { myConnectionString.Close(); sda.Dispose(); myConnectionString.Dispose(); } } protected void Page_Load(object sender, EventArgs e) { ExportToExcel(object sender, EventArgs e); } protected void ExportToExcel(object sender, EventArgs e) { string strQuery = "select CustomerID, ContactName, City, PostalCode" + " from customers"; OdbcCommand cmd = new OdbcCommand(strQuery); DataTable dt = GetData(cmd); GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=DataTable.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); for (int i = 0; i < GridView1.Rows.Count; i++) { GridView1.Rows[i].Attributes.Add("class", "textmode"); } GridView1.RenderControl(hw); string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } }AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to call different protected void in the Page_Load ?
May 01, 2012 01:05 PM|LINK
in protected void ExportToExcel(object sender, EventArgs e)
why do u need object and eventArgs?
make you function as following
and in your page loadBimalvv
Contributor
2346 Points
478 Posts
Re: How to call different protected void in the Page_Load ?
May 01, 2012 01:08 PM|LINK
Need to pass pageloads sender and eventargs.
protected void Page_Load(object sender, EventArgs e) { ExportToExcel(sender, e); }Bimal
cms9651
Member
174 Points
571 Posts
Re: How to call different protected void in the Page_Load ?
May 01, 2012 01:09 PM|LINK
hi, thanks for your reply, but:
Method overload 'ExportToExcel' no accepts 0 arguments protected void Page_Load(object sender, EventArgs e) { ExportToExcel(); } protected void ExportToExcel(object sender, EventArgs e) { ... }AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: How to call different protected void in the Page_Load ?
May 01, 2012 01:11 PM|LINK
change this
protected void ExportToExcel(object sender, EventArgs e) { ... }i don't see that you need these parameterscms9651
Member
174 Points
571 Posts
Re: How to call different protected void in the Page_Load ?
May 01, 2012 01:12 PM|LINK
hello Bimalvv!
Thanks you very much for your help
I'm really happy for your quickly answer.
Good bye