Getting SelectedValue from Wpf Combobox

Hi, i don't know why this seemed not easy but here is how it goes

Your XAML for the WPF Combo

<ComboBox Name="ddlApplication" DisplayMemberPath="AppName" SelectedValuePath="ID"  SelectedItem="App" Margin="92.709,56,220.46,0" VerticalAlignment="Top" Height="22.96" SelectionChanged="ddlApplication_SelectionChanged" />

Then you can easily bind a IList<YourObject> like this

  ddlApplication.ItemsSource = list;

            if (ddlApplication.HasItems)

            {

                ddlApplication.SelectedIndex = 0;

            }

and getting the selected value is easy as

 YourObject obj =  (YourObject)ddlApplication.SelectedItem;

            this.Title = obj .ID.ToString();

Done!  happy xamiling 

Posted by Adel Khalil with no comments
Filed under: , ,

Consuming JSON Web Service from jQuery and Server Code

This is a trick i learned today, if you have your web service returning JSON string to utilize on your ajax calls if you planned to connect to this service using server code (Web Reference) for internal facing app for example you have a problem.

If you are using HttpModule to alter the content-type to application/json you need to disabled this first, add web reference and then re-enable it again.

when trying to add web reference with Visual Studio you can’t get valid disco/wsdl files however if you added the reference while disabling the HttpModule and re-enable it again, it works.

Hope this helps.

Cross-domain JSONP with jQuery call step-by-step guide

I’ve been banging my head all day to accomplish this, it’s like a puzzle.. but since i get it to work i thought i could write this post for you and myself.

What we want to accomplish?

Simple way to communicate cross-domain with ASMX .NET 3.5 Web Service

How can we do it?

1. Implement a web service method like the following

   [ScriptService]
   public class JSONP_EndPoint : System.Web.Services.WebService
   {
       [WebMethod]
       [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
       public string Sum(string x,string y)
       {
           return x + y;
       }
   }

2. Add New class library with a name ContentTypeHttpModule

The reason for this is no matter how you specify the content-type of your ajax call ASP.NET send the request with Content-Type text/xml; charset=utf-8 this is security feature explained here by ScottGu 

3. Add the following code to your Class (Code by Jason i just did a simple modification)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace ContentTypeHttpModule
{
    public class ContentTypeHttpModule : IHttpModule
    {
        private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";

        #region IHttpModule Members
        public void Dispose()
        {
        }

        public void Init(HttpApplication app)
        {
            app.BeginRequest += OnBeginRequest;
            app.ReleaseRequestState += OnReleaseRequestState;
        }
        #endregion

        public void OnBeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpRequest resquest = app.Request;
            if (!resquest.Url.AbsolutePath.Contains("JSONP-EndPoint.asmx")) return;

            if (string.IsNullOrEmpty(app.Context.Request.ContentType))
            {
                app.Context.Request.ContentType = JSON_CONTENT_TYPE;
            }
        }

        public void OnReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpResponse response = app.Response;
            if (app.Context.Request.ContentType != JSON_CONTENT_TYPE) return;

            response.Filter = new JsonResponseFilter(response.Filter);
        }
    }

    public class JsonResponseFilter : Stream
    {
        private readonly Stream _responseStream;
        private long _position;

        public JsonResponseFilter(Stream responseStream)
        {
            _responseStream = responseStream;
        }

        public override bool CanRead { get { return true; } }

        public override bool CanSeek { get { return true; } }

        public override bool CanWrite { get { return true; } }

        public override long Length { get { return 0; } }

        public override long Position { get { return _position; } set { _position = value; } }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);
            strBuffer = AppendJsonpCallback(strBuffer, HttpContext.Current.Request);
            byte[] data = Encoding.UTF8.GetBytes(strBuffer);
            _responseStream.Write(data, 0, data.Length);
        }

        private string AppendJsonpCallback(string strBuffer, HttpRequest request)
        {
            return request.Params["callback"] +"(" + strBuffer + ");";
        }

        public override void Close()
        {
            _responseStream.Close();
        }

        public override void Flush()
        {
            _responseStream.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _responseStream.Seek(offset, origin);
        }

        public override void SetLength(long length)
        {
            _responseStream.SetLength(length);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _responseStream.Read(buffer, offset, count);
        }
    }
}

4. Register the HttpModule in the service project

4.1 Add referance to the HttpModule assembly to the service project

4.2 Add this code to web.config to register the module

<add name="ContentTypeHttpModule"
                    type="ContentTypeHttpModule.ContentTypeHttpModule, ContentTypeHttpModule" />

This goes under system.web / httpmodules section

5. Add a web project for testing the application

5.1 add the following libs

jquery-1.3.1.js

json2.js

5.2 add new script file caller.js

function test() {
    $.ajax({ url: "http://localhost:1690/JSONP-EndPoint.asmx/Sum",
    data: { x: JSON.stringify("Now i am getting jsop string"), y: JSON.stringify("2nd param") },
        dataType: "jsonp",
        success: function(json) {
            alert(json.d);
        },
        error: function() {
            alert("Hit error fn!");
        }
    });
}
5.3 Add referances to jquery-1.3.1.js and json2.js
5.4 Add Default.aspx page with input button that has onclick=”return test();”

