< system.net>
< mailSettings>
< smtp from="feedback@dotnetkanna.com" >
< network host="smtp.dotnetkanna.com" defaultCredentials ="true" userName="feedback@dotnetkanna.com" port="25" password="12345" />
< /smtp>
< /mailSettings >
< /system.net>
To Access, this SMTP Mail Setting Programatically, you need to import below namespaces:
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
The .NET Framework provides APIs for accessing settings in a configuration file. Below is how you access the SMTP mail settings of a web.config file in code:
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
Response.Write("host: " + settings.Smtp.Network.Host + "< br />");
Response.Write("port: " + settings.Smtp.Network.Port + "< br />");
Response.Write("Username: " + settings.Smtp.Network.UserName + "< br />");
Response.Write("Password: " + settings.Smtp.Network.Password + "< br />");
Response.Write("from: " + settings.Smtp.From + "< br />");
Showing posts with label Web Config. Show all posts
Showing posts with label Web Config. Show all posts
Tuesday, July 21, 2009
How to Increase the Maximum Upload Size?
The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, you'd do this:
< system.web>
< httpRuntime executionTimeout="240" maxRequestLength="20480" />
< /system.web>
< system.web>
< httpRuntime executionTimeout="240" maxRequestLength="20480" />
< /system.web>
Tuesday, January 27, 2009
How to Encrypt/Decrypt the web.Config Connection String?
using System.Web.Configuration;
using System.Web.Security;
using System.Configuration;
public void EncryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
}
--------------
public void DecryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
using System.Web.Security;
using System.Configuration;
public void EncryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
}
--------------
public void DecryptConnString()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
Wednesday, March 5, 2008
How to Add User defined tags in the Webconfig file and Write and Retrive the values
using System.Xml; // Must Include
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="AdminSettings" type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
.
.
.
</configSections>
.
.
</configuration>
//Your WebConfig Content Something like this
//User defined tags should have to mention like the above section
---------------------------------
<AdminSettings>
<add key="AdminUserName" value="professionals" />
<add key="AdminPassword" value="buzz" />
</AdminSettings>
---------------------------------
protected void BtnWriteConfig_Click(object sender, EventArgs e)
{
string Path=Server.MapPath("~/web.config");
XmlDocument xDoc = new XmlDocument();
XmlNodeList nodeList;
XmlNodeList nodeAppSettings;
xDoc.Load(Path);
nodeList = xDoc.GetElementsByTagName("AdminSettings");
nodeAppSettings = nodeList[0].ChildNodes;
for (int i = 0; i < nodeAppSettings.Count; i++)
{
XmlAttributeCollection xmlAttCollection = nodeAppSettings[i].Attributes;
if (xmlAttCollection[0].InnerXml == "AdminUserName")
{
xmlAttCollection[1].InnerXml = USR.ToString();
}
if (xmlAttCollection[0].InnerXml == "AdminPassword")
{
xmlAttCollection[1].InnerXml = PWD.ToString();
}
}
xDoc.Save(Path);
}
---------------------------------
//Getting userdefined tag values
string Uname = ((System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("AdminSettings"))["AdminUserName"] + "";
string Pwd = ((System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("AdminSettings"))["AdminPassword"] + "";
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="AdminSettings" type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
.
.
.
</configSections>
.
.
</configuration>
//Your WebConfig Content Something like this
//User defined tags should have to mention like the above section
---------------------------------
<AdminSettings>
<add key="AdminUserName" value="professionals" />
<add key="AdminPassword" value="buzz" />
</AdminSettings>
---------------------------------
protected void BtnWriteConfig_Click(object sender, EventArgs e)
{
string Path=Server.MapPath("~/web.config");
XmlDocument xDoc = new XmlDocument();
XmlNodeList nodeList;
XmlNodeList nodeAppSettings;
xDoc.Load(Path);
nodeList = xDoc.GetElementsByTagName("AdminSettings");
nodeAppSettings = nodeList[0].ChildNodes;
for (int i = 0; i < nodeAppSettings.Count; i++)
{
XmlAttributeCollection xmlAttCollection = nodeAppSettings[i].Attributes;
if (xmlAttCollection[0].InnerXml == "AdminUserName")
{
xmlAttCollection[1].InnerXml = USR.ToString();
}
if (xmlAttCollection[0].InnerXml == "AdminPassword")
{
xmlAttCollection[1].InnerXml = PWD.ToString();
}
}
xDoc.Save(Path);
}
---------------------------------
//Getting userdefined tag values
string Uname = ((System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("AdminSettings"))["AdminUserName"] + "";
string Pwd = ((System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("AdminSettings"))["AdminPassword"] + "";
Friday, July 20, 2007
How to Create DataBase Connection Using WebConfig for .Net
For Access
===========
In WebConfig
============
Rules
=====
1.Within a Configuration tag u can palce it anywhere.
2.This <connectionStrings> or <appSettings> tag shoul not be nested with any other tag other than this <configuration>
Within the configuration tag they are 2 diff way of connection,they are
==============================================================
1st way
========
Include the below content in webconfig
======================================
<appSettings>
<remove key ="Con"/>
<add key ="Con" value ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Kanna\Test.mdb" />
</appSettings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OleDbConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OleDbConnection(ConfigurationManager.AppSettings["Con"]);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
=======================================================================================
2nd Way
=======
Include the below content in webconfig
======================================
<connectionStrings>
<remove name ="Con" />
<add name ="Con" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Kanna\Test.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OleDbConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
=========================================================================================
For Oracle
==========
1st way
========
Include the below content in webconfig
======================================
<appSettings>
<remove key ="Con"/>
<add key ="Con" value ="data source=tcms; User id=tcms_testnew; password=socool;" />
</appSettings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OracleConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OracleConnection (ConfigurationManager.AppSettings["Con"]);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OracleDataAdapter adp = new OracleDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
=======================================================================================
2nd Way
=======
Include the below content in webconfig
======================================
<connectionStrings>
<remove name="Con"/>
<add name="Con" connectionString="data source=tcms; User id=tcms_testnew; password=socool;" providerName="System.Data.OracleClient"/>
</connectionStrings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OracleConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OracleConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OracleDataAdapte adp = new OracleDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
===========================================================================================
===========
In WebConfig
============
Rules
=====
1.Within a Configuration tag u can palce it anywhere.
2.This <connectionStrings> or <appSettings> tag shoul not be nested with any other tag other than this <configuration>
Within the configuration tag they are 2 diff way of connection,they are
==============================================================
1st way
========
Include the below content in webconfig
======================================
<appSettings>
<remove key ="Con"/>
<add key ="Con" value ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Kanna\Test.mdb" />
</appSettings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OleDbConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OleDbConnection(ConfigurationManager.AppSettings["Con"]);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
=======================================================================================
2nd Way
=======
Include the below content in webconfig
======================================
<connectionStrings>
<remove name ="Con" />
<add name ="Con" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Kanna\Test.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OleDbConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
=========================================================================================
For Oracle
==========
1st way
========
Include the below content in webconfig
======================================
<appSettings>
<remove key ="Con"/>
<add key ="Con" value ="data source=tcms; User id=tcms_testnew; password=socool;" />
</appSettings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OracleConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OracleConnection (ConfigurationManager.AppSettings["Con"]);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OracleDataAdapter adp = new OracleDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
=======================================================================================
2nd Way
=======
Include the below content in webconfig
======================================
<connectionStrings>
<remove name="Con"/>
<add name="Con" connectionString="data source=tcms; User id=tcms_testnew; password=socool;" providerName="System.Data.OracleClient"/>
</connectionStrings>
In Code Behind
=================
public partial class Default3 : System.Web.UI.Page
{
OracleConnection Connect;
protected void Page_Load(object sender, EventArgs e)
{
Connect = new OracleConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
Response.Write("connected");
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
OracleDataAdapte adp = new OracleDataAdapter("select * from Test", Connect);
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
===========================================================================================
Subscribe to:
Posts (Atom)