Thursday 31 October 2013

Use Of String.Format in Different Scenario

Example of String.Format


 How to use with Date


D_O_B=12/04/2013 11:22:33String.Format("{0:d}",Eval("D_O_B"))


<asp:TextBox ID="TextBox8" Text='<%#String.Format("{0:d}",Eval("D_O_B")) %>' Width="80px" runat="server"></asp:TextBox>

output : 12/04/2013

How to use with Time                                      

protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = string.Format("{0:t}"DateTime.Now);
    }

output : 4:15 PM

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



 Put the double Zero after the Point(.);int chek=2525;String.Format("{0:00}",chek)


output :  2525.00

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

Add The Double Zero After Point                                                                            


label76.Text=string.Format("{0:N2}",Math.Round(Convert.ToDouble(label76.Text),0));

 Output:  125.00

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

Put the comma( , ) between number
int iii=25252252;string.Format("{0:,###,###,###,##0.00}", iii);

 int iii = Convert.ToInt32(dataGridView1.Rows[b].Cells[1].Value.ToString());
                    string ssss = string.Format("{0:,###,###,###,##0.00}", iii);

output : 25,252,252.00


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

Another Solution



Aspx page

  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                     <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"
                         ontextchanged="TextBox1_TextChanged" ></asp:TextBox>

              <asp:Image ID="Image1" runat="server" ImageUrl="~/cal.png" Width="20px" Height="25px" />

        <asp:CalendarExtender ID="CalendarExtender1" PopupButtonID="Image1" Format="MM/dd/yyyy"TargetControlID="TextBox1" runat="server">
        </asp:CalendarExtender>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ContentTemplate>
        </asp:UpdatePanel>
       
       <br /><br /><br />
    <asp:label ID="Label1" runat="server" text="Label"></asp:label><br /><br /><br />
<asp:button ID="Button1" runat="server" text="Button" onclick="Button1_Click" />
      
       

    </div>
    </form>
</body>
</html>

Aspx.cs page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = string.Format("{0:dd/MM/yy}"DateTime.Now);
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox2.Text = TextBox1.Text;
        DateTime st = Convert.ToDateTime(TextBox1.Text);
        string sss = string.Format("{0:dd/MM/yyyy}", st);
        TextBox1.Text = sss;
    }

}

Tuesday 29 October 2013

The installer was interrupted before application could be installed. You need to restart installer to try again


The installer was interrupted before application could be installed. You need to restart installer to try again”.

To solve this problem I made some of changes in my system.

Follow below steps to solve this problem

Step1: Go to Control Panel ---> Programs ---> Uninstall a Program ---> Turn Windows features on or off
 
Step 2: After click on Turn Windows features on or off option one new window will open in that select Internet Information Service ---> Web Management Tools ---> IIS 6 Management Compatibility ---> in that select IIS Metabase and IIS6 configuration compatibility

Once you select IIS Metabase and IIS 6 configuration compatibility click OK.

How to Convert Different Timezone values into Indian Time using C#.net


 If you are developing an application,which captures timings from your local machines, and your clients are from different time zones, then if you are using DateTime.now() function you will get the current time in that area only,But if your server is following IST,you need to convert the time zones into Indian Standard timings.

So here is the code..

            DateTime crnttime = DateTime.UtcNow.Addhour(5.5);
           

First we will get the current time from local machine and we will declare a TimeZoneInfo object, and there we will set the "India Standard Time" and convert it into IST as per the third line.
For TimeZoneInfo library you need  .Net framework 3.5 and above.
If you want time in 24 hour format you have to convert time into string as per the third line code.
          string time = IndianTme.ToShortDateString();
If you want to display only Date you can use the above code.
         string time = IndianTme.ToShortTimeString();
This code for displaying time only.

            System.TimeZone t1 = System.TimeZone.CurrentTimeZone;
            string timez = t1.StandardName;
For displaying current timezone name you can use above code.

Getting the client browser's date and time with ASP.NET


In order to provide relevant content to a site visitor, I wanted to find out what time of day it was wherever the visitor was located.  This would be useful to serve different content based on if its morning ("Buy Coffee!!"), lunch ("Eat lunch near you!!"), or evening ("Come rent a movie from us!!")
Using a similar approach to how viewstate is maintained during postbacks, I added a couple of methods to my base page implementation and a read-only property which returns the value of hidden input.  When the base page's load method fires, it registers a hidden input and a small script that will set the input's value equal to the date of the client browser during a postback.
public class ClientTimeBasePage : Page
{

    private const string CLIENTTIME_SCRIPT_ID = "__CLIENTTIMEJS";
    private const string CLIENTTIME_FIELD = "__CLIENTTIME";

    public string ClientTime
    {
        get { return this.Request.Form[CLIENTTIME_FIELD]; }
    }