6. Remarks

6.1 I use the JSON.stringify function to serialize the string data parameters.

6.2 .d is a security features on ASP.NET 3.5

Download the code

Subscribed to your own blog :D ?

Are you? Why ?

Posted by Adel Khalil with no comments
Filed under: ,

All my kids will program on Boku

One day, i will FORCE :D all my kids to program on Boku like Chris kids do.

http://devlicio.us/blogs/christopher_bennage/archive/2008/12/09/an-early-start.aspx

Posted by Adel Khalil with no comments

Full Time Software Engineers Needed

Just graduated with IT related degree ?

Are you creative, motivated and talented?

Excited by developing new applications that really make a difference?

Want to tackle new challenges?

kinematechs is hiring freshman developers

Great environment, great people…

apply jobs.kinematechs.com 

About Kinematechs®

Vision

Provide the framework and infra-structure for incubating new ideas and innovative projects in the field of Information and Computer Technology (ICT).

Mission

Provide creative and unconventional solutions to the most challenging ICT problems.

Develop software that implements these solutions .

Research and develop new technologies that will come to the benefit of the industry as a whole.

Values

Our people are our most important asset .

Quality is not a luxury, it is mandatory and must be reflected in everything we do.

We will always thrive to be creative and original in whatever we do.

Under all circumstances, Kinematechs® will always conduct business professionally and ethically.

Posted by Adel Khalil with no comments
Filed under: ,

Milk Offers and Notice Periods

Joel Spolsky talked in his recent post about exploding job offers (http://www.joelonsoftware.com/items/2008/11/26.html), where the recruiter says that you have to response to the offer with in fixed period of time, that was what happened to me exactly couple of month back, were the offer had expiration date of 4 days :)

Their execute is that they can fill the position with someone else, as soon as possible.

Another thing I've noticed is the notice period thingy that you found in most contracts, which says that before you decide to leave your job you should tell them 2 month prior.

Now there is two problems here first, if i come to the situation that i hate the job, my boss and everything about the company and rather to stay home jobless and not to wake up for the next day to go to work, i should feel that 2 month prior to it happening,

seconded, if i was hunting for a job and got an offer, which company will wait for me 2 months so i can join them, there is but very few.

last thing, what if I'm get laid off, would they give me even one month notice.

Posted by Adel Khalil with 1 comment(s)

Neat feature in Foxit Reader 2.3

So Foxit Reader is a replacement for Acrobat Reader, it’s light, can be stand alone, and has a super fast launching time and FREE. since i tried Foxit Reader i had no single Acrobat Reader installation.

Today i discovered a neat feature, if you closed Foxit Reader while you viewing PDF document, when you come back and open the same document it remembers the page you were reading not only that but the exact position of the scrolls so it will be arranged for the exact paragraph to resume your reading, very handy.

http://www.foxitsoftware.com/pdf/rd_intro.php

Posted by Adel Khalil with no comments
Filed under:

.NET got new logo

.NET got brand new logo, I kind liked the old one more… anyway…

http://blogs.msdn.com/msmossyblog/archive/2008/10/25/embrace-the-new-net-logo.aspx

Posted by Adel Khalil with no comments

TechCrunch started a layoffs counter

In respond to the latest economy crisis TechCrunch started a layoffs counter, Companies like eBay, Yahoo, nVidia and many more have layoffs exceeded 3000+ employee.

http://www.techcrunch.com/layoffs/

Is the current crisis affect Egypt’s outsource spots?

Posted by Adel Khalil with no comments
Filed under:

New Search Engine “Cuil” in Town

Mohamed Tanna refers me via Facebook link to this promising search engine, i love the search result layout by the way simple and brilliant.

http://www.cuil.com

Posted by Adel Khalil with no comments
Filed under:

Finally Microsoft Shipping Open Source

 

This is awesome, Microsoft finally decided to ship open source software, jQuery will be shipped with ASP.NET MVC and will be supported as any Microsoft product, also will be shipped with Visual Studio on the long run, this is a bold move

http://www.hanselman.com/blog/jQueryToShipWithASPNETMVCAndVisualStudio.aspx

http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx

Posted by Adel Khalil with 1 comment(s)

Windows Live Messenger 2009 Build 14.xx.xx.xx

WLM 2009 leaked, if you tried to install it on Windows Server 2008 it may not work and return error message saying that the service is no available, if you do so and couldn’t get rid of the new installation remove the bits manually and use Windows Installer Cleanup tool to get rid of the entry on the Windows Installer log. 

Google Mobile Platform: Android

The Android platform is a software stack for mobile devices including an operating system, middleware and key applications. Developers can create applications for the platform using the Android SDK. Applications are written using the Java programming language and run on Dalvik, a custom virtual machine designed for embedded use, which runs on top of a Linux kernel.

There is a SDK at Andriod Website the two features that interest me is how application is created equal so you can create your own dialer for example and replace the existing one, as well as the multitasking nature of the platform.

More Posts Next page »