Friday, 10 February 2012

FILE HANDLING IN ASP.NET : RETRIEVE DATA FROM A FILE



Protected Void RetriveFromFile()
{
position = 0;
total = 0;

FileStream fs = new FileStream(Server.MapPath("Temp/temp.txt"), FileMode.Open, FileAccess.Read);

StreamReader sr = new StreamReader(fs);
while (!sr.EndOfStream)
{
for (int i = 0; i < 5; i++)
{
sr.ReadLine();
}
total++;
}
position++;
read_text(position);

sr.Close();
fs.Close();
}
protected void read_text(long pos)
{
FileStream fs = new FileStream(Server.MapPath("temp.txt"), FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);

if (pos == 1)
{
sr.BaseStream.Seek(0, SeekOrigin.Begin);

TextBox1.Text = sr.ReadLine();
TextBox2.Text = sr.ReadLine();
TextBox3.Text = sr.ReadLine();
TextBox4.Text = sr.ReadLine();

sr.ReadLine();
            }
 else
{
for (int i = 0; i < (pos - 1) * 5; i++)
{
sr.ReadLine();
}
TextBox1.Text = sr.ReadLine();
TextBox2.Text = sr.ReadLine();
TextBox3.Text = sr.ReadLine();
TextBox4.Text = sr.ReadLine();

sr.ReadLine();
}

sr.Close();
fs.Close();
}

========================================================================

HOW TO ADD IMAGE WORD DOC. USING C# CODE IN DOT NET




Hi everyone,
i'd like to add an image/logo to word document automatically via c# code.
i used with Microsoft.Office.Interop.Word.Document object to open the word documents,
and then i used the Shapes.AddPicture method for inserting the image into the documents.

(I didn't use the InlineShapes.AddPicture method because, these method doesn't support layers etc.)

here is my code:
object FileName = filesCollection.ToString();
object saveFileName = FileName;
object vk_read_only = false;
object vk_visibile = true;
object vk_false = false;
object vk_true = true;
object vk_dynamic = 2;
object vk_missing = System.Reflection.Missing.Value;
object LinkToFile = true;
object myRange = System.Reflection.Missing.Value;
object vk_format = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatDocument;
object Left = 1;
object Top = 1;
object Width = 498;
object Height = 89;

oWord.Visible = true;
oWord.Activate();






FILE HANDLING IN ASP.NET : INSERT IN A FILE




Protected Void InsertIntoFile()
{
if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text != "")
{
FileStream fs = new FileStream(Server.MapPath("Temp/temp.txt"), FileMode.Append, FileAccess.Write);

StreamWriter sw = new StreamWriter(fs);

sw.WriteLine(TextBox1.Text);
sw.WriteLine(TextBox2.Text);
sw.WriteLine(TextBox3.Text);
sw.WriteLine(TextBox4.Text);
sw.WriteLine();
sw.Flush();
sw.Close();
fs.Close();
}
else Response.Write("Please fill all the fields.");

total = 0;

FileStream fs1 = new FileStream(Server.MapPath("Temp/temp.txt"), FileMode.Open, FileAccess.Read);

StreamReader sr = new StreamReader(fs1);

while (!sr.EndOfStream)
{
for (int i = 0; i < 5; i++)
{
sr.ReadLine();
}
total++;
}
}




Thursday, 9 February 2012

XML FILE FOR ADROTATOR IN ASP.NET



<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>

<Ad>

<ImageUrl>Picture_URL1.jpg</ImageUrl>

<NavigateUrl>http://www.address1.com</NavigateUrl>

<AlternateText>Text1</AlternateText>

<Keyword>Key1</Keyword>

<Impressions>100</Impressions>

</Ad>

<Ad>

<ImageUrl>Picture_URL2.jpg</ImageUrl>

<NavigateUrl>http://www.address2.com</NavigateUrl>

<AlternateText>Text2</AlternateText>

<Keyword>Key2</Keyword>

<Impressions>100</Impressions>

</Ad>

<Ad>

<ImageUrl>Picture_URL3.jpg</ImageUrl>

