Wednesday, December 2, 2009

Connection Class and WebConfig(Sample using for a application)

//***********a)CONNECTION CLASS(in C#)***********
using System;
using System.Data;
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;
using System.Data.SqlClient;


namespace Connection
{

public class ClsConnection
{


private SqlConnection conSQL;
// private string conString = "server=SYS9;uid=root; password=integral; database=dbsuperlistexplode;";
// private string conString = ConfigurationSettings.AppSettings["constringLogin"].ToString();
private string conString = ConfigurationManager.ConnectionStrings["conStrSuperListExplode"].ToString();


//private MySqlConnection consql=new MySqlConnection(ConfigurationSettings.AppSettings(conStrTutorinn);



//-----------------------------------------------------------------------------------
string _SuccessMsg;
public string SuccessMsg //success message - cannot always be returned, so we have a read only access method..
{
get { return _SuccessMsg; }
set { _SuccessMsg = value; }
}
//-----------------------------------------------------------------------------------------
public ClsConnection()//Constructor
{

//conSQL = new MySqlConnection(ConnectionStringSettings["constringLogin"]);
conSQL = new SqlConnection(conString);
}

public string SqlExecuteQuery(string str)
{
try
{
conSQL.Open();
SqlCommand cmd = new SqlCommand(str, conSQL);
cmd.ExecuteNonQuery();
conSQL.Close();
SuccessMsg = "Success";
return SuccessMsg;
}
catch (Exception e)
{
conSQL.Close();
SuccessMsg = e.Message;
return SuccessMsg;
}

}
//-----------------------------------------------------------------------------------------
public bool SQLReturnBool(string sqlstr)
{

bool retval;
try
{
conSQL.Close();
conSQL.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conSQL);
int retvalbool = int.Parse(cmd.ExecuteScalar().ToString());
if (retvalbool == 1) retval = true;
else retval = false;
conSQL.Close();
}
catch (Exception ex)
{
conSQL.Close();
SuccessMsg = ex.Message;
retval = false;
}
return retval;
}

//-----------------------------------------------------------------------------------------
public int SQLReturnInteger(string sqlstr)
{

int retval;
try
{
conSQL.Close();
conSQL.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conSQL);
retval = int.Parse(cmd.ExecuteScalar().ToString());
conSQL.Close();
}
catch (Exception ex)
{
conSQL.Close();
SuccessMsg = ex.Message;
retval = 0;
}
return retval;
}

//-----------------------------------------------------------------------------------------
public float SQLReturnFloat(string sqlstr)
{
float retval;
try
{
conSQL.Close();
conSQL.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conSQL);
retval = float.Parse(cmd.ExecuteScalar().ToString());
conSQL.Close();
}
catch (Exception ex)
{
conSQL.Close();
SuccessMsg = ex.Message;
retval = 0;
}
return retval;
}
//-----------------------------------------------------------------------------------------
public string SQLReturnString(string sqlstr)
{
string retval;
try
{
conSQL.Close();
conSQL.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conSQL);
retval = cmd.ExecuteScalar().ToString();
SuccessMsg = "Success";
conSQL.Close();
}
catch (Exception ex)
{
conSQL.Close();
SuccessMsg = ex.Message;
retval = "";
}
return retval;
}

//-----------------------------------------------------------------------------------------



public DataSet SelectDataset(string sqlstr, string sqltbl)
{
DataSet dssql = new DataSet();
try
{
if (conSQL.State == ConnectionState.Open)
{
conSQL.Close();
conSQL.Open();
}
else
{
conSQL.Open();
}

SqlDataAdapter dasql = new SqlDataAdapter(sqlstr, conSQL);

dasql.Fill(dssql, sqltbl);

}
catch (Exception ex)
{
SuccessMsg = ex.Message;
conSQL.Close();
}
conSQL.Close();
return dssql;
}

public byte[] SQLReturnByte(string sqlstr)
{
byte[] Rblob = null;
// try
// {
conSQL.Close();
conSQL.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conSQL);
SqlDataReader MyReader = cmd.ExecuteReader();
if (MyReader.Read())
{
//byte[] m_MyImage = (byte[])MyReader["pimage"];
Rblob = (byte[])MyReader[0];
//Response.BinaryWrite(retval);

}
//retval[]= Convert.ToBase64CharArray(cmd.ExecuteScalar().ToString());
SuccessMsg = "Success";
conSQL.Close();

// }
// catch (Exception ex)
// {
// conSQL.Close();
// SuccessMsg=ex.Message;
// //retval= "";
//
// }
return Rblob;

}


}
}

//-------------------------------------------------------------------------------

// **********b)WEBCONFIG****************


<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>

<appSettings>
<add key="ChooseMode" value="TEST"/>
<!--
Set ChooseMode as TEST for test server.
Set ChooseMode as LIVE for live server.
-->
<add key="mailFrmPrimestreams.SendMail" value="http://primestreams.net/SendMail.asmx"/>
</appSettings>
<connectionStrings>
<add name="conStrSuperListExplode" connectionString="Data Source=SYS26\SQLEXPRESS;Initial Catalog=dbAffiliser;User ID=sa;Password=integraloffice"/>
<add name="dbAffiliserConnectionString" connectionString="Data Source=SYS26\SQLEXPRESS;Initial Catalog=dbAffiliser;User ID=sa;Password=integraloffice" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<sessionState mode="InProc" cookieless="false" timeout="30"></sessionState>
<identity impersonate="true" />
<httpHandlers>
<add verb="GET" path="FtbWebResource.axd" type="FreeTextBoxControls.AssemblyResourceHandler, FreeTextBox" />
<add verb="GET" path="CaptchaImage.aspx" type="WebControlCaptcha.CaptchaImageHandler, WebControlCaptcha" />
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

</httpHandlers>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<!--jerry-->
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->

<customErrors mode="Off" />
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

</system.web>
<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<!--
<jsonSerialization maxJsonLength="500">
<converters>
<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
</converters>
</jsonSerialization>
-->
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
<!--
<authenticationService enabled="true" requireSSL = "true|false"/>
-->
<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
writeAccessProperties attributes. -->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true" enableCaching="true" />
-->
</scripting>



</system.web.extensions>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</system.webServer>
</configuration>

No comments:

Post a Comment