Tuesday 19 November 2013

Calling Online web service using ASP.NET


First Step: Create Web Site project
1. To create new Web Site project, choose New from File menu, then choose Web Site as shown below.
<formulas></formulas>

2. Choose ASP.NET Web Site. Name the project and click OK
Second Step: Add web Reference
After creating the Web Site project, it’s time to add a web reference for our web service.
1. In the solution explorer, right click the project node, choose Add Web Reference
2. A new window with Add Web Reference title will be opened.
In the URL field, insert the URL for the web service. In this tutorial as I mentioned before I’ll use the test published web services form Extentrix. “Extentrix Web Services 2.0 – Application Edition”
After clicking the Go button you will see the web services APIs.
3. Set a name for your web service reference in the web reference name field and click Add Reference
Third Step: Call the web services APIs inside the code
After a successful adding to the web service, now we are ready to call the web services APIs inside our project.
1. First we need to add the added web reference to our class.
“ExtentrixWS” is the name of the added web service from the previous step.
 using ExtentrixWS;  
2. Create a proxy object for our added web service reference, where the ExtentrixWebServicesForCPS is the name of the Web Services
      //define a web service proxy object.
    private ExtentrixWS.ExtentrixWebServicesForCPS proxy;

3. As I explained before we need credentials to pass to Citrix Presentation Server, we will pass these credentials through the web services APIs
     //define a Citrix Presentation Server Credentials object
      private Credentials credentials;

Initialize the proxy and the credentials objects
    //intialaize objects
     proxy = new ExtentrixWebServicesForCPS();
     credentials = new Credentials();

4. Set the values for Citrix credentials. I set the credentials values for the test of Extentrix Web Service.
//set credentials
//these values are according to Citrix testdrive presentation server
//for which Extentrix published a web service for delovepers to use it
//as a test web service.
      credentials.Password = "demo";
      credentials.UserName = "citrixdesktop";
      credentials.Domain = "testdrive";
    
   
//because it is a sample,we will use no encryption method.
//so the password will be sent as a clear text.
      credentials.PasswordEncryptionMethod = 0;

//set the domain type to windows domain
      credentials.DomainType = 0;



Now we can call any web services available as simple as calling any ordinary function.
5. Call the GetApplicationsByCredentialsEx web service. This web service takes the following parameters:
  • Credentials: Citrix Credential to access Citrix Presentation Server Farm.
  • Client Name: pass your machine name
  • Client IP: pass your machine IP
  • Desired Details : what the details you asked for
  • Server Types: pass “all”
  • Client Types: pass “all”
Am not going to explain Extentrix web services APIs, if you are interested you can go tohttp://www.extentrix.com/Web%20Services/Index.htm and look for it.
This API returns an array of ApplicationItemEx, this class will be built for you once you add the web reference.
This class contains the published application properties. I used this web service to get all the published applications, and then I created an ImageButton for each application.
         // 1) Get all the published applications list by calling GetApplicationsByCredentialsEx web service.
         // 2) create an ImageButton for each application
         // 3) Create Image for the application
         // 4) Add it to the AppList panel.
         // 5) Set the event handler for each ImageButton, so when clicking it the associated application will run
    

       
         //calling the web service
          ApplicationItemEx[] items = proxy.GetApplicationsByCredentialsEx(credentials, Request.UserHostName,
         Request.UserHostAddress, new  string[] { "icon","icon-info"}, new string[]{ "all" },
         new string[] { "all"});
         
    //loop for each published application
    for (int i = 0; i < items.Length; i++) {
           //create the ImageButton
          System.Web.UI.WebControls.ImageButton app = new System.Web.UI.WebControls.ImageButton();


    //set the Image URL to the created image
           app.ImageUrl = createIcon(items[i].InternalName,items[i].Icon);
    
          
    //set the ToolTip to the name of the published application
           app.ToolTip = items[i].InternalName;
    


     //add the ImageButton to the AppList panel
           AppList.Controls.Add(app);
    

           //set the event handler for the ImageButton.
           app.Click += new
    System.Web.UI.ImageClickEventHandler(this.OnApplicationClicked);

            }