    protected override void OnLoad(EventArgs e)
    {

        ClientScript.RegisterHiddenField(CLIENTTIME_FIELD, "");
        if(!ClientScript.IsOnSubmitStatementRegistered(typeof(string), CLIENTTIME_SCRIPT_ID))
        {
            ClientScript.RegisterOnSubmitStatement(typeof(string), CLIENTTIME_SCRIPT_ID, "document.getElementById('" + CLIENTTIME_FIELD + "').value=new Date();");

        }  
        base.OnLoad(e);

    }
}
At this point, I should mention I have not tested this thoroughly but I'm pretty sure I'll run into problems when we get into different culture and locale settings across browsers.  A few other considerations that come to mind is using AJAX for a similar implementation and possibly attaching the client date / time to the Request context.  This seems like a more appropriate place for it - perhaps using extension methods in C# 3.0.  Another option is to write some more intricate javascript to perform a little more parsing on the client-side.  We should always be careful when we rely on the client's browser for input data.

Monday 28 October 2013

CREATE SETUP OF WEB APPLICATION STEP BY STEP WITH VISUAL STUDIO 2010


start with an empty instance of Visual Studio and create a new VS 2010 Web Application project (select File->New Project->ASP.NET Web Application).  For the purposes of this simple sample we’ll have two pages in the project:
We’ll add a label to the Default.aspx page and a Page_Load event handler in the code-behind to output the current timestamp on each request. When I press F5 to build and run the application, the project will compile and run as I’d expect (and by default use the built-in VS Web Server):

2) Add a VS 2010 Web Setup Project to the Solution

Now that we have a simple ASP.NET application built, we’ll want to add a VS 2005 Web Setup Project to the solution. Choose the File->Add->New Project menu item to add one into your solution:
Note that the “Web Setup Project” type shows up under the “Other Project Types->Setup and Deployment” node in the New Project dialog above. Name it whatever you want and hit ok. It will then show up in your solution explorer as a separate project.
Our next step will be to configure the web setup project to take the compiled assemblies (\bin directory contents) + content markup (.aspx, .config, etc files) from our Web Application Project and use them as inputs within our setup project. To-do this, right-click on the web setup project node in the solution explorer and choose the “Add->Project Output” context menu item:
A dialog will then appear allowing us to select which project in the solution, and which of its project contents, we want to add to the setup package:
For ASP.NET Web Application Projects it is really important that we select both the “Primary Output” (which are the compiled assemblies for the \bin directory) as well as the “Content Files” (which are the .aspx markup files) within this dialog.
By default, the web setup project will copy both of these items into the root of the target Web Application Folder that the setup project will create. You can see that it is configured this way by opening up the “File System” view within the web setup project (right click on the web setup project root and choose View->File System):
This actually isn’t what we want to have happen though – since we really want the assemblies (indicated by the Primary Output node) to be copied into the application’s \bin directory instead (otherwise ASP.NET won’t be able to find them at runtime). To fix this, drag/drop the “Primary Output from MyApplication” node into the \bin directory. Once this is done you should be able to click on the “Web Application Folder” node on the left-hand side and see this:
And then click on the “bin” folder sub-node and see this:
We now have a basic web setup project created and configured for our ASP.NET Web Application. Next step is to build and run it.

3) Build and Run the VS 2010 Web Setup Project to the Solution

To build the web-setup project we can right-click on the web setup project node within the solution explorer and choose the “Build” option:
If you open the output window within VS (View->Output menu item), you will see the results of this build operation:
Our “MyApplicationSetup” project created a new MyApplicationSetup.msi Windows installer file and compressed and packaged the contents of our ASP.NET Web Application (note: in the web setup project properties dialog you can choose whether the compression algorithm used is optimized for size or speed).
Very Important: Because setup projects take awhile to build, they are by default marked not to build as part of the solution.  What this means is that you need to right-click on them and explicitly do a build in order for them to be recompiled.  Be careful to-do this when you make and test changes - otherwise you'll be running the previously compiled version and not the one with your latest code!
To test it, we can right-click on the web setup project within the solution explorer and choose the “Install” option to install it (or alternatively launch it outside of VS by running it):
This will launch a standard Windows installer and walk the user through installing the application on IIS:
VS 2005’s web setup projects allow you to pick which site to install the application on if multiple sites are configured on IIS (this wasn’t supported with the VS 2003 version). You can optionally specify an application virtual-directory path to use (for example: http://www.myserver.com/myapppath), or you can leave this value blank to install it as the root application on the site (for example: http://www.myserver.com/).
Once the installer completes, the application will have been copied to disk and registered with IIS. We can now run the application using the HTTP path we provided during installation like so:
Once installed the application will also show up in the standard “Add or Remove Programs” utility within the Windows Control Panel:
You can remove the application either by running uninstall from the control panel utility, or (at development time) by right-clicking on the web setup project node within the VS Solution Explorer and selecting the “Uninstall” menu item. This will cause all installed files to be removed from disk.

4) Update the Wizard UI of the Web Setup Project

