protected void Button3_Click(object sender, EventArgs e) { DataSet dsExport = dataSet; DataGrid dgExport = new DataGrid(); dgExport.DataSource = dsExport; //ExportToExcel(); DataGridToExcel(dgExport, Response); } protected void DataGridToExcel(DataGrid dGridExport, HttpResponse httpResp) { httpResp.Clear(); httpResp.Charset = ""; httpResp.ContentType = "application/vnd.ms-excel"; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); DataGrid dGrid = new DataGrid(); dGrid = dGridExport; dGrid.HeaderStyle.Font.Bold = true; dGrid.DataBind(); dGrid.RenderControl(htmlWrite); httpResp.Write(stringWrite.ToString()); httpResp.End(); }
Or to export a DataGrid to Excel use:
protected void LinkButton1_Click(object sender, EventArgs e) { exportToExcel(); } protected void exportToExcel() { VerifyRenderingInServerForm(GridView1); /* This is a limit in Excel prior to Office 2007 */ if (GridView1.Rows.Count > 65536) { Page.Response.Clear(); Page.Response.Write("Too many lines to export."); Page.Response.End(); } GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.DataBind(); Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=FileName.xls"); Response.Charset = ""; // If you want the option to open the Excel file without saving than // comment out the line below // Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); GridView1.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { /* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */ }
Post Comment Now
Written with .NET 2.0. Should work with 3.x as well.
using System.IO.Compression; using System.Text; protected string Compress(string text) { byte[] buffer = Encoding.UTF8.GetBytes(text); MemoryStream ms = new MemoryStream(); using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) { zip.Write(buffer, 0, buffer.Length); } ms.Position = 0; MemoryStream outStream = new MemoryStream(); byte[] compressed = new byte[ms.Length]; ms.Read(compressed, 0, compressed.Length); byte[] gzBuffer = new byte[compressed.Length + 4]; System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length); System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4); return Convert.ToBase64String(gzBuffer); } protected string Decompress(string compressedText) { byte[] gzBuffer = Convert.FromBase64String(compressedText); using (MemoryStream ms = new MemoryStream()) { int msgLength = BitConverter.ToInt32(gzBuffer, 0); ms.Write(gzBuffer, 4, gzBuffer.Length - 4); byte[] buffer = new byte[msgLength]; ms.Position = 0; using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress)) { zip.Read(buffer, 0, buffer.Length); } return Encoding.UTF8.GetString(buffer); } }
Post Comment Now
I am fairly certain the .NET versioning (numbering release) scheme is the dumbest ever created. Seriously who in their right mind does a point release that is a extension of the previous point but not the one before that (2.0 -> 3.0)? Then 3.0 -> to 3.5? wtf? Remember 1.0 and 1.1? Hmmm. How does that fit in? Oh yea, it doesn’t.
It went like this…
1.0 to 1.1. Perfect. Basically the 1.0 framework fixed with a couple new things. That’s actually how it took place.
Then 2.0. A whole new framework. Yay! We are good at this point.
Then 3.0. Huh? Another new framework? Yay!? Wait, no, huh? It’s not a framework? It’s a extension but only for 2.0 and not related at all to 1.0 or 1.1? Oh good heavens MS.
Then 3.5 Another extension to 2.0 or actually 3.0? Ok, now I’m getting mad. What happened to 3.1, 3.2, etc.?
What’s next? 8.0 with 10.0 extentions?
Might as well number it 38.5. What’s the difference at this point? There is NO consistency anyway and the higher the number the better right?
Anyway here is the 2.0 -> 3.5 relationships…
![]()
Seriously, if you know what the hell MS was thinking, post a comment.
Post Comment Now
protected void Page_Load(object sender, EventArgs e) { // Show admin link if they have access to the admin directory. // hyperLinkAdmin.Visible = hasAccessToDir("admin"); } protected bool hasAccessToDir(string dir) { return System.IO.Directory.Exists(MapPath(dir)); }
Post Comment Now
C#
public string CreateADUser(string szUsername, string szDisplayName, string szFirstName, string szLastName, string szMiddleInitial, string szPhone, string szEmail, string szOrgUnit) { string szPassword = "fooBar_123"; //temp only. string szCNName = "CN=" + szUsername; try { DirectoryEntry objContainer = new DirectoryEntry("LDAP://OU=mySubOU,OU=myMainOU,DC=KEVINCORNWELL,DC=com"); DirectoryEntry objUser = objContainer.Children.Add(szCNName.ToLower(), "user"); objUser.Properties["sAMAccountName"].Value = szUsername; objUser.Properties["userPrincipalName"].Value = szUsername + "@KEVINCORNWELL.COM"; objUser.Properties["displayName"].Value = szDisplayName; objUser.Properties["GivenName"].Value = szFirstName; objUser.Properties["sn"].Value = szLastName; objUser.Properties["Initials"].Value = szMiddleInitial; objUser.Properties["mail"].Value = szEmail; objUser.Properties["TelephoneNumber"].Value = szPhone; objUser.Properties["Description"].Value = szOrgUnit; objUser.CommitChanges(); objUser.Invoke("SetPassword", szPassword); objUser.CommitChanges(); int flags = (int)objUser.Properties["userAccountControl"].Value; objUser.Properties["userAccountControl"].Value = flags & ~0x2; //Enable User Account objUser.Properties["pwdLastSet"].Value = 0; objUser.CommitChanges(); return ""; } catch (Exception ex) { return ex.InnerException + ex.Message + ex.Source + ex.StackTrace + ex.Data; } }
Post Comment Now