Finally another example in calling web service is to launch the published application.
In this example, in the event handler of the applications ImageButtons I launch the clicked application.
I get the ICA file content by calling LaunchApplication web service.
Then I write the ICA file content to the response to launch the application.
    private
        void OnApplicationClicked (object
        sender, System.EventArgs e)
    {     
     ServicePointManager.Expect100Continue = false;
       
        // Get the event source object.     
          System.Web.UI.WebControls.ImageButton app = (System.Web.UI.WebControls.ImageButton)sender;
         
                
        //Get the file ICAfile content by calling LaunchApplication web service.        
        string ica = proxy.LaunchApplication(app.ToolTip, credentials, Request.UserHostName, Request.UserHostAddress);

        
        //Set the response content type to "application/x-ica" to run the file.       
        Response.ContentType = "application/x-ica";
           
        
        //Run the application by writing the file content to the response.
            Response.BinaryWrite(Response.ContentEncoding.GetBytes(ica</place /></city />) );
     Response.End();
    }


Thursday 14 November 2013

Update table using joins in sql server 2008

This is one of the most interesting questions I keep on getting on this email and I find that not everyone knows about it. In recent times I have seen a developer writing a cursor to update a table. When asked the reason was he had no idea how to use multiple tables with the help of the JOIN clause in the UPDATE statement.
Let us see the following example. We have two tables Table 1 and Table 2.
-- Create table1CREATE TABLE Table1 (Col1 INTCol2 INTCol3 VARCHAR(100))INSERT INTO Table1 (Col1Col2Col3)SELECT 111'First'UNION ALLSELECT 1112'Second'UNION ALLSELECT 2113'Third'UNION ALLSELECT 3114'Fourth'GO-- Create table2CREATE TABLE Table2 (Col1 INTCol2 INTCol3 VARCHAR(100))INSERT INTO Table2 (Col1Col2Col3)SELECT 121'Two-One'UNION ALLSELECT 1122'Two-Two'UNION ALLSELECT 2123'Two-Three'UNION ALLSELECT 3124'Two-Four'GO
Now let us check the content in the table.
SELECT *FROM Table1SELECT *FROM Table2
GO
Now let us see the following image. Our requirement is that we have Table2 which has two rows where Col1 is 21 and 31. We want to update the value from Table2 to Table1 for the rows where Col1 is 21 and 31. Additionally, we want to update the values of Col2 and Col3 only.
When you look at this it looks very simple but when we try to think the solution, I have seen developers coming up with many different solutions for example sometime they write cursor, table variables, local variables etc. However, the easiest and the most clean way is to use JOIN clause in the UPDATE statement and use multiple tables in the UPDATE statement and do the task.
UPDATE Table1SET Col2 t2.Col2,Col3 t2.Col3FROM Table1 t1INNER JOIN Table2 t2 ON t1.Col1 t2.Col1WHERE t1.Col1 IN (2131)GO
Now let us select the data from these tables.
-- Check the content of the tableSELECT *FROM Table1SELECT *FROM Table2
GO
As you can see that using JOIN clause in UPDATE statement it makes it very easy to update data in one table from another table. You can additionally use MERGE statement to do the same as well, however I personally prefer this method. Let us clean up the clause by dropping the tables which we have created.
DROP TABLE Table1DROP TABLE Table2
GO
Do let me know if you use any other trick in similar situations. If you do, I would like to learn more about it.

Monday 11 November 2013

Select only one RadioButton in gridview in asp.net using javascript




<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
</style>

.......................................................................
<script language="javascript" type="text/javascript">
function SelectSingleRadiobutton(rdbtnid) {
var rdBtn = document.getElementById(rdbtnid);
var rdBtnList = document.getElementsByTagName("input");
for (i = 0; i < rdBtnList.length; i++) {
if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id)
{
rdBtnList[i].checked = false;
}
}
}
</script>

..............................................................................
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false"DataKeyNames="UserId" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton id="rdbUser" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="Name"/>
<asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>




protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
SqlConnection con = new SqlConnection("Data Source=MYSystem;Initial Catalog=MySamplesDB;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select UserId,UserName,FirstName,LastName,Location from User_Information", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvdata.DataSource = ds;
gvdata.DataBind();
con.Close();
}