adsense

Monday, November 9, 2009

Programmatically manipulating web.config (ASP. NET 2.0/3.5) and App.config

The following lines of code changes the appSetting section "DEFAULT_LANGUAGE" and
refreshes the current page (causes re-load of the entire web as we modify the Web.config)

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



private AppSettingsSection section;

protected void btnSave_Click(object sender, EventArgs e)
{


Configuration config= WebConfigurationManager.OpenWebConfiguration("~");
section = (AppSettingsSection)config.GetSection("appSettings");
section.Settings["DEFAULT_LANGUAGE"].Value ="some language";
config.Save();

Response.Redirect(Request.Url.ToString());

}

For windows applications code is listed below.


System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["oldPlace"].Value = "3";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

Happy Coding...!

samitha

Thursday, November 5, 2009

Report Viewer Export to PDF

You can use this as a common method to create reports. Here a dynamic pdf will be generated with the time ticks
//using

using Microsoft.Reporting.WebForms;



public static void GenerateReport(string repName,ref ReportViewer rv)
{
string mimeType;
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streamids;
byte[] exportBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streamids, out warnings);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = mimeType;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename="+repName+ DateTime.Now.TimeOfDay.Ticks.ToString() + "." + fileNameExtension);
HttpContext.Current.Response.BinaryWrite(exportBytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}

Tuesday, November 3, 2009

ASP Net - 'PageMethods' is undefined

For this make sure you have following things set

1)PageMethods must be exposed with the public access modifier and static.
E.g.
[WebMethod]

public static bool foo

2)Set EnablePageMethods="true" on your ScriptManager.