By default the Windows installer created by a web setup project has some default instruction strings and banner images for the setup:
You can change this and customize the screens by right-clicking on the web setup project node in the VS solution explorer and selecting the "View->User Interface" context menu item):
This will then bring up a screen that shows the list of screens to be displayed during setup:
Unfortunately there isn't a forms-designer that you can use to override the screens above.  However, you can select a screen, and then go to the property grid to customize its text and change the graphics used within the screen:
You can also create new screens and add them into the setup wizard.  Later in this tutorial we'll use this feature to create a custom screen to collect database connection-string information and use it to automate configuring our web.config file to point at the appropriate database.

5) Adding Custom Actions to the VS 2010 Web Setup Project

Web Setup Projects contain built-in support for configuring and performing common setup actions. These include editors for adding/changing registry entries (choose View->Register to configure), changing file-type associations (View->File Types), and for validating prerequisite components are already installed (it automatically checks that the .NET Framework 2.0 redist is installed). Setup Projects also allow you to configure a number of common IIS settings declaratively (click on the “Web Application Folder” in the File System view and then look at the property grid to see these):
But for non-trivial setups you are likely to want to be able to execute your own custom code during setup to customize things. The good news is that web setup projects support this with something called “Custom Actions” – which is code you write that can execute during both install and uninstall operations.
To add a custom action you first want to add a new class library project to your solution (File->Add->New Project->Class Library). 
You then want to add assembly references in this newly created Class Library to the System.Configuration.Install.dll, System.Configuration.dll, System.Diagnostics.dll, and System.Web.dll assemblies. You’ll then want to create a new class for your custom action and have it derive from the “System.Configuration.Install.Installer” base class like so:
using System;
using 
System.Configuration.Install;
using 
System.ComponentModel;

namespace 
MyCustomAction
{
    [RunInstaller(
true)]
    
public class ScottSetupAction : Installer
    {
        
public override void Install(System.Collections.IDictionary stateSaver)
        {
            
base.Install(stateSaver);

            
// Todo: Write Your Custom Install Logic Here
        
}
    }
}
Notice the custom “RunInstaller(true)” attribute that must be set on the class name. This is important and required (and easy to forget!). You’ll need to add a using statement to the System.ComponentModel namespace to avoid fully qualifying this.
Next we’ll need to make sure this Custom Action assembly gets added to our web setup project. To-do this, right-click on the Web Setup Project root node in the solution explorer and select the View->File System menu item to bring up the file-system editor. Right-click on the “bin” sub-folder and choose “Add->Project Output” like we did earlier to get the custom action assembly added to the web setup project:
In this case we’ll want to select the Custom Action Class Library project instead of our web application one. Pick it from the project drop-down at the top of the dialog and then select the “Primary Output” option as the piece we want to add to the web-setup project (this will cause the Custom Action assembly to get added):
Lastly, we’ll configure the web-setup project to call our custom action assembly during the install phase of setup. To do this we’ll right-click on the web setup project root node in the solution explorer and choose the “View->Custom Actions” menu item. This will then bring up the Custom Actions Editor. Right-click on the “Install” node and choose “Add Custom Action”:
Drill into the Web Application Folder and Bin directory and choose the output from our Custom Action we imported:
The Setup Project will then automatically detect the custom action because of the “RunInstaller” attribute:
Our custom action class and Install method will now run anytime we run the installation setup program.

6) Useful Custom Action Example: ASP.NET Script Mapping Checker

The previous section showed how to create and configure an empty custom action class and install method. Let’s now do something useful with it. Specifically, let’s add code to verify that the right version of ASP.NET is correctly mapped for the application we are creating.
Because ASP.NET V1.1 and V2.0 can run side-by-side with each other on the same machine, it is possible to have different parts of a web server configured to run using different versions of ASP.NET. By default, the versions inherit hierarchically – meaning if the root application on a site is configured to still run using ASP.NET V1.1, a newly created application underneath the site root will by default run using V1.1 as well. What we’ll do in the steps below is write some code to ensure that our new application always runs using ASP.NET 2.0.
To begin with, we’ll select our custom action within the Custom Action explorer (just like in the previous screenshot above - using the View->Custom Action context menu item). We’ll then go to the property grid and specify a few parameters to pass to our custom action to use:
Specifically, we’ll pass in the target directory that the application is being installed in, the IIS site map path, and the IIS virtual directory name that the user specified in the setup wizard. This string of values looks like this:
/targetdir="[TARGETDIR]\" /targetvdir="[TARGETVDIR]" /targetsite="[TARGETSITE]"
We’ll then update our custom action to access these values and do something with them like so:
using System;
using 
System.Configuration;
using 
System.Configuration.Install;
using 
System.ComponentModel;
using 
System.Diagnostics;
using 
System.IO;

