Thursday, August 26, 2010

Click Once Deployment Technique in Visual Studio

Click Once Deployment Technique in Visual Studio

Introduction

Deployment of Windows Application has always been a not-so-easy task. We had to create Deployment Project, ship it via some media and then it had to be installed to make the application available to use. Moreover, maintaining or managing updates and patches were even more challenging. ClickOnce technology introduced with .NET Framework 2.0 takes care of all the challenges of conventional Windows Application Deployment Issues. Let us get into the core of it.


Key Features of ClickOnce Technology

  1. We can develop Windows forms application and by using the Publish option to deploy the application onto any of the following locations: File System, Local Web Server, FTP Site, or a Remote Web Site.

  2. Once the application is deployed onto the target location, the users of the application can browse to the publish.htm file using any web browser and install the application onto their machine. Note that publish.htm file is the entry point for installing the application.

  3. Once the user has installed the application, a shortcut icon will be added to the Start Menu and also the application will be listed in the Control Panel/Add Remove Programs.

  4. When the user launches the application again, the manifest will contain all the information to decide if the application should go to the source location and check for updates to the original application. Let us say, for instance, a newer version of the application is available, it will be automatically downloaded and made available to the user. Note that when the new version is downloaded, it is performed in a transacted manner meaning that either the entire update is downloaded or nothing is downloaded. This will ensure that the application integrity is preserved.

How It Works


Let us start with a Simple WinForm Application. I have created a Windows Forms Application in C# named ClickOnceDeploymentDemo with one form with a button and added a simple code to display the assembly path with the button click event. Refer to the following screen shots:




When you execute the project, you will see something like the following screen shot:



Now right click the project and click on Properties to get the Property window and then click on Publish Tab for making the settings for deployment. Refer to the following screenshot. I have put in the publish location as a web server path, that can be accessed via web browser using HTTP. Also one can select the availability of application, whether online or offline mode and so on and so forth.


There are additional settings like the Prerequisites for the application to work. By default .NET Framework is selected. One can select additional components that are required to run the application as pre-requisite. Refer to the following screenshot:

Below the Prerequisite button, there is an Updates button for specifying the checking of application updates.

Then Click on Options button to set the Publish Options in terms of language and the web page to get access of the deployment. Refer to the following screenshot:



Once all the necessary settings are done, we can go ahead and simply Publish the application. Right click the Project and click on Publish button and follow the screen instructions for deployment. Refer to the following screen shots:









Clicking Finish will Publish the application for availability over the net. Now you can browse the web folder to check the files and folders that are published.



The following screen shows how to access the published application for installation / deployment.



Click on Install button to install the application:



You will also notice the following changes as a result of the installation:

An icon is added to the Start menu.
An entry is added in Control Panel -> Add/Remove Programs
From this point onwards, you can launch the application through the shortcut in the Start -> Programs menu.
From Add/Remove Programs, you can either completely uninstall the application or revert to the previous version, if there was one.

With every deployment or update over the existing deployment, the same shall be downloaded by the client machine once the connection is available to the server and the client installation gets updated automatically. The client installation checks updates once the application starts.

Conclusion

So, by now we learnt how to use ClickOnce technology to deploy our Win Application and make the same available over the net and how to manage the updates. This was a very simple example.



How to add a message box in C# Asp.net application ?

In .aspx,

private void MessageBox(string msg)
{
Label lbl = new Label();
lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
Page.Controls.Add(lbl);
}

and u can call

MessageBox("try again and enter correct key....");

Monday, August 23, 2010

How to use AutoCompleteExtender in dotnet page.

Here is the solution :



@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="ajaxToolkit"



Which says that the reference assembly "AjaxControlToolkit.dll" should be registered on the page by using above method. We should also have tagprefix="ajaxToolkit" which is similar to .

In this article I have very simple database table called as "Tbl_Countries" with fields as:

CountryId int primary key,
CountryName varchar(50)

I am using a web service called as "AutoComplete.asmx" whose code behind gets automatically added to the "App_Code" folder.

Note: Very important part is you need to give a reference of "AjaxControlToolkit.dll".

Here is complete code for "AutoComplete.asms.cs".





using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

///

/// Summary description for AutoComplete

///

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

public class AutoComplete : System.Web.Services.WebService

{

[WebMethod]

public string[] GetCountriesList(string prefixText)

{

DataSet dtst = new DataSet();

SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

string strSql = "SELECT CountryName FROM Tbl_Countries WHERE CountryName LIKE '" + prefixText +"%' ";

SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);

sqlCon.Open();

SqlDataAdapter sqlAdpt = new SqlDataAdapter();

sqlAdpt.SelectCommand = sqlComd;

sqlAdpt.Fill(dtst);

string[] cntName = new string[dtst.Tables[0].Rows.Count];

int i = 0;

try

{

foreach (DataRow rdr in dtst.Tables[0].Rows)

{

cntName.SetValue(rdr["CountryName"].ToString(), i);

i++;

}

}

catch { }

finally

{

sqlCon.Close();

}

return cntName;

}

}

Let us take another page called as "default.aspx" in which I am having a tag and in which a tag in which you can specify path of webservices. You have already registered Assembly="AjaxControlToolkit" on this page so you can use that.


So my entire code will look something like this

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="ajaxToolkit"%>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Pagetitle>

head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">

<Services>

<asp:ServiceReference Path="AutoComplete.asmx" />

Services>

asp:ScriptManager>

<div>

<asp:TextBox ID="txtCountry" runat="server">asp:TextBox>

<ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoComplete.asmx"ServiceMethod="GetCountriesList" MinimumPrefixLength="1" EnableCaching="true" />

div>

form>

body>

html>



If you are using the contextKey in Autocomplete, then you should include that in the method like this :


public string[] GetCountriesList(string prefixText, int count, string

contextKey)


contextKey is useful when you have another parameter according to which the text box should filter the data. For Example : a Dropdownlist.


In this case, you can add the following code in the SelectedIndexChanged event of the DropDownList1.



protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)

{

autoComplete1.ContextKey = DropDownList1.SelectedValue;

}



and more over you can include it in the pageload like :


protected void Page_Load(Object sender, EventArgs e)
{
autoComplete1.ContextKey = DropDownList1.SelectedValue;
}