<NavigateUrl>http://www.address3.com</NavigateUrl>

<AlternateText>Text3</AlternateText>

<Keyword>Key3</Keyword>

<Impressions>100</Impressions>

</Ad>

</Advertisements>



HOW TO USE ENCRYPTION & DECRYPTION METHOD IN ASP.NET



string NORMAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string ENCRYPT = "YO17KPLVSU50C8WE64GAI3MB2DFNZQ9JXTRH";
string strInput = "MamtaDevi";
string strEncrypted = string.Empty;
string strDecrypted = string.Empty;
string strNewChar;
string strLtr;
int iIdx;
protected EncryptionDecryptionDemo()
   {
    Response.Write(strInput);
    Response.Write("<br />");
    for (int i = 0; i < strInput.Length; i++)
   {
    strLtr = strInput.Substring(i, 1).ToUpper();
    iIdx = NORMAL.IndexOf(strLtr);
    strNewChar = ENCRYPT.Substring(iIdx, 1).ToUpper();
    strEncrypted += strNewChar;
    }
    Response.Write(strEncrypted);
    Response.Write("<br />");

    for (int i = 0; i < strInput.Length; i++)
    {
    strLtr = strEncrypted.Substring(i, 1).ToUpper();
    iIdx = ENCRYPT.IndexOf(strLtr);
    strNewChar = NORMAL.Substring(iIdx, 1).ToUpper();
    strDecrypted += strNewChar;
    }
    Response.Write(strDecrypted);
    }

If you want to use any special characters you can add in variable "NORMAL" and equivalent encrypted value in variable "ENCRYPT".
"ENCRYPT" variable has the reordered letters of "NORMAL" variable.

=====================================================================


XML FILE OPERATIONS : UPDATE AND DELETE IN XML FILE



XML File Operations : Update a node in XML file

protected void UpdateXMLNode(string xmlFilePath)
        {
        if (File.Exists(xmlFilePath))
        {
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFilePath);
            string filterExpression = "NAME = '" + TextBox1.Text + "'";
            DataRow[] row = ds.Tables[0].Select(filterExpression);
         if (row.Length == 1)
         {
             row[0][0] = TextBox2.Text;
             row[0][1] = TextBox3.Text;
             row[0][2] = TextBox4.Text;
            ds.WriteXml(xmlFilePath);
          }
          }
          }


XML File Operations : Delete from XML file

protected void DeleteXMLNode(string xmlFilePath)
        {
        if (File.Exists(xmlFilePath))
        {
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFilePath);
            string filterExpression = "NAME = '" + TextBox4.Text + "'";
            DataRow[] row = ds.Tables[0].Select(filterExpression);
            if (row.Length == 1)
         {
            row[0].Delete();
            ds.WriteXml(xmlFilePath);
          }
          }
          }




HOW TO CREATE WEB CONFIG FILE IN ASP.NET



<?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=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
    </sectionGroup>
    </sectionGroup>
    </sectionGroup>
    </configSections>
    <appSettings/>
    <connectionStrings/>
    <system.web>
<!--
            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.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        </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>
        -->
        <pages>
            <controls>
            <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
             <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </controls>
        </pages>
        <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
         <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
        </httpHandlers>
        <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
    </system.web>
    <system.codedom>
     <compilers>
     <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
           <providerOption name="CompilerVersion" value="v3.5"/>
           <providerOption name="WarnAsError" value="false"/>
            </compiler>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
           <providerOption name="CompilerVersion" value="v3.5"/>
           <providerOption name="OptionInfer" value="true"/>
           <providerOption name="WarnAsError" value="false"/>
           </compiler>
           </compilers>
           </system.codedom>
<!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
-->
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <remove name="ScriptHandlerFactory"/>
            <remove name="ScriptHandlerFactoryAppServices"/>
            <remove name="ScriptResource"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.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=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
    </system.webServer>
    <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
     <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
     </dependentAssembly>
     <dependentAssembly>
     <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
     </dependentAssembly>
     </assemblyBinding>
     </runtime>
</configuration>