namespace 
MyCustomAction
{
    [RunInstaller(
true)]
    
public class ScottSetupAction : Installer
    {

        public override void 
Install(System.Collections.IDictionary stateSaver)
        {
            
base.Install(stateSaver);

            
// Retrieve configuration settings
            
string targetSite Context.Parameters["targetsite"];
            string 
targetVDir Context.Parameters["targetvdir"];
            string 
targetDirectory Context.Parameters["targetdir"];

            if 
(targetSite == null)
                
throw new InstallException("IIS Site Name Not Specified!");

            if 
(targetSite.StartsWith("/LM/"))
                targetSite 
targetSite.Substring(4);

            
RegisterScriptMaps(targetSite, targetVDir);
        
}

        
void RegisterScriptMaps(string targetSite, string targetVDir)
        {
            
// Calculate Windows path
            
string sysRoot System.Environment.GetEnvironmentVariable("SystemRoot");

            
// Launch aspnet_regiis.exe utility to configure mappings
            
ProcessStartInfo info = new ProcessStartInfo();
            
info.FileName Path.Combine(sysRoot, @"Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe");
            
info.Arguments = string.Format("-s {0}/ROOT/{1}", targetSite, targetVDir);
            
info.CreateNoWindow = true;
            
info.UseShellExecute = false;

            
Process.Start(info);
        
}
    }
}
The above code launches the aspnet_regiis.exe utility that ships with ASP.NET within the \Windows\Microsoft.net\framework\v2.0.5.0727\ directory, and passes in the path location information for the site that the web setup installer previously created, along with the “-s” flag – which indicates that the IIS script-maps for that application should be updated to specifically use the ASP.NET 2.0 version, and not inherit the version number from any parent applications.
A special thanks to John for figuring this out in his blog post here.
Note: If you are using IIS6 or IIS7, you'll probably want to also add some logic into the custom action to ensure that the application pool that the application is being hosted in is also mapped to use ASP.NET 2.0.  Either that or you'll want to tell the admin to manually check the application pool settings after the setup is complete.

7) Useful Custom Action Example: Configuring Database Connection String

For our next custom action example, let’s add some UI to the setup that allows a user to configure the connection string details of a database the application should use.
Right click on the web setup project and open up the user interface screens again:
Right click on the "Install" node on the user interface screens page and chose to add a new dialog to the install wizard:
Chose one of the TextBox screens to use for gathering connection string details from the user:
Right-click on the TextBox screen node and move it up to be earlier in the wizard (right after we pick the IIS site and application name to use):
Then click on the TextBox screen and access its property window.  Via the property window you can change the text displayed on the screen, as well as control how many textboxes are visible:
Note in the above property window how I've set the Edit2, Edit3 and Edit4 text boxes to not be visible.  Now when we build and run the setup package we'll see this dialog in our wizard steps:
Now that we have UI to capture the connection-string value entered by a user in the wizard, we want to make sure it is passed to our custom action class.  You can do this by right-clicking on the web setup project node and by then choosing the "View->Custom Actions" context menu and then opening the property page window of our custom action:
We'll want to update the CustomActionData property value and pass in the connection-string of the database to use (we'll pass in the value from the EDITA1 textbox in the user interface screen):
/targetdir="[TARGETDIR]\" /db="[EDITA1]" /targetvdir="[TARGETVDIR]" /targetsite="[TARGETSITE]"
We can then update our custom action class to retrieve and use this connectionstring value to update the web.config file of the new application to contain the value the user installing the application entered. Below is a method that opens the web.config file for our new application and programmatically updates it with the user entered connection string:
void ConfigureDatabase(string targetSite, string targetVDir, string connectionString)
{
    
// Retrieve "Friendly Site Name" from IIS for TargetSite
    
DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/" + targetSite);
    string 
friendlySiteName entry.Properties["ServerComment"].Value.ToString();

    
// Open Application's Web.Config
    
Configuration config WebConfigurationManager.OpenWebConfiguration("/" + targetVDir, friendlySiteName);

    
// Add new connection string setting for web.config
    
ConnectionStringSettings appDatabase = new ConnectionStringSettings();
    
appDatabase.Name DATABASE_CONNECTION_KEY;
    
appDatabase.ConnectionString connectionString;

    
config.ConnectionStrings.ConnectionStrings.Clear();
    
config.ConnectionStrings.ConnectionStrings.Add(appDatabase);

    
// Persist web.config settings
    
config.Save();}
And now after we run the setup program our newly installed ASP.NET application's web.config file will have been updated to point to the right database.