tag:blogger.com,1999:blog-80639585734551413452024-03-13T09:43:45.241-07:00Reference ManualKAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.comBlogger45125truetag:blogger.com,1999:blog-8063958573455141345.post-60042400957467126952023-06-21T12:48:00.006-07:002023-07-06T07:23:41.346-07:00Apache Spark Installation on Azure Ubuntu VM<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Installation of Apache Spark on Ubuntu VM Server on Azure</h2>
<h3 style="text-align: left;">
Create VMs</h3>
<div>
First step is to create VMs on Azure, I am not going into the details of that. Once done, please try to remember the User ID and password.</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Installation of Putty</h3>
<div>
In order to login to the box, we need to install the putty for windows, i used the link below</div>
<div>
<br /></div>
<div>
http://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html</div>
<div>
<br /></div>
<div>
After installation, open putty and enter the public IP address as below and it will open a screen where it will ask the username and password. I have not set up password less authentication yet on the VMs</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixGmLO5DWTf2h6XnxELHnCr7YZmaTavQ9faDO4KL734eNamYAFKGJ7S3nOemHSpca2FDnIjpMtMJhvpptHZjqccw3v4RZd8DmrwK9wKniPMK2jxQ3y-N6Qq_R0JMhLTSpQ_HR_9DoTU6I/s1600/putty.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="284" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixGmLO5DWTf2h6XnxELHnCr7YZmaTavQ9faDO4KL734eNamYAFKGJ7S3nOemHSpca2FDnIjpMtMJhvpptHZjqccw3v4RZd8DmrwK9wKniPMK2jxQ3y-N6Qq_R0JMhLTSpQ_HR_9DoTU6I/s320/putty.PNG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h3 style="clear: both; text-align: left;">
Download Spark into desired folder</h3>
<div>
For that we need to first create the folder where we want the binary download of spark to get downloaded</div>
<div>
<br /></div>
<div>
<div>
login as: plasparkadmin</div>
<div>
plasparkadmin@13.65.203.139's password:</div>
<div>
plasparkadmin@13.65.203.139's password:</div>
<div>
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-72-generic x86_64)</div>
<div>
<br /></div>
<div>
* Documentation: https://help.ubuntu.com/</div>
<div>
<br /></div>
<div>
System information as of Tue Apr 11 18:55:11 UTC 2017</div>
<div>
<br /></div>
<div>
System load: 0.24 Memory usage: 0% Processes: 87</div>
<div>
Usage of /: 40.9% of 1.94GB Swap usage: 0% Users logged in: 0</div>
<div>
<br /></div>
<div>
Graph this data and manage this system at:</div>
<div>
https://landscape.canonical.com/</div>
<div>
<br /></div>
<div>
Get cloud support with Ubuntu Advantage Cloud Guest:</div>
<div>
http://www.ubuntu.com/business/services/cloud</div>
<div>
<br /></div>
<div>
0 packages can be updated.</div>
<div>
0 updates are security updates.</div>
<div>
<br /></div>
<div>
Your Hardware Enablement Stack (HWE) is supported until April 2019.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
The programs included with the Ubuntu system are free software;</div>
<div>
the exact distribution terms for each program are described in the</div>
<div>
individual files in /usr/share/doc/*/copyright.</div>
<div>
<br /></div>
<div>
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by</div>
<div>
applicable law.</div>
<div>
<br /></div>
<div>
plasparkadmin@pal-dev-vm-01:~$ cd ..</div>
<div>
plasparkadmin@pal-dev-vm-01:/home$ ls</div>
<div>
plasparkadmin</div>
<div>
plasparkadmin@pal-dev-vm-01:/home$ cd plasparkadmin/</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ mkdir work</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ ls</div>
<div>
work</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ chmod 777 -R work</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ ls</div>
<div>
work</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ cd work</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work$ wget http://d3kbcqa49mib13.cloudfront.net/sp ark-2.1.0-bin-hadoop2.7.tgz</div>
<div>
--2017-04-12 16:55:57-- http://d3kbcqa49mib13.cloudfront.net/spark-2.1.0-bin-ha doop2.7.tgz</div>
<div>
Resolving d3kbcqa49mib13.cloudfront.net (d3kbcqa49mib13.cloudfront.net)... 54.23 0.5.90, 54.230.5.79, 54.230.5.12, ...</div>
<div>
Connecting to d3kbcqa49mib13.cloudfront.net (d3kbcqa49mib13.cloudfront.net)|54.2 30.5.90|:80... connected.</div>
<div>
HTTP request sent, awaiting response... 200 OK</div>
<div>
Length: 195636829 (187M) [application/x-tar]</div>
<div>
Saving to: ‘spark-2.1.0-bin-hadoop2.7.tgz’</div>
<div>
<br /></div>
<div>
100%[======================================>] 195,636,829 26.2MB/s in 7.7s</div>
<div>
<br /></div>
<div>
2017-04-12 16:56:05 (24.1 MB/s) - ‘spark-2.1.0-bin-hadoop2.7.tgz’ saved [1956368 29/195636829]</div>
<div>
<br /></div>
<div>
<b>Now extract the downloaded file as follows</b></div>
<div>
<br /></div>
<div>
plasparkadmin@pal-dev-vm-01:~/work$ ls</div>
<div>
spark-2.1.0-bin-hadoop2.7.tgz</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work$ tar -xvf spark-2.1.0-bin-hadoop2.7.tgz</div>
<h3 style="text-align: left;">
Download Java</h3>
</div>
<div>
Go to the Oracle site and download JDK 8, in my case from the ssh I did </div>
<div>
<br /></div>
<div>
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.tar.gz"</div>
<div>
<br /></div>
<div>
The above command should download and install the latest version of JAVA.</div>
<div>
<b>extract it with the command : </b>tar xzf jdk-8u121-linux-x64.tar.gz</div>
<h3 style="text-align: left;">
Setting Environment Paths</h3>
<div>
We need to set up JAVA_HOME, SPARK_HOME AND HADOOP_HOME as below from the command line</div>
<div>
<br /></div>
<div>
<div>
plasparkadmin@pal-dev-vm-01:~$ export JAVA_HOME=/home/plasparkadmin/work/jdk1.8.0_121</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ export PATH=$JAVA_HOME/bin:$PATH</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ echo $JAVA_HOME</div>
<div>
/home/plasparkadmin/work/jdk1.8.0_121</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ export SPARK_HOME=/home/plasparkadmin/work/spark- 2.1.0-bin-hadoop2.7</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ export PATH=$SPARK_HOME/bin:$PATH</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ echo $SPARK_HOME</div>
<div>
/home/plasparkadmin/work/spark-2.1.0-bin-hadoop2.7</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ export HADOOP_HOME=/home/plasparkadmin/work/spark -2.1.0-bin-hadoop2.7</div>
<div>
plasparkadmin@pal-dev-vm-01:~$ echo $HADOOP_HOME</div>
<div>
/home/plasparkadmin/work/spark-2.1.0-bin-hadoop2.7</div>
</div>
<div>
<br /></div>
<div>
<b>It might be prudent to set up the above paths into .profile along with .bashrc</b></div>
<div>
<b><br /></b></div>
<div>
Command : vi ~/.profile</div>
<div>
After opening scroll to the end of the file and press CNTRL + I, this will open the file in insert mode.</div>
<div>
<br /></div>
<div>
Type the paths in and then press ESC when done. This will change the file from Insert mode to normal mode.</div>
<div>
<br /></div>
<div>
To save and exit the file type = :x</div>
<div>
To quit without saving type= :q</div>
<div>
<h3 style="text-align: left;">
<span id="spanDownloadLink">Install Python Tools</span><span id="spanDownloadLink"> </span></h3>
<div style="text-align: left;">
<span id="spanDownloadLink">To make python development interactive, we might need to install some python development tools like iPython and Jupytar notebook</span>. Here is a site below which had detailed instructions on how to get that done and I was able to follow that with ease.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">https://www.digitalocean.com/community/tutorials/how-to-set-up-a-jupyter-notebook-to-run-ipython-on-ubuntu-16-04</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">Listing out the main commands. At first try to update the ubuntu version from terminal with the command : </span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$ sudo apt-get update</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">Please note that on first time run of the above command I faced an issue and the error was like below</span></div>
<div style="text-align: left;">
<br /></div>
<pre><code>E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?
</code></pre>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">I was able to resolve the above error with the following commands</span></div>
<div style="text-align: left;">
<br />
<span class="vote-count-post" itemprop="upvoteCount"></span></div>
<table><tbody>
<tr><td class="votecell"><br /></td><td class="answercell"><div class="post-text" itemprop="text">
Remove your <code>/var/lib/dpkg/lock</code> file and force package reconfiguration.<br />
<pre><code>sudo rm /var/lib/dpkg/lock
sudo dpkg --configure -a
</code></pre>
It should work after this.</div>
</td></tr>
</tbody></table>
<div style="text-align: left;">
<span id="spanDownloadLink">And the steps are explained above which I copied from stackoverflow.</span></div>
<div style="text-align: left;">
<span id="spanDownloadLink"><br /></span></div>
<div style="text-align: left;">
<span id="spanDownloadLink">Once your ubuntu is updated now install pip and python dev as follows</span></div>
<div style="text-align: left;">
<span id="spanDownloadLink"><br /></span></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$ sudo apt-get -y install python-pip python-dev</span></div>
<div style="text-align: left;">
<span id="spanDownloadLink"><br /></span></div>
<div style="text-align: left;">
<span id="spanDownloadLink">Once done you can check the installation location</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$pip --version</span></div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<span id="spanDownloadLink">pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$python --version</span></div>
<div style="text-align: left;">
<span id="spanDownloadLink">Python 2.7.12</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$whereis python2.7</span></div>
<div style="text-align: left;">
<span id="spanDownloadLink">python2: /usr/bin/python2.7-config /usr/bin/python2 /usr/bin/python2.7 /usr/lib/python2.7 /etc/python2.7 /usr/local/lib/python2.7 /usr/include/python2.7 /usr/share/man/man1/python2.1.gz</span></div>
<div style="text-align: left;">
</div>
</div>
<div>
<br /></div>
<div>
<div style="text-align: left;">
<span id="spanDownloadLink">once done, we can proceed with installation of ipython and notebook</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<b>Since we are working from ssh so notebook might not work, but here are the steps below for its installation.</b><br />
<br />
<span id="spanDownloadLink">$ sudo apt-get -y install ipython ipython-notebook</span></div>
<div style="text-align: left;">
<br /></div>
Now we can move on to installing Jupyter Notebook:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo -H pip install jupyter
</li>
</ul>
</code></pre>
Depending on what version of pip is in the Ubuntu apt-get repository, you might get the following error when trying to install Jupyter:<br />
<pre class="code-pre"><code><div class="secondary-code-label" title="Output">
</div>
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
</code></pre>
<br />
If so, you can use pip to upgrade pip to the latest version:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo -H pip install --upgrade pip
</li>
</ul>
</code></pre>
Upgrade pip, and then try installing Jupyter again:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo -H pip install jupyter
</li>
</ul>
</code></pre>
<div style="text-align: left;">
<span id="spanDownloadLink">Try out by typing</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$ jupyter notebook</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span id="spanDownloadLink">Installation done</span><br />
<h3 style="text-align: left;">
Setting up worker node/cluster</h3>
<div>
Login to the worker vm and install the open ssh server with the following commands</div>
<div>
<div>
At first we will install open-ssh server on the worker node so that it can remotely logged into from the master node. So open a terminal on the worker node and then type the following installation commands</div>
<div>
<br /></div>
<div>
<div class="line number1 index0 alt2">
<code class="bash comments"># On Worker nodes, we install SSH Server so that we can access this node from Master node</code></div>
<div class="line number2 index1 alt1">
<code class="bash functions">sudo</code> <code class="bash plain">apt-get </code><code class="bash functions">install</code> <code class="bash plain">openssh-server</code></div>
</div>
<div>
<br /></div>
<div>
After installing the SSH server on worker node, generate a key from the master so that master can access the worker node without asking for a password.</div>
<div>
<br /></div>
<div>
On the master node, we generate rsa key for remote access to the worker node.</div>
<div>
<br /></div>
<div>
<div class="line number3 index2 alt2">
<code class="bash comments"># On Master node, we generate a rsa key for remote access</code></div>
<div class="line number4 index3 alt1">
<code class="bash functions">ssh</code><code class="bash plain">-keygen</code></div>
</div>
<div>
<br /></div>
<div>
After the above command just keep pressing enter until the pass phrase gets generated</div>
<div>
<br /></div>
<div>
kghosh@DVY1L32-ubuntu-1:~$ ssh-keygen<br />
Generating public/private rsa key pair.<br />
Enter file in which to save the key (/home/kghosh/.ssh/id_rsa):<br />
Created directory '/home/kghosh/.ssh'.<br />
Enter passphrase (empty for no passphrase):<br />
Enter same passphrase again:<br />
Your identification has been saved in /home/kghosh/.ssh/id_rsa.<br />
Your public key has been saved in /home/kghosh/.ssh/id_rsa.pub.<br />
The key fingerprint is:<br />
SHA256:B8GWGJ+vSRKSr6L+Y+QeBvGr5Rljie4Crr+Lztc4csM kghosh@DVY1L32-ubuntu-1<br />
The key's randomart image is:<br />
+---[RSA 2048]----+<br />
| .+.. |<br />
| ...+o |<br />
| . o ..+ |<br />
| o o . o |<br />
| . . o S o |<br />
|. o.o. o + |<br />
|o.=X+ o |<br />
|+=*E=. |<br />
|XO@B+ |<br />
+----[SHA256]-----+</div>
<div>
<br /></div>
<div>
The Terminal will list out the passphrase file where it got saved as in the screen shot above.</div>
<div>
<br />
We need to now copy each the passphrase to each of the worker node machines and the generic command for that is as below<br />
<div class="line number5 index4 alt2">
</div>
<div class="line number5 index4 alt2">
<code class="bash comments"># To access Worker ncd odes via SSH without providing password (just use our rsa key), we need to copy our public key to each Worker node</code></div>
<div class="line number6 index5 alt1">
<code class="bash functions">ssh</code><code class="bash plain">-copy-</code><code class="bash functions">id</code> <code class="bash plain">-i ~/.</code><code class="bash functions">ssh</code><code class="bash plain">/id_rsa</code><code class="bash plain">.pub <username_on_remote_machine>@<IP_address_of_that_remote_machine></code></div>
<br />
<br />
<br />
You can get the Ip address of the remote machine by typing ifconfig from the terminal. So in our case that command is as follows<br />
<br />
<div class="line number8 index7 alt1">
<code class="bash functions">ssh</code><code class="bash plain">-copy-</code><code class="bash functions">id</code> <code class="bash plain">-i ~/.</code><code class="bash functions">ssh</code><code class="bash plain">/id_rsa</code><code class="bash plain">.pub kghosh@192.168.1.64</code></div>
<br />
Output-<br />
kghosh@DVY1L32-ubuntu-1:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub kghosh@192.168.1.164<br />
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/kghosh/.ssh/id_rsa.pub"<br />
The authenticity of host '192.168.1.164 (192.168.1.164)' can't be established.<br />
ECDSA key fingerprint is SHA256:jpsnVaUNquTNRqiuKqLGGyR3AYTp/tqIneCJf5ZWcDI.<br />
Are you sure you want to continue connecting (yes/no)? y<br />
Please type 'yes' or 'no': yes<br />
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed<br />
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys<br />
kghosh@192.168.1.164's password:<br />
<br />
Number of key(s) added: 1<br />
<br />
<div style="background-color: white; border: 0px; color: #373737; font-family: "helvetica neue", helvetica, arial, sans-serif; font-size: 15px; margin-bottom: 1.625em; outline: 0px; padding: 0px; vertical-align: baseline;">
Next, copy this file from the master to the the slave machine using </div>
<pre style="background-color: #eff0f1; border: 0px; color: #242729; font-family: consolas, menlo, monaco, "lucida console", "liberation mono", "dejavu sans mono", "bitstream vera sans mono", "courier new", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;">scp <source> <destination>
</code></pre>
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: arial, "helvetica neue", helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
To copy a file from <code style="background-color: #eff0f1; border: 0px; font-family: consolas, menlo, monaco, "lucida console", "liberation mono", "dejavu sans mono", "bitstream vera sans mono", "courier new", monospace, sans-serif; font-size: 13px; margin: 0px; padding: 1px 5px; white-space: pre-wrap;">B</code> to <code style="background-color: #eff0f1; border: 0px; font-family: consolas, menlo, monaco, "lucida console", "liberation mono", "dejavu sans mono", "bitstream vera sans mono", "courier new", monospace, sans-serif; font-size: 13px; margin: 0px; padding: 1px 5px; white-space: pre-wrap;">A</code> while logged into <code style="background-color: #eff0f1; border: 0px; font-family: consolas, menlo, monaco, "lucida console", "liberation mono", "dejavu sans mono", "bitstream vera sans mono", "courier new", monospace, sans-serif; font-size: 13px; margin: 0px; padding: 1px 5px; white-space: pre-wrap;">B</code>:</div>
<pre style="background-color: #eff0f1; border: 0px; color: #242729; font-family: consolas, menlo, monaco, "lucida console", "liberation mono", "dejavu sans mono", "bitstream vera sans mono", "courier new", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;">scp .ssh/id_rsa.pub kghosh@192.168.1.164:/home/kaushik/.ssh/id_rsa.pub</code></pre>
<div style="background-color: white; border: 0px; color: #373737; font-family: "helvetica neue", helvetica, arial, sans-serif; font-size: 15px; margin-bottom: 1.625em; outline: 0px; padding: 0px; vertical-align: baseline;">
and then from slave machine run:</div>
<pre style="background: rgb(244, 244, 244); border: 0px; color: #373737; font-family: "courier 10 pitch", courier, monospace; font-size: 13px; font-stretch: normal; line-height: 1.5; margin-bottom: 1.625em; outline: 0px; overflow: auto; padding: 0.75em 1.625em; vertical-align: baseline;">$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
$ chmod 644 ~/.ssh/authorized_keys</pre>
<h3 style="text-align: left;">
Spark Configuration </h3>
</div>
<div>
Go to the config folder of spark in the master machine and copy the file slaves.template to slaves and specify the ip address of the slave machine</div>
<div>
<br /></div>
<div>
<div>
plasparkadmin@pal-dev-vm-01:~/work$ cd spark-2.1.0-bin-hadoop2.7/</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7$ ls</div>
<div>
bin data jars licenses python README.md sbin</div>
<div>
conf examples LICENSE NOTICE R RELEASE yarn</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7$ cd conf/</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7/conf$ ls</div>
<div>
docker.properties.template metrics.properties.template spark-env.sh.template</div>
<div>
fairscheduler.xml.template slaves.template</div>
<div>
log4j.properties.template spark-defaults.conf.template</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7/conf$ cp slaves.template slaves</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7/conf$ ls</div>
<div>
docker.properties.template slaves</div>
<div>
fairscheduler.xml.template slaves.template</div>
<div>
log4j.properties.template spark-defaults.conf.template</div>
<div>
metrics.properties.template spark-env.sh.template</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7/conf$ vi slaves</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7/conf$ vi slaves</div>
<div>
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7/conf$</div>
</div>
<h3 style="text-align: left;">
Worker Configurations</h3>
<div>
For each worker from the master copy the jdk and spark binary folder recursively using scp command</div>
<div>
<br /></div>
<div>
<div>
plasparkadmin@pal-dev-vm-01:~/work$ scp -r jdk1.8.0_121/ plasparkadmin@13.85.14.23:/home/plasparkadmin/work/</div>
</div>
<div>
<br /></div>
<div>
<div>
plasparkadmin@pal-dev-vm-01:~/work$ scp -r spark-2.1.0-bin-hadoop2.7/ plasparkadmin@13.85.14.23:/home/plasparkadmin/work/</div>
</div>
<div>
<br /></div>
<div>
Once the copy is done, we will need to setup the Java Home path and the Spark and Hadoop Home paths just like we did in the master.</div>
<div>
<br /></div>
<div>
Just like in the master we can directly write set the paths in the terminal on the worker.</div>
<h3 style="text-align: left;">
Python Dev Tools on the Worker</h3>
<div>
Install python on the worker, so that we can submit pyspark jobs.</div>
<div>
<br /></div>
<div>
<div style="text-align: left;">
<span id="spanDownloadLink">$ sudo apt-get update</span></div>
<div style="text-align: left;">
<span id="spanDownloadLink">$ sudo apt-get -y install python-pip python-dev</span><br />
<h3 style="text-align: left;">
Installing Remote Desktop to Master and Minimum GUI</h3>
<div>
Most the steps are copied from the link in the reference. These are the steps I did below to enable remote desktop and load the spark master ui.</div>
<div>
<br /></div>
<div>
First thing to enable is the Port for Remote Desktop Connection on Azure. So login to Azure portal and under the virtual network of the Master VM, Enable Port TCP :3389.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEgfGf7RCCStFpeTnwUfi6WFjkgvFTObccBFIMIq-bNHq5dBDidP2fZI8GRCOWdiDsRwq-RyJGjJNJSR1Eq2yEOdXdwHHx5-NuKdwtL_cxCUmKtPv-6vxhw5PiJuhUuRPWAfKk5gs7VSg/s1600/NSG.PNG" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="118" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEgfGf7RCCStFpeTnwUfi6WFjkgvFTObccBFIMIq-bNHq5dBDidP2fZI8GRCOWdiDsRwq-RyJGjJNJSR1Eq2yEOdXdwHHx5-NuKdwtL_cxCUmKtPv-6vxhw5PiJuhUuRPWAfKk5gs7VSg/s320/NSG.PNG" width="320" /></a></div>
<div>
<br /></div>
<div>
Login to the master node using putty, first install the version of ubuntu desktop that support rdp and xfce.</div>
<div>
<br /></div>
<div>
<div class="lf-text-block lf-block" data-lf-anchor-id="58bdad30bf1cbd34268b99425afa09c2:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
Install <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">xfce</code>, use:<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="codeHeader" style="background-color: whitesmoke; border-bottom-color: initial; border-bottom-style: initial; border-image: initial; border-left-color: rgb(224, 224, 224); border-left-style: solid; border-right-color: rgb(224, 224, 224); border-right-style: solid; border-top-color: rgb(224, 224, 224); border-top-style: solid; border-width: 1px 1px 0px; color: #707070; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 0.8rem; margin-top: 16px; min-height: 30px;">
<div class="copyBtn" style="border-left: 1px solid rgb(224, 224, 224); cursor: pointer; float: right; padding: 2px 10px;">
Copy</div>
<div class="labelHolder" style="padding: 2px 16px;">
Code</div>
</div>
<pre style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border: 1px solid rgb(211, 214, 219); clear: right; color: #222222; direction: ltr; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; hyphens: none; line-height: 1.25; overflow-wrap: normal; overflow: auto; padding: 16px; tab-size: 4; word-break: normal; word-wrap: normal;"><code style="-webkit-font-smoothing: auto; border-radius: 2px; border: 0px; direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; line-height: 1.25; padding: 0px;">#sudo apt-get install xubuntu-desktop
</code></pre>
<div class="lf-text-block lf-block" data-lf-anchor-id="081705518e003024028419095af223b2:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
Then enable <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">xfce</code>, use:<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="codeHeader" style="background-color: whitesmoke; border-bottom-color: initial; border-bottom-style: initial; border-image: initial; border-left-color: rgb(224, 224, 224); border-left-style: solid; border-right-color: rgb(224, 224, 224); border-right-style: solid; border-top-color: rgb(224, 224, 224); border-top-style: solid; border-width: 1px 1px 0px; color: #707070; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 0.8rem; margin-top: 16px; min-height: 30px;">
<div class="copyBtn" style="border-left: 1px solid rgb(224, 224, 224); cursor: pointer; float: right; padding: 2px 10px;">
Copy</div>
<div class="labelHolder" style="padding: 2px 16px;">
Code</div>
</div>
<pre style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border: 1px solid rgb(211, 214, 219); clear: right; color: #222222; direction: ltr; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; hyphens: none; line-height: 1.25; overflow-wrap: normal; overflow: auto; padding: 16px; tab-size: 4; word-break: normal; word-wrap: normal;"><code style="-webkit-font-smoothing: auto; border-radius: 2px; border: 0px; direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; line-height: 1.25; padding: 0px;">#echo xfce4-session >~/.xsession
</code></pre>
<div class="lf-text-block lf-block" data-lf-anchor-id="5d576912af1a92b72ec5910e42ba17da:1" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
For Ubuntu to install xrdp use:<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="codeHeader" style="background-color: whitesmoke; border-bottom-color: initial; border-bottom-style: initial; border-image: initial; border-left-color: rgb(224, 224, 224); border-left-style: solid; border-right-color: rgb(224, 224, 224); border-right-style: solid; border-top-color: rgb(224, 224, 224); border-top-style: solid; border-width: 1px 1px 0px; color: #707070; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 0.8rem; margin-top: 16px; min-height: 30px;">
<div class="copyBtn" style="border-left: 1px solid rgb(224, 224, 224); cursor: pointer; float: right; padding: 2px 10px;">
Copy</div>
<div class="labelHolder" style="padding: 2px 16px;">
Code</div>
</div>
<pre style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border: 1px solid rgb(211, 214, 219); clear: right; color: #222222; direction: ltr; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; hyphens: none; line-height: 1.25; overflow-wrap: normal; overflow: auto; padding: 16px; tab-size: 4; word-break: normal; word-wrap: normal;"><code style="-webkit-font-smoothing: auto; border-radius: 2px; border: 0px; direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; line-height: 1.25; padding: 0px;">#sudo apt-get install xrdp</code></pre>
<div class="lf-text-block lf-block" data-lf-anchor-id="2ec75d31efc34cd32a0c08af54ca08e7:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
Edit the config file <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">/etc/xrdp/startwm.sh</code>, use:</div>
<div class="lf-text-block lf-block" data-lf-anchor-id="2ec75d31efc34cd32a0c08af54ca08e7:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="codeHeader" style="background-color: whitesmoke; border-bottom-color: initial; border-bottom-style: initial; border-image: initial; border-left-color: rgb(224, 224, 224); border-left-style: solid; border-right-color: rgb(224, 224, 224); border-right-style: solid; border-top-color: rgb(224, 224, 224); border-top-style: solid; border-width: 1px 1px 0px; color: #707070; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 0.8rem; margin-top: 16px; min-height: 30px;">
<div class="copyBtn" style="border-left: 1px solid rgb(224, 224, 224); cursor: pointer; float: right; padding: 2px 10px;">
Copy</div>
<div class="labelHolder" style="padding: 2px 16px;">
Code</div>
</div>
<pre style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border: 1px solid rgb(211, 214, 219); clear: right; color: #222222; direction: ltr; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; hyphens: none; line-height: 1.25; overflow-wrap: normal; overflow: auto; padding: 16px; tab-size: 4; word-break: normal; word-wrap: normal;"><code style="-webkit-font-smoothing: auto; border-radius: 2px; border: 0px; direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; line-height: 1.25; padding: 0px;">#sudo vi /etc/xrdp/startwm.sh
</code></pre>
<div class="lf-text-block lf-block" data-lf-anchor-id="24044465c1115218f6808c28c0928554:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
Add line <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">xfce4-session</code> before the line <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">/etc/X11/Xsession</code>.<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="lf-text-block lf-block" data-lf-anchor-id="0a667141b91111b0030002931db20892:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
Restart xrdp service, use:<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="codeHeader" style="background-color: whitesmoke; border-bottom-color: initial; border-bottom-style: initial; border-image: initial; border-left-color: rgb(224, 224, 224); border-left-style: solid; border-right-color: rgb(224, 224, 224); border-right-style: solid; border-top-color: rgb(224, 224, 224); border-top-style: solid; border-width: 1px 1px 0px; color: #707070; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 0.8rem; margin-top: 16px; min-height: 30px;">
<div class="copyBtn" style="border-left: 1px solid rgb(224, 224, 224); cursor: pointer; float: right; padding: 2px 10px;">
Copy</div>
<div class="labelHolder" style="padding: 2px 16px;">
Code</div>
</div>
<pre style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border: 1px solid rgb(211, 214, 219); clear: right; color: #222222; direction: ltr; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; hyphens: none; line-height: 1.25; overflow-wrap: normal; overflow: auto; padding: 16px; tab-size: 4; word-break: normal; word-wrap: normal;"><code style="-webkit-font-smoothing: auto; border-radius: 2px; border: 0px; direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.875rem; line-height: 1.25; padding: 0px;">#sudo service xrdp restart
</code></pre>
<h2 id="connect-your-linux-vm-from-a-windows-machine" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 1.75rem; font-weight: 300; line-height: 1.3; margin-bottom: 12px; margin-top: 32px; overflow-wrap: break-word; text-rendering: optimizelegibility; word-wrap: break-word;">
Connect your Linux VM from a Windows machine</h2>
<div class="lf-text-block lf-block" data-lf-anchor-id="226ca30ec4b32ea4014c179625006a70:0" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
In a Windows machine, start the remote desktop client(Remote Desktop Connection), input your Linux VM DNS name, or go to <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">Dashboard</code> of your VM in Azure classic portal and click <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">Connect</code> to connect your Linux VM, you will see below login window:<span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="lf-text-block lf-block" data-lf-anchor-id="d41d8cd98f00b204e9800998ecf8427e:1" style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; font-size: 16px; margin-top: 1rem; padding-right: 25px; position: relative;">
<img alt="image" data-linktype="relative-path" src="https://docs.microsoft.com/en-us/azure/virtual-machines/linux/classic/media/remote-desktop/no2.png" style="border: 0px; display: inline-block; height: auto; max-width: 100%;" /><span class="lf-thread-btn" style="position: absolute; right: 0px; top: 0px;"></span></div>
<div class="lf-text-block lf-block" data-lf-anchor-id="ee4c090e92a16ef4330c058585024d34:0" style="background-color: white; margin-top: 1rem; padding-right: 25px; position: relative;">
<div style="color: #222222; font-size: 16px;">
Login with the <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">user</code> & <code style="-webkit-font-smoothing: auto; background-color: #f9f9f9; border-radius: 2px; border: 1px solid rgb(211, 214, 219); direction: ltr; display: inline-block; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 0.8rem; line-height: 1.25; padding: 3px 7px;">password</code> of your Linux VM, and enjoy the Remote Desktop from your Microsoft Azure Linux VM right now!</div>
<h3 style="color: #222222; font-size: 16px; text-align: left;">
Sample Python Application and PySpark Submission</h3>
<div style="color: #222222; font-size: 16px;">
SSH into the master and navigate to the spark home directory. Once you are there create a project folder, in our case its called kaushikpla. On the terminal type</div>
<div style="color: #222222; font-size: 16px;">
<br /></div>
<div>
<div style="color: #222222; font-size: 16px;">
plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7$ cd kaushikpla/</div>
<div>
<div>
<span style="color: #222222;">plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7$ vi plaPythonApp.py</span></div>
</div>
<div style="color: #222222; font-size: 16px;">
<br /></div>
</div>
<div style="color: #222222; font-size: 16px;">
Copy paste the below pyspark code from local computer</div>
<div style="color: #222222; font-size: 16px;">
<br /></div>
<div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;">from pyspark import SparkConf</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;">from pyspark import SparkContext </span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;">from pyspark.sql import SQLContext</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;">from pyspark.sql.types import *</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"><br /></span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;">def main():</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> sc = SparkContext()</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> sqlContext = SQLContext(sc)</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"><br /></span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> eventPath = "wasb://sparkjob@pladevstorage.blob.core.windows.net/input/events.log"</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> eventsJson = sqlContext.read.json(eventPath)</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> </span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> resultDF = eventsJson.groupBy(['EventDateTime', 'EventTypeName', 'PN']).count()</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> resultDF.coalesce(1).write.format('csv').options(header='true').save('wasb://sparkjob@pladevstorage.blob.core.windows.net/outputvmcsv')</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> resultDF.coalesce(1).write.format('json').save('wasb://sparkjob@pladevstorage.blob.core.windows.net/outputvmjson')</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> </span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> </span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"><br /></span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;">if __name__ == "__main__":</span></div>
<div>
<span face=""verdana" , sans-serif" style="color: #222222; font-size: xx-small;"> main()</span></div>
</div>
<div>
<span style="color: #222222;"><br /></span></div>
<div>
Once that is done, go back to the spark home directory and submit the job with the below command.</div>
<div style="color: #222222; font-size: 16px;">
<br /></div>
<div>
<div>
<span style="color: #222222;">plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7$ ./bin/spark-submit --master spark://10.0.0.4:7077 kaushikpla/plaPythonApp.py</span><br />
<h3 style="text-align: left;">
<span style="color: #222222;">Install Microsoft SQL JDBC Driver</span></h3>
<div style="text-align: left;">
<span style="color: #222222;">In order to connect to SQL Server, we need to install Microsoft SQL JDBC Driver. So download the latest driver from the link below</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="color: #222222;">https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774</span></div>
<div style="text-align: left;">
<span style="color: #222222;">Download the file : sqljdbc_6.0.8112.100_enu.tar.gz and unpack it using the following command</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="color: #222222;">plasparkadmin@pal-dev-vm-01:~/work$ gzip -d sqljdbc_6.0.8112.100_enu.tar.gz<br />plasparkadmin@pal-dev-vm-01:~/work$ tar -xf sqljdbc_6.0.8112.100_enu.tar<br /> </span></div>
<div style="text-align: left;">
<span style="color: #222222;">Once its extracted, we proceed to adding it to the classpath of spark and write a sample code to test.</span></div>
<div style="text-align: left;">
<span style="color: #222222;">To add the driver class path, I added the *.jar file to the $SPARK_HOME\conf\spark-defaults.conf </span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="color: #222222;">If there are already entries then put a comma and then add the entry below</span></div>
<div style="text-align: left;">
<span style="color: #222222;">/home/plasparkadmin/work/sqljdbc_6.0/enu/jre8/sqljdbc42.jar</span></div>
<div style="text-align: left;">
<span style="color: #222222;"><br /></span></div>
<div style="text-align: left;">
<span style="color: #222222;">So my final spark-defaults.conf looked like below</span></div>
<div style="text-align: left;">
<span style="color: #222222;">spark.jars=/home/plasparkadmin/work/spark-2.1.0-bin-hadoop2.7/lib/hadoop-azure-2.7.0.jar,/home/plasparkadmin/work/spark-2.1.0-bin-hadoop2.7/lib/azure-storage-2.0.0.jar,<b>/home/plasparkadmin/work/sqljdbc_6.0/enu/jre8/sqljdbc42.jar </b></span></div>
<div style="text-align: left;">
<span style="color: #222222;"><br /></span></div>
<div style="text-align: left;">
<span style="color: #222222;">scp the sqljdbc folder to the workers and add the same class path in each workers and restart the cluster.</span></div>
<h3 style="text-align: left;">
<span style="color: #222222;">Testing Connection to SQL Server</span></h3>
<div style="text-align: left;">
<span style="color: #222222;">Write the below sample application and save it and run it with the execution command below</span></div>
<div style="text-align: left;">
<span style="color: #222222;"><br /></span></div>
<div style="text-align: left;">
<span style="color: #222222;">plasparkadmin@pal-dev-vm-01:~/work/spark-2.1.0-bin-hadoop2.7$ ./bin/spark-submit --driver-class-path <b>/home/plasparkadmin/work/sqljdbc_6.0/enu/jre8/sqljdbc42.jar</b> --master spark://10.0.0.4:7077 kaushikpla/plaRollupToSQL.py </span></div>
<div style="text-align: left;">
<span style="color: #222222;"><br /></span></div>
<div style="text-align: left;">
<span style="color: #222222;">please note above that I have added the driver class path in the execution command because it was not able to find the driver without that.</span></div>
<div style="text-align: left;">
<span style="color: #222222;"><br /></span></div>
</div>
</div>
</div>
</div>
<div>
<span style="font-size: x-small;">from pyspark import SparkConf</span><br />
<span style="font-size: x-small;">from pyspark import SparkContext</span><br />
<span style="font-size: x-small;">from pyspark.sql import SQLContext</span><br />
<span style="font-size: x-small;"><br /></span>
<span style="font-size: x-small;">def main():</span><br />
<span style="font-size: x-small;"> conf = (SparkConf().setAppName("data_import"))</span><br />
<span style="font-size: x-small;"> sc = SparkContext(conf = conf)</span><br />
<span style="font-size: x-small;"> #sc = SparkContext()</span><br />
<span style="font-size: x-small;"> sqlContext = SQLContext(sc)</span><br />
<span style="font-size: x-small;"> jdbcDF = sqlContext.read.format("jdbc").option("url", "jdbc:sqlserver://svr.database.windows.net;databaseName=dbname").option("dbtable", "dbo.mytable").option("user", "username").option("password", "password").load()</span><br />
<span style="font-size: x-small;"> # Displays the content of the DataFrame to stdout ...first 10 rows</span><br />
<span style="font-size: x-small;"> jdbcDF.show(10)</span><br />
<span style="font-size: x-small;"><br /></span>
<span style="font-size: x-small;">if __name__ == "__main__":</span><br />
<span style="font-size: x-small;"> main()</span><br />
<h3 style="text-align: left;">
Writing to Azure SQL Database</h3>
<div style="text-align: left;">
Just like you can read from Azure SQL, you can also write to Azure SQL directly from the dataframe, a sample code as below</div>
<div style="text-align: left;">
<br /></div>
<span style="font-size: x-small;">viewpath = "wasb://ap1rpt@dawstopla.blob.core.windows.net/events_dt.csv"</span><br />
<span style="font-size: x-small;">df = edf.select('ET', 'EventCount').groupBy('ET').agg(func.sum("EventCount")).withColumnRenamed('sum(EventCount)', 'EventCount')</span><br />
<span style="font-size: x-small;">df.withColumn('EventDate', lit(eventDate)).coalesce(1).write.format("csv").mode("overwrite").options(header='true').save(viewpath)</span><br />
<span style="font-size: x-small;">df.withColumn('EventDate', lit(eventDate)).write.jdbc(url="jdbc:sqlserver://server.database.windows.net;databaseName=mydb", table="dbo.mytable", mode="append", properties={"user": "username", "password":"password"})</span><br />
<br />
*For the above to wotk, please make sure the select columns and the database column names in the database are same. mode = "append" will mean it will append to the existing data that is there in the database.<br />
<h3 style="text-align: left;">
Python Module for MSSQL Client</h3>
<div style="text-align: left;">
There is a python module for MsSQL client : http://www.pymssql.org/en/stable/intro.html. To install that module do this below</div>
<div style="text-align: left;">
<br /></div>
<pre>sudo apt-get install freetds-dev</pre>
<pre><code class="docutils literal"><span class="pre">sudo pip</span> <span class="pre">install</span> <span class="pre">pymssql</span></code></pre>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Once installed we will run a spark job to write and read from database. I have not used it yet.</div>
</div>
</div>
</div>
<div>
<br /></div>
</div>
</div>
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-50979319479043538162021-08-08T14:08:00.003-07:002021-08-08T14:21:39.646-07:00Remote Login to Ubuntu 20 from Windows 10<h2 style="text-align: left;"> Remote login to Ubuntu 20 from Windows 10</h2><div>Making a note on how to enable remote login to a desktop/notebook having ubuntu 20 installed from windows. As of first steps there are some prerequisites that we need to have and some software we will need to install on the ubuntu machine. I am assuming that you are having sudo access on the ubuntu machine. The steps are outlined as below</div><div>1. Login to the ubuntu machine. or ssh username@ip_address</div><div>2. Install XRDP Packages</div><div>sudo apt-get</div><div>sudo apt-get install xrdp</div><div><br /></div><div>3. Install the desktop environment</div><div>sudo apt-get install xfce4</div><div>sudo apt-get install xfce-terminal</div><div><br /></div><div>4. After installation, configure XRDP to use XFCE with below command</div><div><br /></div><div>sudo sed -i.bak '/fi/a #xrdp multiple users configuration \n xfce-session \n' /etc/xrdp/startwm.sh</div><div><br /></div><div>5. Allow RDP port in Firewall</div><div>sudo ufw allow 3389/tcp</div><div><br /></div><div>6. Restart the Xrdp application</div><div>sudo /etc/init.d/xrdp restart</div><div><br /></div><div>7. After doing the above steps I was logging in and getting a blank screen. The reason being that xrdp/startwm.sh was missing some configurations</div><div><br /></div><div>sudo apt-get install xorg</div><div>sudo -s</div><div>vi /etc/xrdp/startwm.sh</div><div><br /></div><div>8. Once the script filr opens add the lines below</div><div><br /></div><div>unset DBUS_SESSION_BUS_ADDRESS</div><div>unset XDG_RUNTIME_DIR</div><div>.$HOME/.profile</div><div><br /></div><div><br /></div><div>When you open the file it will look as below</div><div><br /></div><div>vi /etc/xrdp/startwm.sh</div><div><br /></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD8waVpezMZuIbLhZjhN4ko875DwBoK2rJ85cVl_8d6C3hhp7wjdpELVNej-OtozvyZ2LhyphenhyphenRtmN8hfg_SU9Icd8SPlHGTm1MAOHQyPNq_t9YfAMk_HI6i2043bAO5y2rUlVtiZ7USe9GQ/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="699" data-original-width="1285" height="349" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD8waVpezMZuIbLhZjhN4ko875DwBoK2rJ85cVl_8d6C3hhp7wjdpELVNej-OtozvyZ2LhyphenhyphenRtmN8hfg_SU9Icd8SPlHGTm1MAOHQyPNq_t9YfAMk_HI6i2043bAO5y2rUlVtiZ7USe9GQ/w640-h349/image.png" width="640" /></a></div><br />9. Finally, reboot the machine/VM after the configurations are set</div><div>reboot</div><div><br /></div>KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-8965137817553008182020-12-17T12:39:00.005-08:002020-12-21T22:27:59.270-08:00Installing Docker Tools on Ununtu VM on Azure to run Superset<h2 style="text-align: left;"> Installing Docker Tools on Ubuntu VMs on Azure to Run Superset</h2><div>As a first step, we are creating a VM on Azure through the portal. We make sure of 2 things in this example</div><div><ol style="text-align: left;"><li>Select Ubuntu 18.04</li><li>Make sure the ports 80, 443, 22, 3389 are open</li><li>Make sure the VM has a public ip address so that you can access it using Putty or other secured shell client from your local machine.</li></ol><div>Since we are dealing with running <a href="https://superset.apache.org/">Apache superset</a> so we will first login and clone the repository and change the admin password</div><h3 style="text-align: left;">Cloning Superset from repository and change Default Password</h3><div>Clone the official repository using</div><div>$ git clone https://github.com/apache/incubator-superset.git</div><div><br /></div><div>The default password is "admin" so we can change it so that no one else knows it</div><div><br /></div><div>$ cd incubator-superset</div><div>$ cd docker</div><div>$ vi docker-init.sh</div><div>#change the line to your desired password ADMIN_PASSWORD="admin"</div><h3 style="text-align: left;">Installing Docker</h3></div><div>I have followed instructions listed on the url : https://docs.docker.com/engine/install/ubuntu/ using the method "install using the repository". Majority of the steps listed here will be from the url listed above and purpose of documenting this is to note down how to resolve the errors while doing a docker compose.</div><div><br /></div><div><div>#SET UP THE REPOSITORY</div><div>#Update the apt package index and install packages to allow apt to use a repository over HTTPS:</div><div><br /></div><div>$ sudo apt-get update</div><div><br /></div><div>$ sudo apt-get install \</div><div> apt-transport-https \</div><div> ca-certificates \</div><div> curl \</div><div> gnupg-agent \</div><div> software-properties-common</div><div>Add Docker’s official GPG key:</div><div><br /></div><div>$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -</div><div><br /></div><div>#Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.</div><div><br /></div><div>$ sudo apt-key fingerprint 0EBFCD88</div><div><br /></div><div>pub rsa4096 2017-02-22 [SCEA]</div><div> 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88</div><div>uid [ unknown] Docker Release (CE deb) <docker@docker.com></div><div>sub rsa4096 2017-02-22 [S]</div><div><br /></div><div>Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.</div></div><div><br /></div><div>$ sudo add-apt-repository \</div><div><div> "deb [arch=amd64] https://download.docker.com/linux/ubuntu \</div><div> $(lsb_release -cs) \</div><div> stable"</div></div><div><br /></div><div><h3 style="text-align: left;">INSTALL DOCKER ENGINE</h3><div>#Update the apt package index, and install the latest version of Docker Engine and containerd, or go to #the next step to install a specific version:</div><div><br /></div><div> $ sudo apt-get update</div><div> $ sudo apt-get install docker-ce docker-ce-cli containerd.io</div></div><div><div><br /></div><div>To install a specific version of Docker Engine, list the available versions in the repo, then select and install:</div><div><br /></div><div>a. List the versions available in your repo:</div><div><br /></div><div>$ apt-cache madison docker-ce</div></div><div><br /></div><div><div>b. Install a specific version using the version string from the second column, for example, 5:18.09.1~3-0~ubuntu-xenial.</div><div><br /></div><div>$ sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io</div></div><div><br /></div><div>In my case it was </div><div><div>$ sudo apt-get install docker-ce=5:20.10.1~3-0~ubuntu-bionic docker-ce-cli=5:20.10.1~3-0~ubuntu-bionic containerd.io</div></div><div><br /></div><div><div>Verify that Docker Engine is installed correctly by running the hello-world image.</div><div><br /></div><div>$ sudo docker run hello-world</div></div><div><br /></div><h3 style="text-align: left;">Install Docker Compose</h3><div>You can install docker-compose using the command below</div><div>$ sudo apt install docker-compose</div><h3 style="text-align: left;">Compose Superset</h3><div>we will try to compose the downloaded superset by the command below</div><div>$ docker-compose up</div><div><br /></div><div>On running the above command you will face issue where it will complain about the version of the docker-compose.yaml. In order to resolve it, please change the version to 2.2</div><div><br /></div><div>After making the above change when you run the docker-compose again it will still complain and the error should be "<a href="https://github.com/docker/compose/issues/4181">Couldn't connect to docker daemon</a>". In order to resolve this we need to do the following</div><div><br /></div><div>$export DOCKER_HOST=internalIp of the VM</div><div>$ sudo usermod -aG docker <<username>></div><div>$ service docker restart</div><div>$ sudo docker-compose up -d</div><div><br /></div><div>The -d option is to run it in a detached mode so even if your terminal session closes the service would still keep running. When installation is complete and the services are running you can check what are the services running using the command below</div><div><br /></div><div>$docker-compose ps</div><div><br /></div><div><div>:~/incubator-superset$ docker-compose ps</div><div>WARNING: The CYPRESS_CONFIG variable is not set. Defaulting to a blank string.</div><div> Name Command State Ports</div><div>--------------------------------------------------------------------------------</div><div>superset_app /usr/bin/docker- Up 8080/tcp,</div><div> entrypoint ... 0.0.0.0:8088->8088/tcp</div><div>superset_cache docker-entrypoint.sh Up 127.0.0.1:6379->6379/t</div><div> redis ... cp</div><div>superset_db docker-entrypoint.sh Up 127.0.0.1:5432->5432/t</div><div> postgres cp</div><div>superset_init /usr/bin/docker- Exit 0</div><div> entrypoint ...</div><div>superset_node docker-entrypoint.sh Up</div><div> /app/ ...</div><div>superset_tests_worker /usr/bin/docker- Exit 1</div><div> entrypoint ...</div><div>superset_worker /usr/bin/docker- Up 8080/tcp</div><div> entrypoint ...</div></div><div><br /></div><div>Now you should be able to access the superset instance by http://{Private of Public IP}:8088 and login using the username = admin and password = <<password set in docker-init.sh>></div><div><br /></div><div>In order to stop the services from running use the command below</div><div><br /></div><div>$ docker-compose stop </div><div>In order to remove the container </div><div>$ docker-compose down</div><div><br /></div>KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-41618240718734242812020-07-29T22:06:00.001-07:002020-07-29T22:06:22.986-07:00Enabling remote desktop connection on ubuntu on Azure<h2 style="text-align: left;">Enable remote desktop connection on ubuntu VM on azure</h2><div>There may be instances when you will need to setup a Linux/Ubuntu VM on a public cloud like azure do quickly do some Poc instead of bothering your own local machine which may not be linux based.</div><div><br /></div><div><div>Create Ubuntu (v 18 in my case) VM (Resource Managed)</div><div>ssh into VM with Putty</div><div>sudo apt-get update</div><div>sudo apt-get install lxde</div><div>sudo apt-get install xrdp</div><div>echo startlxde > ~/.xsession</div><div>sudo /etc/init.d/xrdp start</div><div>open port 3389 in Azure firewall</div><div>RDP to Ubuntu desktop :)</div></div><div><br /></div><h3 style="text-align: left;">Steps to Open Port 3389 on VM</h3><div><ol style="text-align: left;"><li>Sign in to the Azure portal.</li><li>In Virtual Machines, select the VM that has the problem.</li><li>In Settings, select Networking. </li><li>In Inbound port rules, check whether the port for RDP is set correctly. The following is an example of the configuration:</li><ol><li>Priority: 300 (set 310 if 300 is already taken)</li><li>Name: Port_3389</li><li>Port(Destination): 3389</li><li>Protocol: TCP</li><li>Source: Any</li><li>Destinations: Any</li><li>Action: Allow</li></ol></ol><div>All Set!</div></div>KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-8100204072815774812017-12-22T16:11:00.001-08:002017-12-22T16:11:45.903-08:00Creating Azure SQL Login and Assigning them permission<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Creating Read-Only Users on Azure SQL</h2>
<div>
If you have admin rights please follow the following steps to create a read-only or a user with login who can just run select queries on azure sql.</div>
<h3 style="text-align: left;">
1. Login to the Database Server as Admin and Select Master Database and run the following queries</h3>
<div>
--This will create a Login on the Server</div>
<div>
CREATE LOGIN <span style="background-color: yellow;">READ_USER</span> WITH PASSWORD = <span style="background-color: yellow;">'StrongPassword'</span>;</div>
<h3 style="text-align: left;">
2. Create User in the Database where the Read-Only permission is required</h3>
<div>
--Select the Database where you will be assigning the Read-only permission and run below command</div>
<div>
CREATE USER <span style="background-color: yellow;">READ_USER</span> FROM LOGIN <span style="background-color: yellow;">READ_USER</span> ;</div>
<h3 style="text-align: left;">
3. Assign db_datareader persmission to the user on the database</h3>
<div>
--Select the Database where you will be assigning read permission and run below query</div>
<div>
EXEC sp_addrolemember '<span style="background-color: yellow;">db_datareader</span>', '<span style="background-color: yellow;">READ_USER</span> ';</div>
<div>
<br /></div>
<div>
Reference : https://azure.microsoft.com/en-us/blog/adding-users-to-your-sql-azure-database/</div>
<div>
<br /></div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com1tag:blogger.com,1999:blog-8063958573455141345.post-14506017918510365912016-10-22T22:26:00.001-07:002016-10-22T22:27:21.197-07:00Linking Jupyter Notebook with Spark on ubuntu 16<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Starting Jupyter Notebook with Apache Spark</h2>
<h3 style="text-align: left;">
Required Variable Setup</h3>
<div>
Open a terminal and enter command to edit the profile variable</div>
<div>
<br /></div>
<div>
$ gedit ~/.bashrc</div>
<div>
<br /></div>
<div>
Once the window opens, enter the following two lines</div>
<div>
<br /></div>
<div>
export PYSPARK_DRIVER_PYTHON=ipython</div>
<div>
export PYSPARK_DRIVER_PYTHON_OPTS="notebook"</div>
<div>
<br /></div>
<div>
Once done hit save and exit the terminal.</div>
<h3 style="text-align: left;">
Running the Notebook with Spark Cluster</h3>
<div>
Assuming that its a local standalone cluster, we can start it using the following commands</div>
<div>
<br /></div>
<div>
$ pyspark --master local[2]</div>
<div>
<br /></div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-37282085186827758082016-10-17T09:54:00.002-07:002016-10-17T09:54:31.440-07:00How To Set Up a Jupyter Notebook to Run IPython on Ubuntu 16.04<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="content-body tutorial-content" data-growable-markdown="">
<div id="introduction" style="text-align: left;">
Copied from : https://www.digitalocean.com/community/tutorials/how-to-set-up-a-jupyter-notebook-to-run-ipython-on-ubuntu-16-04</div>
<div id="introduction" style="text-align: left;">
<br /></div>
<h3 id="introduction">
Introduction</h3>
IPython is an interactive command-line interface to Python. Jupyter
Notebook offers an interactive web interface to many languages,
including IPython.<br />
This article will walk you through setting up a server to run Jupyter
Notebook as well as teach you how to connect to and use the notebook.
Jupyter notebooks (or simply notebooks) are documents produced by the
Jupyter Notebook app which contain both computer code (e.g. Python) and
rich text elements (paragraph, equations, figures, links, etc.) which
aid in presenting reproducible research. <br />
By the end of this guide, you will be able to run Python 2.7 code
using Ipython and Jupyter Notebook running on a remote server. For the
purposes of this tutorial, Python 2 (2.7.x) is used since many of the
data science, scientific computing, and high-performance computing
libraries support 2.7 and not 3.0+. <br />
<h2 id="prerequisites">
Prerequisites</h2>
To follow this tutorial, you will need the following:<br />
<ul>
<li>Ubuntu 16.04 Droplet</li>
<li>Non-root user with sudo privileges (<a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04">Initial Server Setup with Ubuntu 16.04</a> explains how to set this up.)</li>
</ul>
All the commands in this tutorial should be run as a non-root user.
If root access is required for the command, it will be preceded by <code>sudo</code>. <a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04">Initial Server Setup with Ubuntu 16.04</a> explains how to add users and give them sudo access.<br />
<h2 id="step-1-—-installing-python-2-7-and-pip">
Step 1 — Installing Python 2.7 and Pip</h2>
In this section we will install Python 2.7 and Pip. <br />
First, update the system's package index. This will ensure that old or outdated packages do not interfere with the installation.<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo apt-get update
</li>
</ul>
</code></pre>
Next, install Python 2.7, Python Pip, and Python Development:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo apt-get -y install python2.7 python-pip python-dev
</li>
</ul>
</code></pre>
Installing <code>python2.7</code> will update to the latest version of Python 2.7, and <code>python-pip</code>
will install Pip which allows us to manage Python packages we would
like to use. Some of Jupyter’s dependencies may require compilation, in
which case you would need the ability to compile Python C-extensions,
so we are installing <code>python-dev</code> as well.<br />
To verify that you have python installed:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">python --version
</li>
</ul>
</code></pre>
This will output:<br />
<pre class="code-pre "><code><div class="secondary-code-label " title="Output">
Output</div>
Python 2.7.11+
</code></pre>
Depending on the latest version of Python 2.7, the output might be different.<br />
You can also check if pip is installed using the following command:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">pip --version
</li>
</ul>
</code></pre>
You should something similar to the following:<br />
<pre class="code-pre "><code><div class="secondary-code-label " title="Output">
Output</div>
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
</code></pre>
Similarly depending on your version of pip, the output might be slightly different. <br />
<h2 id="step-2-—-installing-ipython-and-jupyter-notebook">
Step 2 — Installing Ipython and Jupyter Notebook</h2>
In this section we will install Ipython and Jupyter Notebook.<br />
First, install Ipython:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo apt-get -y install ipython ipython-notebook
</li>
</ul>
</code></pre>
Now we can move on to installing Jupyter Notebook:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo -H pip install jupyter
</li>
</ul>
</code></pre>
Depending on what version of pip is in the Ubuntu apt-get repository,
you might get the following error when trying to install Jupyter:<br />
<pre class="code-pre "><code><div class="secondary-code-label " title="Output">
Output</div>
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
</code></pre>
If so, you can use pip to upgrade pip to the latest version:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo -H pip install --upgrade pip
</li>
</ul>
</code></pre>
Upgrade pip, and then try installing Jupyter again:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">sudo -H pip install jupyter
</li>
</ul>
</code></pre>
<h2 id="step-3-—-running-jupyter-notebook">
Step 3 — Running Jupyter Notebook</h2>
You now have everything you need to run Jupyter Notebook! To run it, execute the following command:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">jupyter notebook
</li>
</ul>
</code></pre>
If you are running Jupyter on a system with JavaScript installed, it
will still run, but it might give you an error stating that the Jupyter
Notebook requires JavaScript:<br />
<pre class="code-pre "><code><div class="secondary-code-label " title="Output">
Output</div>
Jupyter Notebook requires JavaScript.
Please enable it to proceed.
...
</code></pre>
To ignore the error, you can press <code>Q</code> and then press <code>Y</code> to confirm. <br />
A log of the activities of the Jupyter Notebook will be printed to
the terminal. When you run Jupyter Notebook, it runs on a specific port
number. The first notebook you are running will usually run on port <code><span class="highlight">8888</span></code>. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:<br />
<pre class="code-pre "><code><div class="secondary-code-label " title="Output">
Output</div>
[I NotebookApp] Serving notebooks from local directory: /home/sammy
[I NotebookApp] 0 active kernels
[I NotebookApp] The Jupyter Notebook is running at: http://localhost<span class="highlight">:8888</span>/
[I NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
</code></pre>
If you are running Jupyter Notebook on a local Linux computer (not on a Droplet), you can simply navigate to <code>localhost<span class="highlight">:8888</span></code>
to connect to Jupyter Notebook. If you are running Jupyter Notebook on
a Droplet, you will need to connect to the server using SSH tunneling
as outlined in the next section.<br />
At this point, you can keep the SSH connection open and keep Jupyter
Notebook running or can exit the app and re-run it once you set up SSH
tunneling. Let's keep it simple and stop the Jupyter Notebook process.
We will run it again once we have SSH tunneling working. To stop the
Jupyter Notebook process, press <code>CTRL+C</code>, type <code>Y</code>, and hit <code>ENTER</code> to confirm. The following will be displayed:<br />
<pre class="code-pre "><code><div class="secondary-code-label " title="Output">
Output</div>
[C 12:32:23.792 NotebookApp] Shutdown confirmed
[I 12:32:23.794 NotebookApp] Shutting down kernels
</code></pre>
<h2 id="step-4-—-connecting-to-the-server-using-ssh-tunneling">
Step 4 — Connecting to the Server Using SSH Tunneling</h2>
In this section we will learn how to connect to the Jupyter Notebook
web interface using SSH tunneling. Since Jupyter Notebook is running on
a specific port on the Droplet (such as <code>:8888</code>, <code>:8889</code> etc.), SSH tunneling enables you to connect to the Droplet's port securely.<br />
The next two subsections describe how to create an SSH tunnel from 1)
a Mac or Linux and 2) Windows. Please refer to the subsection for your
local computer.<br />
<h3 id="ssh-tunneling-with-a-mac-or-linux">
SSH Tunneling with a Mac or Linux</h3>
If you are using a Mac or Linux, the steps for creating an SSH tunnel are similar to the <a href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets">How To Use SSH Keys with DigitalOcean Droplets using Linux or Mac</a> guide except there are additional parameters added in the <code>ssh</code> command. This subsection will outline the additional parameters needed in the <code>ssh</code> command to tunnel successfully. <br />
SSH tunneling can be done by running the following SSH command:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">ssh -L <span class="highlight">8000</span>:localhost:<span class="highlight">8888</span> <span class="highlight">your_server_username</span>@<span class="highlight">your_server_ip</span>
</li>
</ul>
</code></pre>
The <code>ssh</code> command opens an SSH connection, but <code>-L</code>
specifies that the given port on the local (client) host is to be
forwarded to the given host and port on the remote side (Droplet). This
means that whatever is running on the second port number (i.e. <code><span class="highlight">8888</span></code>) on the Droplet will appear on the first port number (i.e. <code><span class="highlight">8000</span></code>) on your local computer. You should change <code><span class="highlight">8888</span></code> to the port which Jupyter Notebook is running on. Optionally change port <code><span class="highlight">8000</span></code> to one of your choosing (for example, if <code>8000</code> is used by another process). Use a port greater or equal to <code>8000</code> (ie <code><span class="highlight">8001</span></code>, <code><span class="highlight">8002</span></code>, etc.) to avoid using a port already in use by another process. <code><span class="highlight">server_username</span></code> is your username (i.e. sammy) on the Droplet which you created and <code><span class="highlight">your_server_ip</span></code> is the IP address of your Droplet. For example, for the username <code><span class="highlight">sammy</span></code> and the server address <code><span class="highlight">111.111.111.111</span></code>, the command would be:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">ssh -L <span class="highlight">8000</span>:localhost:<span class="highlight">8888</span> <span class="highlight">sammy</span>@<span class="highlight">111.111.111.111</span>
</li>
</ul>
</code></pre>
If no error shows up after running the <code>ssh -L</code> command, you can run Jupyter Notebook:<br />
<pre class="code-pre command"><code><ul class="prefixed">
<li class="line">jupyter notebook
</li>
</ul>
</code></pre>
Now, from a web browser on your local machine, open the Jupyter Notebook web interface with <code>http://localhost:<span class="highlight">8000</span></code> (or whatever port number you chose).<br />
<h3 id="ssh-tunneling-with-windows-and-putty">
SSH Tunneling with Windows and Putty</h3>
If you are using Windows, you can also easily create an SSH tunnel using Putty as outlined in <a href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-putty-on-digitalocean-droplets-windows-users">How To Use SSH Keys with PuTTY on DigitalOcean Droplets (Windows users)</a>. <br />
First, enter the server URL or IP address as the hostname as shown:<br />
<img alt="Set Hostname for SSH Tunnel" src="https://assets.digitalocean.com/articles/jupyter_notebook/set_hostname_putty.png" /><br />
Next, click <strong>SSH</strong> on the bottom of the left pane to expand the menu, and then click <strong>Tunnels</strong>. Enter the local port number to use to access Jupyter on your local machine. Choose <code><span class="highlight">8000</span></code> or greater (ie <code><span class="highlight">8001</span></code>, <code><span class="highlight">8002</span></code>, etc.) to avoid ports used by other services, and set the destination as <code>localhost:<span class="highlight">8888</span></code> where <code><span class="highlight">:8888</span></code> is the number of the port that Jupyter Notebook is running on. Now click the <strong>Add</strong> button, and the ports should appear in the <strong>Forwarded ports</strong> list:<br />
<img alt="Forwarded ports list" src="https://assets.digitalocean.com/articles/jupyter_notebook/forwarded_ports_putty.png" /><br />
Finally, click the <strong>Open</strong> button to connect to the server via SSH and tunnel the desired ports. Navigate to <code>http://localhost:<span class="highlight">8000</span></code> (or whatever port you chose) in a web browser to connect to Jupyter Notebook running on the server.<br />
<h2 id="step-5-—-using-jupyter-notebook">
Step 5 — Using Jupyter Notebook</h2>
This section goes over the basics of using Jupyter Notebook. By this
point you should have Jupyter Notebook running, and you should be
connected to it using a web browser. Jupyter Notebook is very powerful
and has many features. This section will outline a few of the basic
features to get you started using the notebook. Automatically, Jupyter
Notebook will show all of the files and folders in the directory it is
run from. <br />
To create a new notebook file, select <strong>New</strong> > <strong>Python 2</strong> from the top right pull-down menu:<br />
<div class="growable">
<img alt="Create a new Python 2 notebook" src="https://assets.digitalocean.com/articles/jupyter_notebook/create_python2_notebook.png" /></div>
This will open a notebook. We can now run Python code in the cell or
change the cell to markdown. For example, change the first cell to
accept Markdown by clicking <strong>Cell</strong> > <strong>Cell Type</strong> > <strong>Markdown</strong>
from the top navigation bar. We can now write notes using Markdown and
even include equations written in LaTeX by putting them between the <code>$$</code> symbols. For example, type the following into the cell after changing it to markdown:<br />
<pre class="code-pre "><code># Simple Equation
Let us now implement the following equation:
$$ y = x^2$$
where $x = 2$
</code></pre>
To turn the markdown into rich text, press <code>CTRL+ENTER</code>, and the following should be the results:<br />
<div class="growable">
<img alt="results of markdown" src="https://assets.digitalocean.com/articles/jupyter_notebook/markdown_results.png" /></div>
You can use the markdown cells to make notes and document your code.
Let's implement that simple equation and print the result. Select <strong>Insert</strong> > <strong>Insert Cell Below</strong> to insert and cell and enter the following code:<br />
<pre class="code-pre "><code>x = 2
y = x*x
print y
</code></pre>
To run the code, press <code>CTRL+ENTER</code>. The following should be the results:<br />
<div class="growable">
<img alt="simple equation results" src="https://assets.digitalocean.com/articles/jupyter_notebook/equations_results.png" /></div>
You now have the ability to include libraries and use the notebook as you would with any other Python development environment!<br />
<h2 id="conclusion">
Conclusion</h2>
Congratulations! You should be now able to write reproducible Python
code and notes using markdown using Jupyter notebook running on a
Droplet. To get a quick tour of Jupyter notebook, select <strong>Help</strong> > <strong>User Interface Tour</strong> from the top navigation menu.<br />
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com1tag:blogger.com,1999:blog-8063958573455141345.post-51726042434601573732016-08-25T21:56:00.001-07:002016-08-29T11:06:38.582-07:00R Script arguments from Command Line<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Passing RScript arguments from Command Line</h2>
<div>
There may be situations where you might want to execute R scripts from command line and passing appropriate arguments as batch. So here is an example below</div>
<div>
<br /></div>
<div>
<div>
C:\Users\UserName\Documents\></div>
<div>
"C:\Program Files\R\R-3.3.0\bin\RScript.exe" args.R 2016-08-01 28 1 30 > args.t</div>
</div>
<div>
<br /></div>
<div>
In the above I have specified the location of the RScript.exe on my local computer and args.R is the R script that I want to run from command line and the arguments are 2016-08-01 which is a Date in the format yyyy-mm-dd, then 28 and then 1 and 30 and the result of the execution will get saved in args.txt file which is the output of the execution.</div>
<div>
<br /></div>
<div>
Here is the sample R script code below</div>
<div>
<br /></div>
<div>
<div>
#READ THE ARGUMENTS</div>
<div>
args <- commandArgs(TRUE)</div>
<div>
<br /></div>
<div>
# test if there is at least one argument: if not, return an error</div>
<div>
if (length(args)==0) {</div>
<div>
stop("First parameter is a required argumenst.n", call.=FALSE)</div>
<div>
} </div>
<div>
<br /></div>
<div>
print(args)</div>
<div>
<br /></div>
<div>
#GETTING ARGUMENT OF THE R SCRIPT.</div>
<div>
startDate <- as.Date(args[1])</div>
<div>
sId <- eval(parse(text=args[2]))</div>
<div>
min <- eval(parse(text=args[3]))</div>
<div>
max <- eval(parse(text=args[4]))</div>
<div>
<br /></div>
<div>
#write the variables as observations of a dataframe</div>
<div>
columnNames <- c('sId', 'startDate', 'min', 'max')</div>
<div>
columnValues <- c(sId, startDate, min, max)</div>
<div>
df = data.frame(columnNames, columnValues)</div>
<div>
str(df)</div>
<div>
print(df)</div>
<div>
write.csv(df, file = "C:/args.csv", row.names=F)</div>
</div>
<div>
<br /></div>
<div>
The data frame created above gets saved as a csv file for review. Please note internally Date is represented as integer in R.</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com1tag:blogger.com,1999:blog-8063958573455141345.post-31339232333139134302016-08-17T11:25:00.000-07:002016-08-17T11:25:00.941-07:00Self-Signed Certificates with Microsoft Enhanced RSA and AES Cryptographic Provider<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Creating Enhanced SHA256 self-signed certificates</h2>
<div>
There are 2 options to create self-signed certificates very easily</div>
<div>
<br /></div>
<h3 style="text-align: left;">
using windows makecert</h3>
<div>
The following command can be run from the command prompt to create a self-signed certificate. Based on location of the makecert.exe on you machine, the path might differ. I am using a Windows 8.1</div>
<div>
<div>
"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\makecert.exe" -n "CN=Local" -r -pe -a sha256 -len 2048 -cy authority -e 03/03/2017 -sv Local.pvk Local.cer</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\pvk2pfx.exe" -pvk Local.pvk -spc Local.cer -pfx Local.pfx -po MyPassword -sy 24</div>
<div>
<br /></div>
<h3 style="text-align: left;">
using openSSL</h3>
<div>
you can use openSSL that comes with Apache Webserver to get the same thing done as follows</div>
<div>
<div>
<br /></div>
<div>
openssl.exe req -x509 -nodes -sha256 -days 3650 -subj "/CN=Local" -newkey rsa:2048 -keyout Local.key -out Local.crt</div>
<div>
<br /></div>
<div>
openssl.exe pkcs12 -export -in Local.crt -inkey Local.key -CSP "Microsoft Enhanced RSA and AES Cryptographic Provider" -out Local.pfx</div>
</div>
</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Difference Between Above two</h3>
<div>
One major and most important difference between the 2 above is makecert is not able to create the certificate file with CSP of 24 as provided as provided as parameter so while using this *pfx file to sign any XML as SHA256 will give exception like "Invalid Algorithm Specified" because the CSP value remains 1 instead of 24.</div>
<div>
<br /></div>
<div>
The one created by Open SSL will come out with correct CSP value and will give any errors.</div>
<div>
<br /></div>
<div>
<h3 style="text-align: left;">
Check Keys of Generated Certificate</h3>
<div>
You can write a small test program to test the Keys generated by the certificates in the above 2 methods.</div>
<div>
<br /></div>
<div>
<div>
class Program</div>
<div>
{</div>
<div>
static void Main(string[] args)</div>
<div>
{</div>
<div>
var x509Certificate = new X509Certificate2(@"Local.pfx", </div>
<div>
"LocalSTS", X509KeyStorageFlags.Exportable);</div>
<div>
Console.WriteLine(x509Certificate.ToString(true));</div>
<div>
Console.ReadLine();</div>
<div>
}</div>
<div>
}</div>
</div>
</div>
<h3 style="text-align: left;">
<br /></h3>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com5tag:blogger.com,1999:blog-8063958573455141345.post-78714351814488318182016-05-16T12:06:00.002-07:002016-05-16T12:06:54.481-07:00Azure SQL query slow performance<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Azure SQL Intermittently slow at intervals</h2>
If you do not have any maintenance job scheduled on the database, then you will need to run some maintenance queries to update the stats if your data in the database is having frequent inserts and updates. Here is how you can do it as below<br />
<br />
Connect to your database from SSMS (SQL Server Management Studio), or another client of your choosing.<br />
<br />
Update all your tables data distribution statistics, with a 100% sampling rate (Fullscan). This data is used by the query optimizer to choose an execution plan for the queries, and it’s vital that they are updated to get effective execution plans.<br />
<br />
---------------------------Update statistics of all database tables<br />
DECLARE @sql nvarchar(MAX);<br />
SELECT @sql = (SELECT 'UPDATE STATISTICS ' + DB_NAME() + '.' + rtrim(sc.name) + '.' + rtrim(so.name) + ' WITH FULLSCAN, ALL; '<br />
from sys.sysobjects so<br />
join sys.schemas sc<br />
on so.uid = sc.schema_id<br />
where so.xtype = 'U'<br />
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)');<br />
PRINT @sql<br />
EXEC (@sql)<br />
---------------------------------------------------------------------<br />
Then follow with a recompilation of all objects, by using sp_recompile.<br />
<br />
This causes stored procedures, triggers, and user-defined functions to be recompiled the next time that they are run. It does this by dropping the existing plan from the procedure cache forcing a new plan to be created the next time that the procedure or trigger is run. This ensures the new data distribution statistics or indexes are used in execution plans.<br />
----------------------------------------------------------------------<br />
--Force recompilation of all objects<br />
<br />
SET QUOTED_IDENTIFIER OFF<br />
DECLARE @sql nvarchar(MAX);<br />
SELECT @sql = (SELECT "EXEC sp_recompile '" + rtrim(sc.name) + "." + rtrim(so.name) + "' "<br />
from sys.sysobjects so<br />
join sys.schemas sc<br />
on so.uid = sc.schema_id<br />
where so.xtype = "U"<br />
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)');<br />
PRINT @sql<br />
EXEC (@sql)<br />
SET QUOTED_IDENTIFIER ON<br />
----------------------------------------------------------------------<br />
References<br />
<br />
UPDATE STATISTICS (Transact-SQL)<br />
<br />
https://msdn.microsoft.com/en-us/library/ms187348.aspx<br />
<br />
sp_recompile (Transact-SQL)<br />
<br />
https://msdn.microsoft.com/en-us/library/ms181647.aspx </div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-35585412297244669322015-02-18T16:29:00.002-08:002015-02-18T16:30:09.061-08:00jquery ui autosuggest dropdown in ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Jquery UI Autosugget dropdown in ASP.NET</h2>
<div>
Very recently, I came across a requirement where I have a conventional ASP.NET site which has a dropdown, I have to make</div>
<div>
1. Enabling filtering on the dropdown by its bound text.</div>
<div>
2. Provide another search field which will enable search by the Id and the same will get selected in the auto suggest dropdown.</div>
<div>
<br /></div>
<div>
Problem : The default jquery ui events in the demos of their official site will not have this functionality, since its an existing dropdown which is used in numerous places in the code behind, its not really advisable to change the code behind.</div>
<div>
<br /></div>
<div>
Solution : We know for sure that the dropdown is bound to values in the code behind, all we have to do is to enable filtering by its text, which is pretty straight forward with the jquery ui in play.</div>
<div>
<br /></div>
<div>
Now coming to 2nd part, here we have to bind the source of the search field by the values of the dropdown that is already bound in the code behind. Then we have to enable search by id not by the text and select the appropriate dropdown item on select of the search field.</div>
<div>
<br /></div>
<div>
The following code snippet just does that.</div>
<div>
<br /></div>
<div>
First bind the dropdown in the code behind, (please note that this already existed)</div>
<div>
<div>
//bind dropdown</div>
<div>
ddLangs.Items.Clear();</div>
<div>
ddLangs.Items.Add(new ListItem("select", "0"));</div>
<div>
ddLangs.Items.Add(new ListItem("vb.net", "1"));</div>
<div>
ddLangs.Items.Add(new ListItem("c#", "2"));</div>
<div>
ddLangs.Items.Add(new ListItem("java", "3"));</div>
<div>
ddLangs.Items.Add(new ListItem("pascal", "4"));</div>
</div>
<div>
<br /></div>
<div>
Secondly Add the Search elements to the form and enclose the existing dropdown in a div and style class</div>
<div>
<br /></div>
<div>
<div>
<p></div>
<div>
Search lang Id:<input type="text" id="autoLangs" style="width:500px;" /><input type="hidden" id="autoLangId" /></div>
<div>
<span id="autoResult"></span></div>
<div>
</p></div>
<div>
</div>
<div>
Languages : <div class="ui-widget"><asp:DropDownList ID="ddLangs" runat="server"></asp:DropDownList></div></div>
</div>
<div>
<br /></div>
<div>
Thirdly add the javscript starting with the reference to the UI and Jquery libraries needed to refer and then the custom style and coding, I have highlighted the calls that initiates the combobox feature and the search by ID feature.</div>
<div>
<div>
<br /></div>
<div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script></div>
<div>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" /></div>
<div>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div>
<style></div>
<div>
.ui-autocomplete { </div>
<div>
/* these sets the height and width */</div>
<div>
max-height:200px; </div>
<div>
max-width: 500px; </div>
<div>
<br /></div>
<div>
/* these make it scroll for anything outside */</div>
<div>
overflow-x:auto;</div>
<div>
overflow-y:auto;</div>
<div>
}</div>
<div>
.custom-combobox {</div>
<div>
position: relative;</div>
<div>
display: inline-block;</div>
<div>
}</div>
<div>
.custom-combobox-toggle {</div>
<div>
position: absolute;</div>
<div>
top: 0;</div>
<div>
bottom: 0;</div>
<div>
margin-left: -1px;</div>
<div>
padding: 0;</div>
<div>
}</div>
<div>
.custom-combobox-input {</div>
<div>
margin: 0;</div>
<div>
padding: 5px 10px;</div>
<div>
}</div>
<div>
</style></div>
<div>
<script type="text/javascript"></div>
<div>
$(document).ready(function () {</div>
<div>
(function ($) {</div>
<div>
$.widget("custom.combobox", {</div>
<div>
_create: function () {</div>
<div>
this.wrapper = $("<span>")</div>
<div>
.addClass("custom-combobox")</div>
<div>
.insertAfter(this.element);</div>
<div>
<br /></div>
<div>
this.element.hide();</div>
<div>
this._createAutocomplete();</div>
<div>
this._createShowAllButton();</div>
<div>
},</div>
<div>
<br /></div>
<div>
_createAutocomplete: function () {</div>
<div>
var selected = this.element.children(":selected"),</div>
<div>
value = selected.val() ? selected.text() : "";</div>
<div>
<br /></div>
<div>
this.input = $("<input>")</div>
<div>
.appendTo(this.wrapper)</div>
<div>
.val(value)</div>
<div>
.attr("title", "")</div>
<div>
.addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")</div>
<div>
.css("width", "500px")</div>
<div>
.autocomplete({</div>
<div>
delay: 0,</div>
<div>
minLength: 0,</div>
<div>
source: $.proxy(this, "_source")</div>
<div>
})</div>
<div>
.tooltip({</div>
<div>
tooltipClass: "ui-state-highlight"</div>
<div>
});</div>
<div>
<br /></div>
<div>
this._on(this.input, {</div>
<div>
autocompleteselect: function (event, ui) {</div>
<div>
//alert('select fired');</div>
<div>
//search ruleId text box value set as dropRules</div>
<div>
$("#autoLangs").val('');</div>
<div>
$("#autoLangId").val('0');</div>
<div>
$("#autoResult").text('');</div>
<div>
<br /></div>
<div>
ui.item.option.selected = true;</div>
<div>
this._trigger("select", event, {</div>
<div>
item: ui.item.option</div>
<div>
});</div>
<div>
},</div>
<div>
<br /></div>
<div>
autocompletechange: "_removeIfInvalid"</div>
<div>
});</div>
<div>
},</div>
<div>
<br /></div>
<div>
_createShowAllButton: function () {</div>
<div>
var input = this.input,</div>
<div>
wasOpen = false;</div>
<div>
<br /></div>
<div>
$("<a>")</div>
<div>
.attr("tabIndex", -1)</div>
<div>
.attr("title", "Show All Items")</div>
<div>
.tooltip()</div>
<div>
.appendTo(this.wrapper)</div>
<div>
.button({</div>
<div>
icons: {</div>
<div>
primary: "ui-icon-triangle-1-s"</div>
<div>
},</div>
<div>
text: false</div>
<div>
})</div>
<div>
.removeClass("ui-corner-all")</div>
<div>
.addClass("custom-combobox-toggle ui-corner-right")</div>
<div>
.mousedown(function () {</div>
<div>
wasOpen = input.autocomplete("widget").is(":visible");</div>
<div>
})</div>
<div>
.click(function () {</div>
<div>
input.focus();</div>
<div>
<br /></div>
<div>
// Close if already visible</div>
<div>
if (wasOpen) {</div>
<div>
return;</div>
<div>
}</div>
<div>
<br /></div>
<div>
// Pass empty string as value to search for, displaying all results</div>
<div>
input.autocomplete("search", "");</div>
<div>
});</div>
<div>
},</div>
<div>
<br /></div>
<div>
_source: function (request, response) {</div>
<div>
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");</div>
<div>
response(this.element.children("option").map(function () {</div>
<div>
var text = $(this).text();</div>
<div>
if (this.value && (!request.term || matcher.test(text)))</div>
<div>
return {</div>
<div>
label: text,</div>
<div>
value: text,</div>
<div>
option: this</div>
<div>
};</div>
<div>
}));</div>
<div>
},</div>
<div>
<br /></div>
<div>
_removeIfInvalid: function (event, ui) {</div>
<div>
try {</div>
<div>
<br /></div>
<div>
// Selected an item, nothing to do</div>
<div>
if (ui.item) {</div>
<div>
return;</div>
<div>
}</div>
<div>
<br /></div>
<div>
// Search for a match (case-insensitive)</div>
<div>
var value = this.input.val(),</div>
<div>
valueLowerCase = value.toLowerCase(),</div>
<div>
valid = false;</div>
<div>
this.element.children("option").each(function () {</div>
<div>
if ($(this).text().toLowerCase() === valueLowerCase) {</div>
<div>
this.selected = valid = true;</div>
<div>
return false;</div>
<div>
}</div>
<div>
});</div>
<div>
<br /></div>
<div>
// Found a match, nothing to do</div>
<div>
if (valid) {</div>
<div>
return;</div>
<div>
}</div>
<div>
<br /></div>
<div>
// Remove invalid value</div>
<div>
this.input</div>
<div>
.val("")</div>
<div>
.attr("title", value + " didn't match any item")</div>
<div>
.tooltip("open");</div>
<div>
this.element.val("");</div>
<div>
this._delay(function () {</div>
<div>
this.input.tooltip("close").attr("title", "");</div>
<div>
}, 2500);</div>
<div>
this.input.autocomplete("instance").term = "";</div>
<div>
}</div>
<div>
catch (e) {</div>
<div>
}</div>
<div>
<br /></div>
<div>
},</div>
<div>
autocomplete: function (searchVal, searchTxt) {</div>
<div>
this.element.val(searchVal);</div>
<div>
this.input.val(searchTxt);</div>
<div>
// Search for a match (case-insensitive)</div>
<div>
var value = this.input.val(),</div>
<div>
valueLowerCase = value.toLowerCase(),</div>
<div>
valid = false;</div>
<div>
this.element.children("option").each(function () {</div>
<div>
if ($(this).text().toLowerCase() === valueLowerCase) {</div>
<div>
this.selected = valid = true;</div>
<div>
return false;</div>
<div>
}</div>
<div>
});</div>
<div>
// Found a match, nothing to do</div>
<div>
if (valid) {</div>
<div>
return;</div>
<div>
}</div>
<div>
},</div>
<div>
<br /></div>
<div>
_destroy: function () {</div>
<div>
this.wrapper.remove();</div>
<div>
this.element.show();</div>
<div>
}</div>
<div>
});</div>
<div>
})(jQuery);</div>
<div>
<br /></div>
<div>
$(function () {</div>
<div>
<b> $('#<%= ddLangs.ClientID %>').combobox();</b></div>
<div>
<b> bindAutoSuggest();</b></div>
<div>
});</div>
<div>
<br /></div>
<div>
</div>
<div>
});</div>
<div>
<br /></div>
<div>
//bind auto suggest search</div>
<div>
function bindAutoSuggest() {</div>
<div>
var rules = [];</div>
<div>
var rule = { value: "", label: "", desc: "" };</div>
<div>
<br /></div>
<div>
$('#<%= ddLangs.ClientID %>' + ' option').each(function () {</div>
<div>
var rule = { value: $(this).val(), label: $(this).val() + '-' + $(this).text(), desc: $(this).text() }</div>
<div>
rules.push(rule);</div>
<div>
});</div>
<div>
<br /></div>
<div>
$("#autoLangs").autocomplete({</div>
<div>
minLength: 0,</div>
<div>
source: rules,</div>
<div>
focus: function (event, ui) {</div>
<div>
$("#autoLangs").val(ui.item.label);</div>
<div>
return false;</div>
<div>
},</div>
<div>
select: function (event, ui) {</div>
<div>
$("#autoLangs").val(ui.item.label);</div>
<div>
$("#autoLangId").val(ui.item.value);</div>
<div>
$("#autoResult").text('You selected ruleId : ' + $("#autoLangId").val());</div>
<div>
var selectedRuleId = $("#autoLangId").val();</div>
<div>
var selectedText = ui.item.desc;</div>
<div>
$('#<%= ddLangs.ClientID %>').combobox('autocomplete', selectedRuleId, selectedText);</div>
<div>
return false;</div>
<div>
}</div>
<div>
});</div>
<div>
}</div>
<div>
<br /></div>
<div>
</div>
<div>
</div>
<div>
</script></div>
</div>
<div>
<br /></div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-36724591485104689202014-11-19T14:56:00.001-08:002014-11-19T15:09:35.821-08:00asp.net mvc 4 json post max limit problem<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Sending Long JSON complex objects content length problem</h2>
<div>
While working on a project, I was faced with a very difficult to resolve issue. At first it was very difficult to detect what was wrong and then what boiled down to was that my JSON complex object that I was POSTing back to the server on ajax call was too big and hence the server was not able to handle it.</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Problem</h3>
<div>
In my scenario, the call from the client side was an ajax post call to the MVC controller and the mvc site is actually running as a subsite of the main asp.net website. An initial search on the google will give you resolutions like below</div>
<div>
<br /></div>
<div>
<div>
var jsonResult = Json(dataPlot, JsonRequestBehavior.AllowGet);</div>
<div>
jsonResult.maxJsonLength = int.MaxValue;</div>
<div>
return jsonResult;</div>
<div>
<br /></div>
<div>
Possible configuration change</div>
<div>
<br /></div>
<div>
<configuration></div>
<div>
<system.web.extensions></div>
<div>
<scripting> </div>
<div>
<webServices> </div>
<div>
<jsonSerialization maxJsonLength="1000000" /> </div>
<div>
</webServices></div>
<div>
</scripting></div>
<div>
</system.web.extensions></div>
<div>
</configuration></div>
</div>
<div>
<br /></div>
<div>
But believe me, none of the above will resolve the issue as the inbuilt Javascript serializer is not going to check this setting while serializing and deserializing data to the object.</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Solution</h3>
<div>
I came across a <a href="http://erraticdev.blogspot.com/2010/12/sending-complex-json-objects-to-aspnet.html">blogpost </a>from a gentleman called Robert and his explanation seemed very good. I went with the jquery plugin that he wrote and it actually resolved my issue. I am going to paste the jquery plugin that he wrote and also how I have called it in my code to resolve it is pasted below.</div>
<div>
<br /></div>
<h4 style="text-align: left;">
Call from my Code</h4>
<div>
<div>
Here you can see below I have commented the datatype and content type of Json and used the plugin to convert it to a dictionary of object.</div>
<div>
$.ajax({</div>
<div>
url: '@Url.Action("save", "CmpgnView")',</div>
<div>
type: 'POST',</div>
<div>
//dataType: 'json',</div>
<div>
//contentType: 'application/json; charset=utf-8',</div>
<div>
//data: JSON.stringify(reqObj),</div>
<div>
data: $.toDictionary(reqObj),</div>
<div>
async: false,</div>
<div>
success: function (data) {</div>
<div>
//do something</div>
<div>
},</div>
<div>
error: function (data, status, xhr) {</div>
<div>
alert("Error: " + xhr.status + " : " + xhr.statusText);</div>
<div>
}</div>
<div>
});</div>
</div>
<div>
<br /></div>
<h4 style="text-align: left;">
Controller Method on Server Side</h4>
<div>
<div>
[HttpPost]</div>
<div>
public async Task<ActionResult> save(Req req)</div>
<div>
{</div>
<div>
string userId = UserId;</div>
<div>
Response result = await Task.Run(() =></div>
<div>
{</div>
<div>
return validateAndSave(req, userId);</div>
<div>
});</div>
<div>
return Json(result);</div>
<div>
</div>
<div>
}</div>
</div>
<div>
<br /></div>
<h4 style="text-align: left;">
Jquery Plugin Code from this <a href="http://erraticdev.blogspot.com/2010/12/sending-complex-json-objects-to-aspnet.html">WebSite</a></h4>
<div>
Please also read the explanations given by Robert, its excellent from his blog</div>
<div>
(http://erraticdev.blogspot.com/2010/12/sending-complex-json-objects-to-aspnet.html)</div>
<div>
<br /></div>
<div>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"><span class="rem" style="color: green;">/*!</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"><span class="rem" style="color: green;"> * jQuery toDictionary() plugin</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"><span class="rem" style="color: green;"> *</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"><span class="rem" style="color: green;"> * Version 1.2 (11 Apr 2011)</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"><span class="rem" style="color: green;"> *</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"><span class="rem" style="color: green;"> * Copyright (c) 2011 Robert Koritnik</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"><span class="rem" style="color: green;"> * Licensed under the terms of the MIT license</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"><span class="rem" style="color: green;"> * http://www.opensource.org/licenses/mit-license.php</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"><span class="rem" style="color: green;"> */</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> </pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;">(<span class="kwrd" style="color: blue;">function</span> ($) {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> </pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">// #region String.prototype.format</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">// add String prototype format function if it doesn't yet exist</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> ($.isFunction(String.<span class="kwrd" style="color: blue;">prototype</span>.format) === <span class="kwrd" style="color: blue;">false</span>)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> String.<span class="kwrd" style="color: blue;">prototype</span>.format = <span class="kwrd" style="color: blue;">function</span> () {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> s = <span class="kwrd" style="color: blue;">this</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> i = arguments.length;</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">while</span> (i--)</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> s = s.replace(<span class="kwrd" style="color: blue;">new</span> RegExp(<span class="str" style="color: #cc0000;">"\\{"</span> + i + <span class="str" style="color: #cc0000;">"\\}"</span>, <span class="str" style="color: #cc0000;">"gim"</span>), arguments[i]);</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">return</span> s;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> };</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> }</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">// #endregion</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> </pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">// #region Date.prototype.toISOString</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">// add Date prototype toISOString function if it doesn't yet exist</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> ($.isFunction(Date.<span class="kwrd" style="color: blue;">prototype</span>.toISOString) === <span class="kwrd" style="color: blue;">false</span>)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> Date.<span class="kwrd" style="color: blue;">prototype</span>.toISOString = <span class="kwrd" style="color: blue;">function</span> () {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> pad = <span class="kwrd" style="color: blue;">function</span> (n, places) {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> n = n.toString();</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">for</span> (<span class="kwrd" style="color: blue;">var</span> i = n.length; i < places; i++)</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> n = <span class="str" style="color: #cc0000;">"0"</span> + n;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">return</span> n;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> };</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> d = <span class="kwrd" style="color: blue;">this</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">return</span> <span class="str" style="color: #cc0000;">"{0}-{1}-{2}T{3}:{4}:{5}.{6}Z"</span>.format(</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> d.getUTCFullYear(),</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> pad(d.getUTCMonth() + 1, 2),</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> pad(d.getUTCDate(), 2),</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> pad(d.getUTCHours(), 2),</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> pad(d.getUTCMinutes(), 2),</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> pad(d.getUTCSeconds(), 2),</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> pad(d.getUTCMilliseconds(), 3)</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> );</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> };</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">// #endregion</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> </pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> _flatten = <span class="kwrd" style="color: blue;">function</span> (input, output, prefix, includeNulls) {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> ($.isPlainObject(input))</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">for</span> (<span class="kwrd" style="color: blue;">var</span> p <span class="kwrd" style="color: blue;">in</span> input)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> (includeNulls === <span class="kwrd" style="color: blue;">true</span> || <span class="kwrd" style="color: blue;">typeof</span> (input[p]) !== <span class="str" style="color: #cc0000;">"undefined"</span> && input[p] !== <span class="kwrd" style="color: blue;">null</span>)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> _flatten(input[p], output, prefix.length > 0 ? prefix + <span class="str" style="color: #cc0000;">"."</span> + p : p, includeNulls);</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> }</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> }</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">else</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> ($.isArray(input))</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> $.each(input, <span class="kwrd" style="color: blue;">function</span> (index, value) {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> _flatten(value, output, <span class="str" style="color: #cc0000;">"{0}[{1}]"</span>.format(prefix, index));</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> });</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">return</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> (!$.isFunction(input))</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> (input <span class="kwrd" style="color: blue;">instanceof</span> Date)</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> output.push({ name: prefix, value: input.toISOString() });</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">else</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> val = <span class="kwrd" style="color: blue;">typeof</span> (input);</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">switch</span> (val)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">case</span> <span class="str" style="color: #cc0000;">"boolean"</span>:</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">case</span> <span class="str" style="color: #cc0000;">"number"</span>:</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> val = input;</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">break</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">case</span> <span class="str" style="color: #cc0000;">"object"</span>:</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">// this property is null, because non-null objects are evaluated in first if branch</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> (includeNulls !== <span class="kwrd" style="color: blue;">true</span>)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">return</span>;</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> }</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">default</span>:</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> val = input || <span class="str" style="color: #cc0000;">""</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> output.push({ name: prefix, value: val });</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> }</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> };</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> </pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> $.extend({</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> toDictionary: <span class="kwrd" style="color: blue;">function</span> (data, prefix, includeNulls) {</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">/// <summary>Flattens an arbitrary JSON object to a dictionary that Asp.net MVC default model binder understands.</summary></span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">/// <param name="data" type="Object">Can either be a JSON object or a function that returns one.</data></span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">/// <param name="prefix" type="String" Optional="true">Provide this parameter when you want the output names to be prefixed by something (ie. when flattening simple values).</param></span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">/// <param name="includeNulls" type="Boolean" Optional="true">Set this to 'true' when you want null valued properties to be included in result (default is 'false').</param></span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> </pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">// get data first if provided parameter is a function</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> data = $.isFunction(data) ? data.call() : data;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> </pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="rem" style="color: green;">// is second argument "prefix" or "includeNulls"</span></pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">if</span> (arguments.length === 2 && <span class="kwrd" style="color: blue;">typeof</span> (prefix) === <span class="str" style="color: #cc0000;">"boolean"</span>)</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> {</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> includeNulls = prefix;</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> prefix = <span class="str" style="color: #cc0000;">""</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> }</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> </pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="rem" style="color: green;">// set "includeNulls" default</span></pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> includeNulls = <span class="kwrd" style="color: blue;">typeof</span> (includeNulls) === <span class="str" style="color: #cc0000;">"boolean"</span> ? includeNulls : <span class="kwrd" style="color: blue;">false</span>;</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> </pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">var</span> result = [];</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> _flatten(data, result, prefix || <span class="str" style="color: #cc0000;">""</span>, includeNulls);</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> </pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> <span class="kwrd" style="color: blue;">return</span> result;</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;"> }</pre>
<pre class="alt" style="background-color: #f4f4f4; font-size: 11px; line-height: 16px; width: 613px; word-wrap: normal;"> });</pre>
<pre style="background-color: white; font-size: 11px; line-height: 16px; word-wrap: normal;">})(jQuery);</pre>
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-5333837483978007232014-10-15T21:18:00.002-07:002014-10-15T21:18:58.291-07:00Reading XML in SQL Server 2005/2008<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
XML Processing in SQL Server</h2>
<div>
SQL Server has very good support for reading/processing XML. Here are some of the examples below with different XML structures. I found it extremely handy when using them for passing as collections to stored procedures.</div>
<div>
<br /></div>
<div>
Example 1: Entire data is passed as an attribute so that the XML is short formed. The below example shows how to read the element name along with the attribute value.</div>
<div>
<br /></div>
<div>
SQL : DECLARE @xml XML = </div>
<div>
'<root></div>
<div>
<offer old="old1" new="new1" module="1" /></div>
<div>
<offer old="old2" new="new2" module="2" /></div>
<div>
</root>'</div>
<div>
<br /></div>
<div>
SELECT T.c.value('local-name(.)[1]', 'varchar(100)') AS Element,</div>
<div>
T.c.value('@new', 'varchar(20)') as New</div>
<div>
FROM @xml.nodes('/root/offer') T(c);</div>
<div>
<br /></div>
<div>
Example 2: XML had Node value and as well as attributes</div>
<div>
SQL : </div>
<div>
<div>
DECLARE @xml XML = </div>
<div>
'</div>
<div>
<root></div>
<div>
<item></div>
<div>
<node1 offer="1">2.09</node1></div>
<div>
<node2 offer="2">2.97</node2></div>
<div>
</item></div>
<div>
</root></div>
<div>
'</div>
<div>
<br /></div>
<div>
SELECT T.c.value('local-name(.)[1]', 'varchar(100)') AS Element,</div>
<div>
T.c.value('./text()[1]', 'decimal(17,2)') AS Value,</div>
<div>
T.c.value('@offer', 'varchar(20)') as Offer</div>
<div>
FROM @xml.nodes('//item/child::node()') T(c);</div>
</div>
<div>
<br /></div>
<div>
Both the above examples (xml structures), have been very effective and useful ones for me. </div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-91263851692964253972014-04-04T09:19:00.001-07:002014-04-04T09:20:24.601-07:00Reading ASP.NET Checkboxlist selected text using Jquery<div dir="ltr" style="text-align: left;" trbidi="on">
<h2>
Reading Checkbox list selected items using jquery</h2>
<div>
<div>
*I have posted the same on StackOverflow.</div>
<div>
<br /></div>
<div>
I solved the problem in the following way. My ASP.NET code for the checkboxlist is as below</div>
<div>
</div>
<div>
<asp:CheckBoxList ID="chkHourly" runat="server" RepeatLayout="Table" </div>
<div>
RepeatColumns="4" RepeatDirection="Horizontal"></div>
<div>
<asp:ListItem Value="0">00:00 AM</asp:ListItem></div>
<div>
<asp:ListItem Value="1">01:00 AM</asp:ListItem></div>
<div>
<asp:ListItem Value="2">02:00 AM</asp:ListItem></div>
<div>
</asp:CheckBoxList> </div>
<div>
<br /></div>
<div>
The generated HTML will look like this below</div>
<div>
<br /></div>
<div>
<table id="ctl00_chkHourly" border="0"></div>
<div>
<TBODY></div>
<div>
<TR></div>
<div>
<TD></div>
<div>
<INPUT id=ctl00_chkHourly_0 name=ctl00$chkHourly$0 value="" CHECKED type=checkbox> </div>
<div>
<LABEL for=ctl00_chkHourly_0>00:00 AM</LABEL></TD></div>
<div>
<TD></div>
<div>
<INPUT id=ctl00_chkHourly_1 name=ctl00$chkHourly$1 value="" type=checkbox></div>
<div>
<LABEL for=ctl00_chkHourly_1>01:00 AM</LABEL></TD></div>
<div>
<TD></div>
<div>
<INPUT id=ctl00_chkHourly_2 name=ctl00$chkHourly$2 value="" type=checkbox></div>
<div>
<LABEL for=ctl00_chkHourly_2>02:00 AM</LABEL></div>
<div>
</TD></div>
<div>
</TR></div>
<div>
</TBODY></div>
<div>
<br /></div>
<div>
Please notice that there is a label created besides each input in the table, and when a checkbox is checked, the input's value will be 'on' and what you see as an option is the label's text, in my case I needed the text, but to get the value also in a round about away, I would read the name of the individual input fields that are checked. Please see the code below that I have written to read the text selected and also the name of the input selected so that I can strip it and read the value if needed.</div>
<div>
<br /></div>
<div>
var postData = new Array();</div>
<div>
$("[id*=chkHourly] input[type=checkbox]:checked").each(function () {</div>
<div>
alert($(this).next().text());</div>
<div>
alert($(this).next().html());</div>
<div>
alert($(this).attr("name"));</div>
<div>
postData.push($(this).next().text());</div>
<div>
});</div>
<div>
</div>
<div>
if (postData.length > 0) {</div>
<div>
alert("Selected Text(s): " + postData);</div>
<div>
} </div>
<div>
else {</div>
<div>
alert("No item has been selected.");</div>
<div>
}</div>
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-54633053690456638322014-02-26T15:16:00.003-08:002016-08-27T16:02:35.994-07:00Ajax FileUpload on all Browsers including IE 8, 10<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
File Upload in ASP.NET MVC via ajax</h2>
<h3 style="text-align: left;">
Problem</h3>
<div>
While looking for fileupload options via ajax and without posting the whole form, we often come across code in the internet using FormData API, which works perfectly fine on chrome and mozilla but on on IE. so a code snippet using a FormData API would like this below</div>
<div>
<br /></div>
<div>
//View Code</div>
<div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<script type="text/javascript"></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
function save() {</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
$("#test").submit();</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
}</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
function submitForm() {</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
var formData = new FormData($('#test')[0]);</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<br /></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
$.ajax({</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
url: '@Url.Action("Upload","<wbr></wbr>FileUpload")',</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
type: 'POST',</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
data: formData,</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
async: false,</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
success: function (data) {</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
alert('posted')</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
},</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
cache: false,</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
contentType: false,</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
processData: false</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
});</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<br /></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
return false;</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
}</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
</script></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<h2>Index</h2></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<input type="button" value="Submit" onclick="save();" /></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<form id="test" action="javascript:submitForm(<wbr></wbr>);" method="post" enctype = "multipart/form-data"></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<div></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<label for="fileUpload">File upload</label></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<input type="file" id="fileUpload" name="fileUpload" /></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
</div></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
</form></div>
</div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<br /></div>
<div style="background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
//Controller Code</div>
<div style="background-color: white;">
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
public ActionResult Upload()</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
{</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
HttpPostedFileBase postedFile = Request.Files[0];</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
return View();</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
}</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
The above snipped works fine on chrome and mozilla and you will be able to see the postedFile in the controller but it does not work on IE because most versions of IE does not support FormData.</div>
<h3 style="color: #222222; font-family: arial, sans-serif; font-size: 13px; text-align: left;">
Solution</h3>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
So the solution I came up with after going some recommendations over internet about HTML controls on different browsers is that its better to use iframes. So Idea is to point the target of the form to an iframe and even bind a load event to the iframe so that you know when the upload is finished and write additional jquery functions. Also you can even hide the iframe and not show the user. But this solution works on IE as well. The code is as below</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<br /></div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
The code also shows how to post additional data along with the file post.</div>
<div style="color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<br /></div>
<div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">@{</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> ViewBag.Title = "Index";</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">}</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><script src="~/scripts/jquery-1.9.1.min_.js"></script></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><script type="text/javascript"></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> function successFunction() {</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> alert($('#my_iframe').contents().find('p').html());</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> }</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> function redirect() {</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> //debugger;</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> document.getElementById('my_form').target = 'my_iframe'; //'my_iframe' is the name of the iframe</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> //document.getElementById('my_form').submit();</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> var callback = function () {</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> if (successFunction)</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> successFunction();</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> $('#</span><span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">my_iframe</span><span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">').unbind('load', callback);</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> };</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><br /></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> $('#my_iframe').bind('load', callback);</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> $('#hfParam').val('id:1');</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><br /></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> $('#my_form').submit();</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> //$("#my_form").trigger("submit");</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> </span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> }</span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"></script></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><h2>Index</h2></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><input type="button" name="action" value="Upload" onclick="redirect();"/></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><form id="my_form" name="my_form" action="/FileUpload/UploadFile" method="POST" enctype="multipart/form-data" ></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> <div id="main"></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> <input name="my_hidden" id="hfParam" type="hidden" /></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> <input name="my_files" id="my_file" type="file" /></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> <iframe id='my_iframe' name='my_iframe' src=""></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> </iframe></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> <div id="someDiv"></div></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> </div></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><br /></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"></form></span></div>
</div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"><br /></span></div>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"></span><br />
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">[HttpPost]</span></div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">
</span>
<br />
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> public ActionResult UploadFile()</span></div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">
</span>
<br />
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> {</span></div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">
</span>
<div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;"> ContentResult result = new ContentResult() { Content = "<p></p>", ContentType = "text/html"};</span></div>
<span style="color: #222222; font-family: "arial" , sans-serif; font-size: x-small;">
<div>
HttpPostedFileBase postedFile = Request.Files[0];</div>
<div>
try</div>
<div>
{</div>
<div>
result.Content = "<p>" + postedFile.FileName + "</p>";</div>
<div>
<br /></div>
<div>
}</div>
<div>
catch (System.Exception ex)</div>
<div>
{</div>
<div>
result.Content = ex.Message;</div>
<div>
}</div>
<div>
return result;</div>
<div>
}</div>
</span></div>
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com25tag:blogger.com,1999:blog-8063958573455141345.post-47810240569934004952014-02-20T21:16:00.001-08:002014-02-20T21:16:11.798-08:00JQuery Set Visibility ON/OFF for a Button in ASP.NET MVC<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
How to Make a Button Visible true or False of a button in ASP.NET MVC project</h2>
<div>
I posted the same solution on stackoverflow site</div>
<div>
<br /></div>
<div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
First Method: As explained above, but I am afraid, it will not work if for example you have a partial view and based on something there you want to show or hide something.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Second Method:Initial Page load, button is visible.</div>
<pre class="lang-cs prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">input id</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'btnAdd'</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> type</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'button'</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> value</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'Add'</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> style</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'display:block;'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">/></span></code></pre>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Based on some action on page/partial view</div>
<pre class="lang-cs prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;"><script></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
function disableAdd</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
$</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'#btnAdd'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">hide</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"></</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">script</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">></span></code></pre>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
*Please note, jquery will not be able to hide/show if you use visibility in style sheet.</div>
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-70229455137355980782014-02-20T21:10:00.002-08:002014-02-20T21:11:08.580-08:00Readonly TextBox using @Html.TextAreaFor<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
How to Create Conditional Readonly TextBox in ASP.NET MVC5</h2>
<div>
<b>I posted this solution below on stackoverflow because it worked very well for me, so copying from there</b></div>
<div>
<b><br /></b></div>
<div>
<b></b><br />
<div>
<b>By setting readonly attribute to either true or false is not going to work in most browsers, I have done it as below, when the mode of the page is "reload", I've not included "readonly" attribute.</b></div>
<b>
<div>
<br /></div>
<div>
@if(Model.Mode.Equals("edit")){</div>
<div>
@Html.TextAreaFor(model => Model.Content.Data, new { id = "modEditor", @readonly = moduleEditModel.Content.ReadOnly, @style = "width:99%; height:360px;" })</div>
<div>
}</div>
<div>
@if (Model.Mode.Equals("reload")){</div>
<div>
@Html.TextAreaFor(model => Model.Content.Data, new { id = "modEditor", @style = "width:99%; height:360px;" })}</div>
</b></div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-34875290049357929942014-02-20T21:04:00.001-08:002014-02-20T21:12:05.269-08:00Json Object Postback, IE JSON.stringfy problem resolution.<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: center;">
Making Json Object Post to Server in Asp.NET, Problem in IE.</h2>
<div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
I am copying my own post from StackOverflow.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
There may be situations where it may not be possible to include the Doctype or meta tag or nothing might work as in my case so I had to figure out this way below as explained.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
To post json objects back to the server, json.strinfy will have to be supported. To support the same on IE, please download json2.js from <a href="https://github.com/douglascrockford/JSON-js" rel="nofollow" style="background-color: transparent; border: 0px; color: #4a6b82; cursor: pointer; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">https://github.com/douglascrockford/JSON-js</a> and refer in your view. The following code snipped worked for me, I hope it helpe someone else too.</div>
<pre class="default prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="com" style="background-color: transparent; border: 0px; color: grey; margin: 0px; padding: 0px; vertical-align: baseline;">//include jquery library from your preferred cdn or local drive.</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><!--</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> include json2</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">js only </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">when</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> you need JSON</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">stringfy method </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">--></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">script type</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"text/javascript"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> src</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"~/scripts/json2.js"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">></</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">script</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">script type</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"text/javascript"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">function</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> button_click</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background-color: transparent; border: 0px; color: grey; margin: 0px; padding: 0px; vertical-align: baseline;">//object to post back to the server.</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> postData </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"Id"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> $</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'#hfUserId'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">val</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(),</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"Name"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> $</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'#Name'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">text</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(),</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"address"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Array</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">};</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> address </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Array</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> addr</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
addr </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"HouseNo"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"1"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"Street"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"ABC"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"City"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"Chicago"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"State"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"IL"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">};</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
address</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="lit" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">]</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> addr</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
addr </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"HouseNo"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"2"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"Street"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"DEF"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"City"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"Hoffman Est"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"State"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"IL"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">};</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
address</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="lit" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">]</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> addr</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background-color: transparent; border: 0px; color: grey; margin: 0px; padding: 0px; vertical-align: baseline;">//make ajax call to server to save the data</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
$</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">ajax</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">({</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
url</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'@Url.Action("myAction", "MyController")'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
type</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'POST'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
dataType</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'json'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
contentType</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'application/json; charset=utf-8'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
data</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> JSON</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">stringify</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">postData</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">),</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
async</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">true</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
success</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">function</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">data</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> alert</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'User Saved'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">},</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
error</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">function</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">data</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> alert</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">'Could not save User'</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">});</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"></</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">script</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">></span></code></pre>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
The model for the address list will be as below. Please note that the property names are the same as the addr object and it has get and set.</div>
<pre class="default prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">class</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Address</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">HouseNo</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">get</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">set</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Street</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">get</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">set</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">City</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">get</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">set</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">State</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">get</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">set</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span></code></pre>
<pre class="default prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin-bottom: 1em; padding: 0px; vertical-align: baseline; white-space: normal;">
The controller action will be something like below</div>
<pre class="default prettyprint prettyprinted" style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">HttpPost</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">]</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ActionResult</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> myAction</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Id</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Name</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">List</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><</span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Address</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> address</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">JsonResult</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> result </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">null</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
result </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">JsonResult</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background-color: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Data</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
error </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">false</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
message </span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"User Saved !"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">};</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;">return</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> result</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span></code></pre>
</span></code></pre>
</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-37625149112804711782013-11-22T14:44:00.001-08:002013-11-22T14:44:29.780-08:00How to fix Configuration system failed to initialize<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Configuration system failed to initialize- Issue in .NET</h2>
<h3 style="text-align: left;">
Problem : </h3>
<div>
I was working on a console application which will work as a Task Scheduler on the server, so I created a console application in Visual Studio 2012, so the project was created with Target Framework as v4.5, but the problem with that was, neither our QA server or Production server for that app had Framework v4.5 installed on them. So what I did was, I went to the project properties and changed the Target Framework to 4.0 instead of 4.5 and tried to run on Debug, thats it, I keep getting the error whenever the application was trying to read configuration values using ConfigurationManager.AppSettings and the error reads as below</div>
<div>
<br /></div>
<div>
"Configuration system failed to initialize".</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Solution : </h3>
<div>
Like most of us, I copied the exception details and googled it and this is what I got as a probable resolution from Stack overflow.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div>
<a href="http://stackoverflow.com/questions/6436157/configuration-system-failed-to-initialize">http://stackoverflow.com/questions/6436157/configuration-system-failed-to-initialize</a></div>
<div>
<br /></div>
<div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
make sure that your config file (web.config if web, or app.config if windows) in your project starts as:</div>
<pre class="default prettyprint prettyprinted" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"><?</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">xml version</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">"1.0"</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">?></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;"><configuration></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;"><configSections></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;"><sectionGroup</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background-color: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">name</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background-color: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">"applicationSettings"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background-color: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background-color: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">"System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;"><section</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background-color: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">name</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background-color: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">"yourProjectName.Properties.Settings"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background-color: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background-color: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">"System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background-color: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">requirePermission</span><span class="pun" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background-color: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">"false"</span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;">/></span><span class="pln" style="background-color: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background-color: transparent; border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;"></sectionGroup></span></code></pre>
</div>
</div>
<div>
<br /></div>
<div>
The above answer and more such similar answers on the internet points to the configSettings section & has been marked as an answer by most people so I guess this has resolved their issues but not in my case. It still gave me the errors. So this is what I did to get it resolved.</div>
<div>
<br /></div>
<div>
If you have an already running Console Application like me lets say in v4.5, then </div>
<div>
1. If that application is open in visual studio make sure its running in the current version, just check-in your changes to source control and close the studio.</div>
<div>
2. Open the *.csproject file on notepad and change the targetFramework from 4.5 to 4.0 like this </div>
<div>
"<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>" and save the file.</div>
<div>
3. Open the project now on visual studio and go to App.config file and make the change in the file under element startup and make .NETFramework, Version=v4.0 like below</div>
<div>
<br /></div>
<div>
<div>
<startup> </div>
<div>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /></div>
<div>
</startup></div>
</div>
<div>
<br /></div>
<div>
Now if you run your application, it should run just fine, atleast in my case.</div>
<div>
<br /></div>
<div>
<br /></div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-33874398038969703052013-11-20T09:31:00.000-08:002013-11-20T09:39:39.106-08:00Unable to Resolve Service Name, Oracle connection from ASP.NET or SSIS Business Intelligence Studio<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="font-size: small;">Unable to Resolve Service Name, Oracle connection from ASP.NET or SSIS Business Intelligence Studio</span></h2>
<h3 style="text-align: left;">
<span style="font-size: small;">Problem</span></h3>
<div>
<span style="font-size: small;">In one of my recent projects, I had this peculiar problem where I was not able to connect to Oracle Database from my ASP.NET application and every time it gave me an error like "Unable to resolve service name", although my tnsnames.ora file was correct and moreover, I was able to connect to the same oracle datasource from the Oracle SQL Developer studio, it was just driving me crazy.</span></div>
<div>
<br /></div>
<h3 style="text-align: left;">
Solution</h3>
<div>
One quick solution I figured out for both my connecting to oracle from the BIDS and Microsoft Visual Studio web configuration like below</div>
<div>
<br /></div>
<div>
Lets say your tnsnames.ora reads like below</div>
<div>
<br /></div>
<div>
<div>
<span style="font-size: x-small;">oradb.mydomain.com =</span></div>
<div>
<span style="font-size: x-small;">(DESCRIPTION =</span></div>
<div>
<span style="font-size: x-small;"> (ADDRESS = (PROTOCOL = TCP)(HOST = host.mydomain.com)(PORT = 1521))</span></div>
<div>
<span style="font-size: x-small;"> (LOAD_BALANCE = YES)</span></div>
<div>
<span style="font-size: x-small;"> (CONNECT_DATA =</span></div>
<div>
<span style="font-size: x-small;"> (SERVER = DEDICATED)</span></div>
<div>
<span style="font-size: x-small;"> (SERVICE_NAME = oradb.mydomain.com)</span></div>
<div>
<span style="font-size: x-small;"> (FAILOVER_MODE =</span></div>
<div>
<span style="font-size: x-small;"> (TYPE = SELECT)(METHOD = BASIC)(RETRIES = 180)(DELAY = 5)</span></div>
<div>
<span style="font-size: x-small;"> )</span></div>
<div>
<span style="font-size: x-small;"> )</span></div>
<div>
<span style="font-size: x-small;">)</span></div>
</div>
<div>
<br /></div>
<div>
This is what has to be done when putting the connection string inside Visual Studio to get it connected to the oracle database.</div>
<div>
<br /></div>
<div>
<span style="font-size: x-small;"><connectionStrings></span></div>
<div>
<span style="font-size: x-small;"><add name="myConn" connectionString="Server=host.mydomain.com/oradb.mydomain.com; User Id=guest; Password=guest" providerName="System.Data.OracleClient" /></span></div>
<div>
<br /></div>
<div>
So above all that I did was to mention the host name along with the service name. Mind you, there is one important thing here, the oracle data source is running on the default port 1521.</div>
<div>
<br /></div>
<div>
The problem associated with connecting to Oracle datasource using an OLEDBDatsource control on BIDS is slightly more than just the connection to the oracle datasource if you are working on a 64 bit machine/PC. The BIDS is a 32 bit application and so you have to make sure that you have a 32 bit oracle client installed on your PC along with the 64 client of Oracle otherwise the BIDS may not be able to detect the "Oracle Provider for OLE DB" driver from the PC that you are working on. </div>
<div>
<br /></div>
<div>
Once this is resolved, like the visual studio, here also we will mention the DataSource in the connection string as host/service_name in the connection used as shown in the screen shot shown below.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJjYtoJuo2JGGe-ualrZGd3imp1njFTIXrndsWRGWUxG8I-WS6Ooc6GfevHPQkXPwXvS1wmyBGsvgJ2FGTE36mc1-9fpgCFbPgUg6694KskP8r2L-ew1TeADWz-Z2Hi86rquQojZ9tjlY/s1600/BIDS_OracleConn.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJjYtoJuo2JGGe-ualrZGd3imp1njFTIXrndsWRGWUxG8I-WS6Ooc6GfevHPQkXPwXvS1wmyBGsvgJ2FGTE36mc1-9fpgCFbPgUg6694KskP8r2L-ew1TeADWz-Z2Hi86rquQojZ9tjlY/s320/BIDS_OracleConn.png" width="312" /></a></div>
<div>
<br /></div>
<div>
In the connection manager above, the Server or file name is mentioned as "host.mydomain.com/oradb.mydomain.com". But here also please note that in my case the oracle datasource was running on default port 1521. </div>
<div>
<br /></div>
<div>
Thants it ! Happy coding !</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-79903023214075197752013-11-05T14:00:00.001-08:002014-10-19T19:18:23.325-07:00Reading XML nodes with namespaces in SQL Server<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Reading XML nodes with namespaces in SQL Server</h2>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Like me, many of you might have gone through situations where you might had to parse and read XML in SQL Server. SQL Server 2005 onward have a very good functional support to read XML. Lets go over some examples on how to do so </div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
1. The first XML is an xml with persons details and contact details and here is how we can read that below</div>
<div style="text-align: left;">
<br /></div>
<span style="font-size: x-small;">DECLARE @MyXML XML</span><br />
<span style="font-size: x-small;">SET @MyXML = '<SampleXML></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><Person></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><firstName>Kaushik</firstName></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><lastName>Ghosh</lastName></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><skill>ASP.NET</skill></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><language montherTongue="Bengali">Hindi</language></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><age>31</age></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></Person></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><Contacts></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><primaryEmail>primary@mydomain.com</primaryEmail></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><secondaryEmail>secondary@mydomain.com</secondaryEmail></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><primaryPhone>1234567890</primaryPhone></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><secondaryPhone>Melon</secondaryPhone></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></Contacts></span><br />
<span style="font-size: x-small;"></SampleXML>'</span><br />
<span style="font-size: x-small;"><br /></span>
<span style="font-size: x-small;">SELECT</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Person[1]/firstName[1]','varchar(10)') AS firstName,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Person[1]/lastName[1]','varchar(10)') AS lastName,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Person[1]/skill[1]','varchar(10)') AS skill,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Person[1]/language[1]/@montherTongue','varchar(10)')+ ',' + a.b.value('Person[1]/language[1]','varchar(10)') AS language,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Person[1]/age[1]','varchar(10)') AS age,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Contacts[1]/primaryEmail[1]','varchar(30)') AS primaryEmail,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Contacts[1]/secondaryEmail[1]','varchar(30)') AS secondaryEmail,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Contacts[1]/primaryPhone[1]', 'varchar(10)') AS primaryPhone,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>a.b.value('Contacts[1]/secondaryPhone[1]', 'varchar(10)') AS secondaryPhone</span><br />
<span style="font-size: x-small;">FROM @MyXML.nodes('SampleXML') a(b)</span><br />
<span style="font-size: x-small;"><br /></span>
In the above example it works fine, when the nodes are not repeating, but what if the XML has repeating nodes, please see the next example. This time we will take an example which will have namespaces, nested nodes with attributes and also multiple repeating nodes.<br />
<br />
<span style="font-size: x-small;">DECLARE @xml_text xml;</span><br />
<span style="font-size: x-small;">SET @xml_text = '</span><br />
<span style="font-size: x-small;"><towers xmlns="http://www.mydomain.org/schemas/xmls" version="1.4.1.0"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><tower xmlns="http://www.mydomain.org/schemas/xmls" uid="2183256"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><name>J.D. TAYLOR 24 #1</name></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><numAPI>0100320139</numAPI></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><towerLocation uid="Location1"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><latitude uom="dega">20.329705810</latitude></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><longitude uom="dega">-27.720651970</longitude></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></towerLocation></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><towerDatum uid="GL"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><code>GL</code></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><name>Ground Level</name></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></towerDatum></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><operator>Mobile comp1</operator></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><country>Unknown</country></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><county>DuPage</county></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><state>Illinois</state></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><timeZone>-00:00</timeZone></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><commonData></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><dTimCreation>2013-10-04T18:51:09.377</dTimCreation></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><dTimLastChange>2013-10-04T18:51:09.377</dTimLastChange></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></commonData></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></tower></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><tower xmlns="http://www.mydomain.org/schemas/xmls" uid="2183256"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><name>K.D. TAYLOR 24 #1</name></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><numAPI>0100320140</numAPI></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><towerLocation uid="Location2"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><latitude uom="dega">10.329705811</latitude></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><longitude uom="dega">-17.720651970</longitude></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></towerLocation></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><towerDatum uid="SL"></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><code>SL</code></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><name>Sea Level</name></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></towerDatum></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><operator>Mobile Company 2</operator></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><country>Unknown</country></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><county>Cook</county></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><state>ILLINOIS</state></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><timeZone>-00:00</timeZone></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><commonData></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><dTimCreation>2013-11-04T18:51:09.377</dTimCreation></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span><dTimLastChange>2013-11-04T18:51:09.377</dTimLastChange></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></commonData></span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span></tower></span><br />
<span style="font-size: x-small;"></towers>';</span><br />
<span style="font-size: x-small;"><br /></span>
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>with xmlnamespaces('http://www.mydomain.org/schemas/xmls' as towers,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>default 'http://www.mydomain.org/schemas/xmls')</span><br />
<span style="font-size: x-small;"> SELECT T.Item.value('(name/text())[1]', 'varchar(200)') as name,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('(numAPI/text())[1]', 'varchar(30)') as numAPI,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('towerLocation[1]/@uid[1]', 'varchar(20)') as towerLocation,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('towerLocation[1]/latitude[1]', 'varchar(20)') + ' ' </span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>+ T.Item.value('towerLocation[1]/latitude[1]/@uom[1]', 'varchar(10)') as latitude,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('towerLocation[1]/longitude[1]', 'varchar(20)') + ' ' </span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>+ T.Item.value('towerLocation[1]/longitude[1]/@uom[1]', 'varchar(10)') as longitude,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('towerDatum[1]/@uid[1]', 'varchar(10)') as towerDatumCode,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('towerDatum[1]/code[1]', 'varchar(10)') + ' or ' </span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>+ T.Item.value('towerDatum[1]/name[1]', 'varchar(20)') as towerDatum,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('(operator/text())[1]', 'varchar(200)') as operator,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('(country/text())[1]', 'varchar(30)') as country,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('(county/text())[1]', 'varchar(30)') as county,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('(state/text())[1]', 'varchar(30)') as state,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('(timeZone/text())[1]', 'varchar(30)') as timeZone,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('commonData[1]/dTimCreation[1]', 'varchar(30)') as created,</span><br />
<span style="font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span>T.Item.value('commonData[1]/dTimLastChange[1]', 'varchar(30)') as changed</span><br />
<span style="font-size: x-small;"> FROM @xml_text.nodes ( 'towers/tower' ) AS T(item)</span><br />
<span style="font-size: x-small;"><br /></span>
The above XML is a more realistic example of XML data that you might need to parse in the database while doing some sort of integration.</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-78124241197262712342013-10-03T09:27:00.002-07:002014-10-09T21:16:16.597-07:00Saving & retrieving/updating data using cookies<div dir="ltr" style="text-align: left;" trbidi="on">
<h2>
Saving & retrieving/updating data using cookies.</h2>
<div>
Like me, I believe most of you might had to use cookies in one way or the other, in my case, I had the following scenario. In a website, the user logs in who has access to numerous retail brands and every time he/she works with only one particular brand and this particular brand in which he/she works most has to be set as a preferred brand. So when the user is done with his/her work and closes the session, the last chosen brand has to be persisted and that has to be his/her preferred brand.</div>
<div>
<br /></div>
<div>
So going by the requirement, the most simple and quick resolution will seem to save the preferred brand in cookies of the browser. So the following two functions does exactly that. The get function gets the saved preferred brand when the user first opens the session to the website and the set functions sets a preferred brand when the user switches from one brand to the other. From where you want to call these functions is left abstract as your site may have master pages, or may not have master pages or your pages might be inherited from a Base Page class hence there can be numerous such scenarios.</div>
<div>
<br /></div>
<div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// <summary></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// gets the preferred brand for the user from saved cookie.</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// </summary></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// <param name="userId"></param></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// <returns></returns></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> public int getPreferredBrand(string userId)</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> int preferredBrandId = 0;</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> try</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> //1. check to see if cookie exists with preferred brandid.</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> //2. if yes then return the preferred brandid.</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> HttpCookie authCookie = HttpContext.Current.Request.Cookies[PrefBrandCookieName];</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> if (authCookie != null)</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> System.Web.Security.FormsAuthenticationTicket ticket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> preferredBrandId = int.TryParse(ticket.UserData.ToString(), out preferredBrandId) == true ? preferredBrandId : 0;</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> else</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> preferredBrandId = 0;</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> catch (Exception ex)</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> Exception customEx = new Exception("Error while trying to retrieve cookiename<<" + userId + ">>.See detailed exception.." + ex.Message);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> ExceptionManager.Publish(customEx);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> return preferredBrandId;</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// <summary></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// sets preferred brand for the user when session expires or browser is closed.</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// </summary></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// <param name="userId"></param></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> /// <param name="brandId"></param></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> public void setPreferredBrand(string userId, int brandId)</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> try</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> //1. Check if cookie exists</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> System.Web.Security.FormsAuthenticationTicket authTicket = null;</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> HttpCookie authCookie = HttpContext.Current.Request.Cookies[PrefBrandCookieName];</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> authTicket = new System.Web.Security.FormsAuthenticationTicket(1, userId, DateTime.Now, DateTime.MaxValue, true, brandId.ToString());</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"><br /></span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> //2. Persist selected brand in cookies.</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> //will persist cookie even when the browser is shutdown.</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> if (authCookie != null)</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> authCookie.Value = System.Web.Security.FormsAuthentication.Encrypt(authTicket);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> else</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> authCookie = new HttpCookie(LCoreUser.PrefBrandCookieName, System.Web.Security.FormsAuthentication.Encrypt(authTicket));</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> authCookie.Expires = DateTime.MaxValue;</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> HttpContext.Current.Response.Cookies.Add(authCookie);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> catch (Exception ex)</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> {</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> Exception customEx = new Exception("Error while persisting cookie for user<<" + userId + ">> and brand<<" + brandId.ToString() + ">>.See detailed exception.." + ex.Message);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> ExceptionManager.Publish(customEx);</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
<div style="text-align: left;">
<span style="font-size: x-small;"> }</span></div>
</div>
<div>
<br /></div>
<div>
The above code is pretty self explanatory. In the set function, I first check if the cookie does exist or not and if it does, then update the cookie value by assigning a new ticket into it. And if the cookie does not exist then all I do is create a new cookie and assign the ticket value.</div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-22855382232355557082013-09-18T09:26:00.002-07:002013-09-18T09:26:36.740-07:00Oracle connect problems from ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on">
Many people like me, might have faced the issue, where even after installing the Oracle client and doing all configurations right inside tnsnames.ora, may not be able to connect to Oracle database.<br />
<br />
The Error message might read as "ORA-12154 :TNS:Could not resolve the connect identifier specified".<br />
<br />
Here is a quick tip on how to connect. Instead of only mentioning the Service Name or the SID name in the Web.config, mention the entire connection description like below<br />
<br />
<add key="OracleConnection" value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=1521)))(CONNECT_DATA=(SID=oradev))); User Id=user_id; Password=password"/><br />
<br />
* The above method will definitely work if you are able to connect to your Oracle database using Oracle Net Configuration.</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-38196689233659557222013-08-23T08:22:00.001-07:002013-08-23T08:22:40.268-07:00Granting permission to a particular on Db schema (SQL Server)<div dir="ltr" style="text-align: left;" trbidi="on">
There might be scenarios where you might move the code to QA or production and then grant access to user setup for the Application's access. Here is how you can do it as shown below<br />
<br />
USE <b>DBName</b><br />
GO<br />
<br />
GRANT EXECUTE ON SCHEMA :: <b>schemaName </b>TO <b>App_User</b>;<br />
<br />
The highlighted in bold options are the inputs that will depend on the database name of your application, the schema whose objects you need to access and the user setup for application's access.</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0tag:blogger.com,1999:blog-8063958573455141345.post-73172963611317388072013-08-21T12:38:00.001-07:002013-08-21T12:38:18.275-07:00Querying Active Directory using LDAP by LDAPBrowser and C#<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="color: #0c343d; font-size: small;">Insight into Problem</span></h2>
<div>
There may be projects/project work where you might have to lookup the user on Active Directory using some LDAP tool or querying the domain controller to see how the user has been setup without making any modifications as such. Adding to that, there might be scenarios where you might have to get user details from Active Directory for the purpose of impersonating the user in the application for the sake of troubleshooting.</div>
<div>
<br /></div>
<h2 style="text-align: left;">
<span style="color: #0c343d; font-size: small;">Looking up users on LDAP using Tool</span></h2>
<div>
For looking up a user on Active Directory or Novell using a tool can be done easily using a tool like LdapBrowser and the way the tool binds user information can lay the foundation of a C# application which can do the same, if we need the user to be impersonated for troubleshooting purposes.</div>
<div>
<br /></div>
<div>
After installing the LDAP Browser and setting up the profile, please refer to <a href="http://www.ldapbrowser.com/info_softerra-ldap-browser.htm">link </a>on how to do so and get the browser software, here we are not going into the details of that but a typical user search on directory will have the parameters and result screen as follows.</div>
<div>
<br /></div>
<div>
Search DN: DC=mycomp,DC=com; Filter: (cn=*kaushik*)</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2tjrN03Zlrl5kaVhgDmTjTdMpv3qCQ33zUjN76rF2KlXGPkYWAYLrhKKtUeWvNnVV3m7P8Z-iKPUr5Jm_1fgZ9c3ZRFeC-AwSbF1NadweS7sT__MDXSIlyhayLw7a_UTWiQnihCqGe98/s1600/ldap_search_by_name_word.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2tjrN03Zlrl5kaVhgDmTjTdMpv3qCQ33zUjN76rF2KlXGPkYWAYLrhKKtUeWvNnVV3m7P8Z-iKPUr5Jm_1fgZ9c3ZRFeC-AwSbF1NadweS7sT__MDXSIlyhayLw7a_UTWiQnihCqGe98/s640/ldap_search_by_name_word.png" width="640" /></a></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: left;">
Whats important in the above screen shot is the Filter Text and the DN value, we are going to use similar parameters while doing it through C# application. Please look at another search below, where we use the user's id and also specify which properties to load in the search result. If you are using the Active Directory, then the userid is usually within the attribute "sAMAccountName" and on Novell its called uid. I think it depends on how the Domain Administrators set it up.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
So the parameters I have put in are below</div>
<div class="separator" style="clear: both; text-align: left;">
1. Search DN: DC=mycomp,DC=com</div>
<div class="separator" style="clear: both; text-align: left;">
2. Filter: (sAMAccountName=myuid), here "myuid" is the id that you need to search on domain controller.</div>
<div class="separator" style="clear: both; text-align: left;">
3. Attributes : cn,mail, sAMAccountName. (These are the attributes that I ask to be loaded in the Search Result).</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div class="separator" style="clear: both; text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlWyGsIPWKy3usZBk3ukfwArbapScOup1pvULcjXb3Ej2D8IU0Jt9Z8ibNhT9t7xRmNWHFL7C27KKu66QZFKBjp-SBz-Jtm1-83xWHCBXbDazqJ2uOft6AdEWfXeM9FhyphenhyphenIko-DmueyWH4/s1600/ldap_search_by_uid.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="354" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlWyGsIPWKy3usZBk3ukfwArbapScOup1pvULcjXb3Ej2D8IU0Jt9Z8ibNhT9t7xRmNWHFL7C27KKu66QZFKBjp-SBz-Jtm1-83xWHCBXbDazqJ2uOft6AdEWfXeM9FhyphenhyphenIko-DmueyWH4/s640/ldap_search_by_uid.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
In the above result screen, not only have we specified a more specific filter but also mentioned what attributes need to be leaded in the search result. Having fairly an idea of how things work, we are going to quickly move on to the next section and see how to do the same operations using C# application without using any third party tools.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h2 style="text-align: left;">
<span style="color: #0c343d; font-size: small;">Querying Active Directory using LDAP/C# application</span></h2>
<div>
<span style="color: #0c343d; font-size: small;">The following method "searchUser", searches the LDAP based on 2 different inputs, one is the search text and the other is the searchby. The searchby specifies weather the search will done by user's name (cn), or user's id (sAMAccountName) or user's email (mail). Based on the searchBy parameter passed to the function, the filterText gets created by calling the getFilterText function, please look at the implementation in the next section along with the declaration of the LDAPUser class.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">///</span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Searches input text on LDAP to get users</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">///</span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"></summary></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">///</span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><param
name="searchText"></param></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">///</span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><param
name="searchBy"></param></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">///</span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><returns></returns></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">LDAPUser</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> searchUser(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> searchText, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> searchBy)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">LDAPUser</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> result = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">List</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">LDAPUser</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">try</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DirectoryEntry</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> entry = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DirectoryEntry</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(_ldapUrl);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DirectorySearcher</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> mySearcher = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DirectorySearcher</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(entry);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">[] propertiesToLoad = { </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"cn,
mail, memberOf, sAMAccountName"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">
mySearcher.PropertiesToLoad.AddRange(propertiesToLoad);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> mySearcher.Filter =
(getFilterText(searchText, searchBy));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> mySearcher.SearchScope = </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">SearchScope</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">.Subtree;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> mySearcher.ServerTimeLimit = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">TimeSpan</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(0, 0, 60);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> mySearcher.ClientTimeout = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">TimeSpan</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(0, 10, 0);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">foreach</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (System.DirectoryServices.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">SearchResult</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> resEnt </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">in</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> mySearcher.FindAll())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> System.DirectoryServices.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DirectoryEntry</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> de =
resEnt.GetDirectoryEntry();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">LDAPUser</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> entity;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">if</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (de.Properties[</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"sAMAccountName"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">].Value
!= </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">null</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">
&& de.Properties[</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"mail"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">].Value != </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">null</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> entity = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">LDAPUser</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> UserId =
de.Properties[</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"sAMAccountName"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">].Value.ToString(),<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Email =
de.Properties[</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"mail"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">].Value.ToString(),<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> Name =
de.Properties[</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"cn"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">].Value.ToString(),<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> LdapBinderUserId =
BinderUserId,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> LdapBinderPassword
= BinderPassword<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result.Add(entity);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">catch</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Exception</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">throw</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> ex;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result;<o:p></o:p></span></div>
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span></div>
<div>
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<div>
<span style="background-color: white; line-height: 115%;"><span style="font-family: inherit;">Below is the implementation of the filter text that we need to pass to the DirectorySearcher and also the LDAPUser class declaration along with the LDAP Url that you will need to save/pass.</span></span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">private</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> getFilterText(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> searchText, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> searchBy)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">.Empty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">try</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">switch</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (searchBy.ToLower())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">case</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"userid"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"(sAMAccountName="</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> +
searchText + </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">")"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">break</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">case</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"email"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"(mail="</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> +
searchText + </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">")"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">break</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">default</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result = </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"(cn=*"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> +
searchText + </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"*)"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">break</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">catch</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Exception</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">throw</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> ex;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> result;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span><span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: left;">
The LDAP User class looks as below</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: left;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt;">LDAPUser</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">long</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> Id { </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">get</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">set</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> Name { </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">get</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">set</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> UserId { </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">get</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">set</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> Email { </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">get</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; </span><span style="background-color: white; color: blue; font-family: Consolas; font-size: 9.5pt;">set</span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;">; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"> </span></div>
<div class="MsoNormal">
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt; line-height: 14px;"> }</span></div>
<div>
<span style="background-color: white; font-family: Consolas; font-size: 9.5pt; line-height: 14px;"><br /></span></div>
<div class="MsoNormal">
<span style="line-height: 115%;"><span style="font-family: inherit;">The LDAP Url can be declared in configuration file or can be saved somewhere else. This url is required to be passed to the constructor DirectoryEntry before the search.</span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;"><</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">add</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;"> </span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">key</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">=</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">"</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">LDAPUrl</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">"</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;"> </span><span style="background: white; color: red; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">value</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">=</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">"</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">LDAP://mycomp.com:389/DC=mycomp,DC=com</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">"</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">/></span><o:p></o:p></div>
</div>
<div>
<span style="background-color: white; line-height: 115%;"><span style="font-family: inherit;"><br /></span></span></div>
<div>
<span style="background-color: white; line-height: 115%;"><span style="font-family: inherit;">That is pretty much how you can search and get users from Active Directory using LDAP/ C#. I hope this helps.</span></span></div>
</div>
KAUSHIK GHOSHhttp://www.blogger.com/profile/07839090927576041604noreply@blogger.com0