Thursday, August 19, 2010

Do Post Back Handling

[asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="300000"]//For Five Minute
[/asp:Timer]

[script language="javascript" type="text/javascript"]

function DoPostBack() {
__doPostBack('Timer1', 'My Argument');
}

[/script]

Add in Webconfige file

[httpHandlers]
[add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/]
[/httpHandlers]

Friday, August 13, 2010

Pass query string in link button

[asp:LinkButton ID="lnkNav" runat="server" Text='[%#Eval("CategoryName")%]' PostBackUrl='[%#"~/Subcategory.aspx?CId="+Eval("CategoryId") %]'][/asp:LinkButton]

Friday, August 6, 2010

Upload multiple files

In .aspx file

[script type="text/javascript"]
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value - 1) + 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my' + num + 'Div';
newdiv.setAttribute('id', divIdName);
newdiv.innerHTML = '[input type="file" name="attachment" id="attachment"/][input type="Button" value="Remove" onclick="removeElement(' + divIdName
+ ')"/]';
ni.appendChild(newdiv);
}


function removeElement(divNum) {
var d = document.getElementById('myDiv');
d.removeChild(divNum);
}

[/script]
[table]
[tr]
[td style="width: 295px"]
Upload Image Logo:
[/td]
[td]
[input type="file" name="attachment" runat="server" id="attachment" onchange="document.getElementById('moreUploadsLink').style.display =
'block';" /]
[div id="moreUploadsLink" style="display: none;"]
[a href="javascript:addElement();" style="font-size:12px;color:White;"]Upload More Images For Sliding[/a]
[/div]
[input type="hidden" value="0" id="theValue" /]
[div id="myDiv"]
[/div]
[/td]
[/tr]
[/table]
In .cs file


HttpFileCollection uploadFiles = HttpContext.Current.Request.Files;
for (int i = 1; i < uploadFiles.Count; i++)
{

HttpPostedFile uploadFile = uploadFiles[i];
if (uploadFile.FileName != null && uploadFile.FileName != "")
{
try
{
uploadFile.SaveAs(Server.MapPath("~/flash/banner/content/images/") + "\\" + Path.GetFileName(uploadFile.FileName));
strImageUrl = "~/flash/banner/content/images/" + Path.GetFileName(uploadFile.FileName);
strImageName = Path.GetFileName(uploadFile.FileName);

tblAlbum objAlbum = new tblAlbum();
objAlbum.PicName = strImageName;
objAlbum.PicUrl = strImageUrl;
objAlbum.BrandId = intBrandId;
db.tblAlbums.InsertOnSubmit(objAlbum);
db.SubmitChanges();
}
catch (Exception ex)
{
}

}
}

Calling Css from code behind

private void CssSelect()
{
if (Request.QueryString["Id"] == "2")
{
lnk2.Attributes.Add("class", "selected");
}
else if (Request.QueryString["Id"] == "3")
{
lnk3.Attributes.Add("class", "selected");
}
else if (Request.QueryString["Id"] == "4")
{
lnk4.Attributes.Add("class", "selected");
}
else if (Request.QueryString["Id"] == "5")
{
lnk5.Attributes.Add("class", "selected");
}
else
{
lnk1.Attributes.Add("class", "selected");
}
}

Write on XML file

private void WriteXml()
{
int intSubCatId = 0;
if (Request.QueryString["SId"] != null)
{
intSubCatId = Convert.ToInt32(Request.QueryString["SId"]);

var result = from subcat in db.tblSubCategories
join alb in db.tblAlbums on subcat.SubCategoryId equals alb.SubCatId
where subcat.SubCategoryId == intSubCatId
select new { FlashImage = alb.PicUrl };

if (result.Count() > 0)
{
string strxmlDocpath = Server.MapPath("~/flash/banner/xml/banner.xml");
XmlTextWriter writer = null;
try
{
writer = new XmlTextWriter(strxmlDocpath, Encoding.UTF8);
writer.Formatting = Formatting.Indented; writer.WriteStartDocument(true);

writer.WriteComment("This file was generated from an ASPX file");

writer.WriteStartElement("banner");

writer.WriteAttributeString("width", "597");
writer.WriteAttributeString("height", "354");
writer.WriteAttributeString("startWith", "1");

foreach (var query in result)
{
string strImage = query.FlashImage;
string strSlideImg = strImage.Replace("~/flash/banner/", "");
writer.WriteStartElement("item");
writer.WriteElementString("path", null, strSlideImg);
writer.WriteElementString("title", null, "");
writer.WriteElementString("target", null, "_blank");
writer.WriteElementString("link", null, "");
writer.WriteElementString("bar_color", null, "0xffffff");
writer.WriteElementString("bar_transparency", null, "40");
writer.WriteElementString("caption_color", null, "0xffffff");
writer.WriteElementString("caption_transparency", null, "60");
writer.WriteElementString("stroke_color", null, "0xffffff");
writer.WriteElementString("stroke_transparency", null, "60");
writer.WriteElementString("slideshowTime", null, "8");
writer.WriteEndElement();

}


writer.WriteEndElement();
writer.Flush();
writer.Close();
XmlDocument doc = new XmlDocument();
doc.Load(strxmlDocpath);

doc.Save(strxmlDocpath);
}
catch (Exception ex)
{

}
finally
{

}
Flash.Visible = true;
}
else
{
Flash.Visible = false;
}
}


}

Wednesday, July 14, 2010

Store Scrapped Image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
scraping();
}
}

protected void scraping()
{
//WebClient webClient = new WebClient();
//const string strUrl = "http://www.gotime.com/seattle/find/places/All-Romantic-Places-in-Seattle";
//byte[] reqHTML;
//reqHTML = webClient.DownloadData(strUrl);
//UTF8Encoding objUTF8 = new UTF8Encoding();
//string output = objUTF8.GetString(reqHTML);
//int start = output.IndexOf("
    //int end = output.IndexOf("
", start) + 8 - start;

//output = output.Substring(start, end);
////lblWebpage.Text = output;

string url = "http://places3.assets.gotime.com/7d5ea606b357cf90ae438faf9b21e55aadf2567b_q.jpg";

string filename = url.Substring(url.LastIndexOf('/') + 1);
byte[] bytes = GetBytesFromUrl(url);
WriteBytesToFile(Server.MapPath("~/Image/" + filename), bytes);
}

static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();

Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}

static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
}

Wednesday, July 7, 2010

Delete Duplicate Records

SET ROWCOUNT 1
DELETE tblcity
FROM tblcity a
WHERE (SELECT COUNT(*) FROM tblcity b WHERE b.city = a.city and b.countryid='c081') > 1
WHILE @@rowcount > 0
DELETE tblcity
FROM tblcity a
WHERE (SELECT COUNT(*) FROM tblcity b WHERE b.city = a.city and b.countryid='c081' ) > 1
SET ROWCOUNT 0

Thursday, June 17, 2010

Convert In Hindi

public string ConvertEtoH(string input)
{
//string input = "0932;093E;0926;0947;0928;";
Regex rx = new Regex(@"([0-9A-Fa-f]{4});");
string output = rx.Replace(input, match => ((char)Int32.Parse(match.Groups[1].Value, NumberStyles.HexNumber)).ToString());
return output;
}

Wednesday, June 16, 2010

Create Polygon On Google Map

[script type="text/javascript"]

function gob(e){
if(typeof(e)=='object')
return(e);
if(document.getElementById)
return(document.getElementById(e));
return(eval(e))
}

var map;
var toolID = 1;
var codeID = 2;
var shapeID = 1;
var polyShape;
var holeShape = new Array();

var ph = 0;
var polygonMode = false;
var markerMode = false;
var circlemode = false;
var rectanglemode = false;
var holemode = false;

var mylistener;
var editlistener = null;
var holelistener = new Array();
var editing = false;
var holeediting = false;
var notext = false;
var polygonDepth = "0";
var polyPoints = new Array();
var holePoints = new Array();
var holesarray = new Array();
var encpoints = new Array();
var encarray = new Array();
var holecoords = new Array();
var holebuilding;
var tinymarker;
var geocoder = null;

var mousemovepoint;
var editingstyles = 0;
var header = "";

var cur = 0;
var plmcur = 0;
var polygonstyles = new Array();
var polylinestyles = new Array();
var placemarks = new Array();
var polygonholes = new Array();
var centerMarker = null;
var radiusMarker = null;
var markerissaved = true;
var lookatsaved = false;

var tinyIcon = new GIcon();
tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
tinyIcon.iconSize = new GSize(12,20);
tinyIcon.shadowSize = new GSize(22,20);
tinyIcon.iconAnchor = new GPoint(6,20);
tinyIcon.infoWindowAnchor = new GPoint(5,1);

var markerOptions = {icon:tinyIcon};


function polystyle() {
this.name = "rangecolour";
this.kmlcolor = "660000FF";
this.kmlfill = "660000FF";
this.color = "#FF0000";
this.fill = "#FF0000";
this.width = 0.1;
this.lineopac = .4;
this.fillopac = .4;
this.fillonoff = 1;
this.lineonoff = 1;
}
function linestyle() {
this.name = "linecolour";
this.kmlcolor = "660000FF";
this.color = "#FF0000";
this.width = 3;
this.lineopac = .4;
}
function placemarkobject() {
this.name = "distribution/range";
this.desc = "";
this.polygonstyle = "rangecolour";
this.linestyle = "linecolour";
this.curstyle = 0;
this.tess = 1;
this.alt = "clampToGround";
this.plmtext = "";
this.jstext = "";
this.toolID = 1;
this.hole = 0;
this.ID = 0;
}


function createstyleobjects() {
var polygonstyle = new polystyle();
polygonstyles.push(polygonstyle);
var polylinestyle = new linestyle();
polylinestyles.push(polylinestyle);
}
function createplacemarkobject() {
var thisplacemark = new placemarkobject();
placemarks.push(thisplacemark);
}
function load() {
if(GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"), {draggableCursor:'default',draggingCursor:'pointer'});
map.setCenter(new GLatLng(45.0,7.0),3);
var customUI = map.getDefaultUI();
customUI.controls.maptypecontrol = false;
customUI.controls.menumaptypecontrol = true;
map.setUI(customUI);
mylistener = GEvent.addListener(map,'click',mapClick);
geocoder = new GClientGeocoder();
GEvent.addListener(map,"mousemove",function(point){
mousemovepoint = point;
var LnglatStr6 = point.lng().toFixed(6) + ', ' + point.lat().toFixed(6);
var latLngStr6 = point.lat().toFixed(6) + ', ' + point.lng().toFixed(6);
});
GEvent.addListener(map,"zoomend",mapzoom);
createstyleobjects();
createplacemarkobject();

}
}

function mapClick(section, clickedPoint){
if(section == null) pushpoint(clickedPoint);
}

function pushpoint(point){
polyPoints.push(point);
drawCoordinates();
}

function closePoly(){
if(polyPoints.length > 2) polyPoints.push(polyPoints[0]);
drawCoordinates();
}

function drawCoordinates(){

if(polyPoints.length > 0){

if(tinymarker) map.removeOverlay(tinymarker);
if(polygonstyles[cur].lineonoff == 0) polygonstyles[cur].color = polygonstyles[cur].fill;

polyShape = new GPolyline(polyPoints,polylinestyles[cur].color,polylinestyles[cur].width,polylinestyles[cur].lineopac);
tinymarker = new GMarker(polyPoints[0], markerOptions);
map.addOverlay(tinymarker);


map.addOverlay(polyShape);
logCoordinates();
}
}

function logCoordinates(){
var j = polyPoints.length;
var plmtext = "";
var coords1 = coords2 = "";
if (notext == false && (j > 0)){
var linefooter;
var lineheader;

placemarks[plmcur].toolID = 1;
plmtext = lineheader;
for (var i=0; i var lat = polyPoints[i].lat();
var longi = polyPoints[i].lng();
coords1 += roundVal(longi) + " " + roundVal(lat) + ",$";
plmtext += roundVal(longi) + " " + roundVal(lat) + ",$";
}
placemarks[plmcur].ID = polyPoints[0].lng().toFixed(3);
plmtext += linefooter;
if(codeID == 2) coo.value = coords1 + coords2;
plmtext = "";
coords1 = coords2 = "";

}
}
function mapzoom(){
var mapZoom = map.getZoom();

}
function mapcenter(){
var mapCenter = map.getCenter();
var latLngStr6 = mapCenter.lat().toFixed(6) + ', ' + mapCenter.lng().toFixed(6);
gob("centerofmap").value = latLngStr6;
}



function showCode(){

var j = polyPoints.length;
var k = holePoints.length;
var pweight = polygonstyles[cur].width;
var lweight = polylinestyles[cur].width;
coo.value = "var points =\n[\n";
if (markerMode) {
if(polyPoints.length > 1) createmarker(polyPoints[0]);
var lat = polyPoints[0].lat();
var longi = polyPoints[0].lng();
coo.value += "new GLatLng(" + roundVal(lat) + "," + roundVal(longi) + ")";

}
coo.value +="\n];\n";
coo.value +="var polyline = new GPolyline(points,'" + polylinestyles[cur].color + "'," + lweight + "," + polylinestyles[cur].lineopac + ");\n";
coo.value +="map.addOverlay(polyline);";

}

function roundVal(val){
if(val.toString().length < 9){
return val;
}else{
var dec = 6;
var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
}



[/script]
[body onload="load()" onunload="GUnload()"]
[form id="form1" runat="server"]
[table id="toptable"]
[tr]
[td]
[input type="button" onclick="closePoly();" value="Close Polyshape" id="CloseButton" /]
[/td]
[/tr]
[tr]
[td]
[div id="map" style="width: 819px; height: 650px"]
[/div]
[/td]
[/tr]
[tr]
[td]
[div id="status"]
[asp:TextBox ID="coords" runat="server" TextMode="MultiLine" Height="450px" Width="200px"][/asp:TextBox]
[/div]
[/td]
[/tr]
[/table]
[script type="text/javascript"]
var coo = gob('coords')
[/script]

[/form]
[/body]
[/html]

Some Query Regarding Spatial Data

DECLARE @g geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SELECT @g as geo, @g.STArea() as Area, 'Blue' as [Color], 10 as [Thickness];

DECLARE @g geography;
SET @g = geography:: STGeomFromText('POLYGON((74.53125000000001 28.613459424004418, 75.05859375 17.560246503294912, 83.40820312500001 18.562947442888283, 83.84765624999998 27.527758206861872, 79.8046875 25.482951175355335, 74.53125000000001 28.613459424004418))', 4326)
SELECT @g as indiatest, 'black' as [Color], 1 as [Thickness];;

DECLARE @a geography;
SET @a = geography:: STGeomFromText('
POLYGON((61.17187500000001 29.840643899834415, 66.357421875 29.76437737516313, 66.62109375 30.826780904779777, 67.67578124999998 31.20340495091738, 69.08203124999998 31.503629305773003, 69.43359375000001 32.62087018318112, 70.224609375 32.99023555965106, 71.19140625000001 34.16181816123038, 71.455078125 35.88905007936091, 71.71875000000003 37.16031654673676, 71.279296875 38.34165619279594, 69.609375 37.718590325588146, 67.14843750000001 37.23032838760387, 65.0390625 37.09023980307206, 63.544921875 35.81781315869662, 61.08398437500001 35.24561909420682, 60.820312500000014 34.016241889667015, 60.732421875000014 32.472695022061494, 64.951171875 31.877557643340015, 61.17187500000001 29.840643899834415))
', 4326)
SELECT @a,@a.STArea() as AreaOfAfganisthan;

DECLARE @c geography;
SET @c = geography:: STGeomFromText('POLYGON((25.04882812499999 31.693275163494224, 25.136718750000018 21.823428188945517, 36.9140625 21.90499674823705, 35.59570312500001 23.928691516534553, 33.75000000000002 27.56931904193159, 32.431640624999986 29.423012724559165, 34.365234375 27.880518540948774, 34.980468750000014 29.499538055197778, 34.27734375000001 31.168316945375416, 32.69531250000001 30.716023241367484, 31.289062500000014 31.093083441658976, 29.091796875000014 30.640433251773864, 25.224609375000014 31.468653088536332, 25.04882812499999 31.693275163494224))', 4326)
SELECT @c as egypt, 'red' as [Color], 1 as [Thickness];

DECLARE @b geography;
SET @b = geography:: STGeomFromText('
POLYGON((-1.9335937500000142 34.99640427042621, -1.4062499999999953 32.215280296416935, -8.525390625000002 28.654602089188238, -8.525390625000002 26.944271796662917, 2.8124999999999907 19.686729292076876, 4.658203125000007 18.940236111954757, 12.128906250000002 23.606946962243896, 9.580078125000007 26.158068192666434, 9.84375 28.03578433217748, 9.931640624999996 30.109652836744534, 9.228515625000016 31.917354820822254, 7.910156249999986 33.54383701210014, 8.613281249999998 36.706008903301424, 6.15234375 36.706008903301424, 3.3398437500000093 36.63551355486138, 0 36.494329231411086, -1.23046875 35.28389270803397, -1.9335937500000142 34.99640427042621))
', 4326)
SELECT @b as algeria, 'blue' as [Color], 1 as [Thickness];


DECLARE @b geography;
SET @b = geography:: STGeomFromText('
POLYGON((-8.613281249999998 27.254629577800077, -8.569335937500001 25.958044673317847, -12.041015625000007 25.83944940206318, -12.041015625000007 23.32208001137844, -13.051757812500014 23.1605633090483, -12.963867187500014 21.371244370618306, -16.699218750000014 21.28937435586042, -16.87500000000001 21.125497636606276, -16.040039062499996 19.683970235888427, -16.215820312499993 19.145168196205283, -15.908203125 17.97873309555617, -16.43554687499999 16.13026201203474, -15.908203125 16.3833911236084, -15.205078124999987 16.3833911236084, -14.458007812500005 16.3833911236084, -13.974609374999987 16.3833911236084, -13.535156249999998 15.919073517982425, -13.271484375000005 15.707662769583493, -12.832031249999985 15.199386048560008, -12.348632812499998 14.902321826141783, -11.733398437500014 14.77488250651626, -11.249999999999996 15.453680224345847, -10.854492187500005 14.987239525774233, -10.327148437499985 15.284185114076446, -9.140624999999985 15.496032414238634, -8.129882812500012 15.326571801420842, -6.767578125000016 15.368949896534717, -5.581054687500015 15.368949896534717, -5.27343749999999 15.792253570362445, -5.537109375000016 16.46769474828897, -5.844726562500008 18.93746442964186, -6.416015624999993 24.926294766395582, -4.833984375000002 24.926294766395582, -6.811523437500015 26.11598592533351, -8.613281249999998 27.254629577800077))

', 4326)
SELECT @b as mauritania, 'blue' as [Color], 1 as [Thickness];

//linke for points with in polygon
http://social.msdn.microsoft.com/Forums/en/sqlspatial/thread/f6e57f97-3c4d-4e62-965b-ab15f51ad45e
//

//count points inside polygon
SELECT count(*)
FROM TestPolygon a, TestPoint b
WHERE b.geom.STIntersects(a.geom) = 1
//

//insert query geography mauritania
DECLARE @h geography;
SET @h = geography::STGeomFromText('
POLYGON((-8.613281249999998 27.254629577800077, -8.569335937500001 25.958044673317847, -12.041015625000007 25.83944940206318, -12.041015625000007 23.32208001137844, -13.051757812500014 23.1605633090483, -12.963867187500014 21.371244370618306, -16.699218750000014 21.28937435586042, -16.87500000000001 21.125497636606276, -16.040039062499996 19.683970235888427, -16.215820312499993 19.145168196205283, -15.908203125 17.97873309555617, -16.43554687499999 16.13026201203474, -15.908203125 16.3833911236084, -15.205078124999987 16.3833911236084, -14.458007812500005 16.3833911236084, -13.974609374999987 16.3833911236084, -13.535156249999998 15.919073517982425, -13.271484375000005 15.707662769583493, -12.832031249999985 15.199386048560008, -12.348632812499998 14.902321826141783, -11.733398437500014 14.77488250651626, -11.249999999999996 15.453680224345847, -10.854492187500005 14.987239525774233, -10.327148437499985 15.284185114076446, -9.140624999999985 15.496032414238634, -8.129882812500012 15.326571801420842, -6.767578125000016 15.368949896534717, -5.581054687500015 15.368949896534717, -5.27343749999999 15.792253570362445, -5.537109375000016 16.46769474828897, -5.844726562500008 18.93746442964186, -6.416015624999993 24.926294766395582, -4.833984375000002 24.926294766395582, -6.811523437500015 26.11598592533351, -8.613281249999998 27.254629577800077))
', 4326);
insert into Test (geog) values(@h)
///

------------------------------------------------------------------------------------------------------
DECLARE @a geography;
SET @a = geography:: STGeomFromText('
POLYGON((78.486328125 32.472695022061494, 79.18945312500001 32.43561304116276, 79.58496093750001 32.9164853473144, 79.58496093750001 33.76088200086919, 79.4091796875 34.23451236236986, 78.7939453125 35.02999636902566, 78.310546875 35.2815006578912, 77.56347656249998 35.353216101238225, 77.03613281250001 35.13787911963418, 76.59667968750001 34.813803317113155, 75.9375 34.56085936708384, 74.39941406250001 34.885930940753155, 73.8720703125 34.59704151614415, 74.13574218750003 33.90689555128868, 74.17968750000001 33.35806161277886, 73.828125 33.174341551002065, 75.23437499999998 32.175612478499325, 74.61914062500001 31.01527898171125, 74.00390625 30.25906720321302, 73.564453125 29.45873118535532, 72.7734375 28.729130483430154, 71.98242187500001 28.265682390146462, 70.79589843750001 27.722435918973456, 70.18066406249998 27.916766641249076, 69.56542968750001 26.824070780470194, 70.048828125 26.352497858154, 70.53222656250001 25.60190226111575, 71.1474609375 24.56710835257599, 69.873046875 24.20688962239801, 68.818359375 24.126701958681657, 68.33496093750001 24.006326198751114, 68.818359375 22.958393318086322, 69.697265625 22.593726063929296, 70.57617187500001 23.1605633090483, 70.18066406249998 22.350075806124863, 69.0380859375 22.024545601240312, 69.56542968750001 21.412162229725403, 70.88378906250001 20.79720143430697, 71.98242187500001 20.961439614096853, 72.37792968750001 21.739091217718563, 73.74023437499998 21.77990534252963, 72.7734375 20.756113874762068, 72.86132812500001 18.979025953255266, 72.94921875000003 18.687878686034196, 73.12500000000001 17.853290114098012, 73.6962890625 15.792253570362445, 74.26757812500001 14.519780046326085, 74.48730468750001 13.068776734357692, 75.05859375 12.511665400971018, 75.673828125 11.307707707765438, 76.5087890625 9.622414142924805, 76.64062500000001 9.10209673872643, 77.4755859375 8.10273857778318, 78.26660156250001 8.841651120809132, 79.05761718750001 9.579084335882534, 79.716796875 10.703791711680736, 79.9365234375 11.910353555774111, 80.33203125000001 13.49647276575895, 80.33203125000001 15.665354182093274, 81.29882812499998 15.961329081596634, 82.3974609375 17.056784609942543, 83.93554687500001 17.89511430374914, 85.34179687500003 19.186677697957833, 86.39648437500001 20.014645445341365, 86.74804687500001 21.453068633086772, 88.2861328125 21.493963563064458, 89.25292968750001 21.983801417384686, 90.615234375 22.30942584120018, 91.1865234375 22.87744046489713, 90.04394531250001 23.443088931121785, 90.08789062500003 24.246964554300913, 91.75781250000001 24.846565348219744, 92.1533203125 24.966140159912964, 92.021484375 25.918526162075153, 90.35156250000001 25.918526162075153, 89.38476562500001 25.878994400196202, 90.39550781250001 26.745610382199025, 92.373046875 27.137368359795584, 91.62597656250003 27.722435918973456, 89.8681640625 27.644606381943326, 87.62695312500001 27.41078570257701, 85.56152343750003 27.800209937418252, 83.6279296875 28.226970038918328, 81.95800781250001 28.613459424004418, 80.20019531249998 28.6905876542507, 80.50781250000003 29.954934549656133, 81.03515625 30.486550842588475, 79.23339843750001 31.20340495091738, 78.7939453125 31.278550858946534, 78.486328125 32.472695022061494))
', 4326)
SELECT @a as india, 'blue' as [Color], 1 as [Thickness];


DECLARE @b geography;
SET @b = geography:: STGeomFromText('
POLYGON((116.89453125000001 49.781264058178344, 114.52148437500003 50.17689812200105, 113.02734375 49.43955695894084, 110.390625 48.92249926375824, 108.10546875 49.2104204456503, 107.75390625000001 49.894634395734215, 106.34765625000001 50.28933925329178, 104.67773437499998 50.23315183247224, 103.35937499999998 50.12057809796007, 102.39257812500003 50.625073063414355, 102.04101562499998 51.124212757826875, 100.986328125 51.39920565355377, 98.96484375000001 51.944264879028765, 98.349609375 51.56341232867588, 97.998046875 51.013754657188194, 98.349609375 50.0641917366591, 97.47070312500003 49.66762782262193, 96.50390625000001 49.83798245308484, 95.36132812500001 49.83798245308484, 94.5703125 50.28933925329178, 93.251953125 50.625073063414355, 92.46093750000003 50.68079714532164, 91.0546875 50.23315183247224, 90.263671875 49.95121990866204, 89.56054687500001 49.95121990866204, 89.56054687500001 49.553725513475776, 88.85742187500001 49.553725513475776, 88.41796875 49.325121991040014, 87.451171875 49.03786794532642, 88.24218750000001 48.45835188280866, 89.033203125 47.93106634750977, 90.13183593750003 47.635783590864854, 90.70312500000001 47.21956811231548, 91.09863281249998 46.468132992155546, 91.0546875 45.67548217560646, 90.7470703125 45.367584368849776, 91.53808593750003 45.05800143539828, 92.59277343750003 44.902577996288855, 93.73535156250003 44.93369638969467, 94.6142578125 44.43377984606822, 95.31738281250001 44.08758502824516, 95.8447265625 43.3890819391175, 96.45996093750001 42.71473218539458, 97.77832031250001 42.617791432823445, 99.31640625 42.488301979602255, 100.7666015625 42.488301979602255, 101.77734375 42.42345651793832, 102.83203125000001 41.96765920367817, 104.15039062500001 41.672911819602085, 105.20507812500001 41.47566020027823, 106.87499999999998 42.2285173562085, 108.50097656250003 42.32606244456202, 109.73144531250003 42.32606244456202, 110.96191406250001 42.940339233631825, 112.06054687500003 43.644025847699496, 111.5771484375 44.08758502824516, 111.533203125 44.62175409623326, 112.54394531250001 44.995882618165446, 113.42285156250001 44.68427737181225, 114.25781249999998 44.995882618165446, 114.78515625000001 45.49094569262733, 115.70800781249997 45.39844997630408, 116.4111328125 45.82879925192133, 117.02636718750001 46.316584181822186, 117.77343749999998 46.619261036171515, 118.6083984375 46.80005944678731, 119.31152343750001 46.73986059969267, 119.53125000000003 46.468132992155546, 119.8388671875 46.73986059969267, 119.794921875 47.1897124644842, 119.35546874999997 47.338822694822, 118.56445312500004 47.78363463526376, 118.03710937499998 47.98992166741419, 117.50976562500001 47.606163043868726, 116.5869140625 47.901613541420765, 115.97167968750001 47.66538735632655, 115.62011718749998 48.07807894349861, 116.01562500000001 48.60385760823253, 116.32324218750001 48.95136647094771, 116.89453125000001 49.781264058178344))
', 4326)
SELECT @b as mangolia, 'blue' as [Color], 1 as [Thickness];

DECLARE @c geography;
SET @c = geography:: STGeomFromText('
POLYGON((32.95898437500001 -26.922069916732795, 32.10205078125001 -26.863280626766265, 32.10205078125001 -26.431228064506442, 32.08007812499999 -25.958044673317847, 32.1240234375 -25.145284610685063, 32.058105468750014 -24.327076540018634, 31.66259765624999 -23.7048945023249, 31.574707031249993 -23.443088931121785, 31.574707031249993 -23.079731762449878, 31.420898437500017 -22.41102852155869, 31.047363281250007 -22.41102852155869, 30.344238281249993 -22.41102852155869, 29.72900390625001 -22.207749178410846, 29.113769531249996 -22.2280904167845, 28.91601562500001 -22.51255695405145, 28.520507812499993 -22.59372606392932, 28.146972656249986 -22.91792293614603, 27.663574218749996 -23.281719175600035, 26.916503906249986 -23.7048945023249, 26.850585937500007 -24.347096633808512, 26.38916015625 -24.666986385216248, 25.993652343750014 -24.766784522874452, 25.708007812500003 -25.264568475331586, 25.576171875000007 -25.72073513441213, 24.916992187499993 -25.78010711842222, 24.060058593749996 -25.72073513441213, 23.48876953125001 -25.34402602913433, 23.04931640624999 -25.363882272740255, 22.74169921875 -25.918526162075153, 22.236328124999996 -26.352497858154, 21.81884765624999 -26.745610382199025, 21.291503906250003 -26.843677401113002, 20.8740234375 -26.863280626766265, 20.58837890624999 -26.824070780470204, 20.63232421874999 -26.450902223672646, 20.8740234375 -26.076520559856973, 20.7421875 -25.681137335685328, 20.43457031250001 -25.22482017676504, 20.28076171875 -24.926294766395596, 20.017089843750007 -24.786734541988878, 20.061035156250003 -28.34306490482549, 19.709472656250014 -28.536274512989916, 19.357910156249993 -28.63274679922588, 19.24804687500001 -28.94086176940554, 18.874511718750003 -28.921631282421316, 18.083496093749993 -28.863918426224565, 17.446289062499993 -28.748396571187406, 17.402343749999996 -28.497660832963475, 17.49023437499999 -28.362401735238236, 17.292480468750014 -28.265682390146477, 17.116699218749986 -28.033197847676376, 16.87500000000001 -28.05259082333983, 16.853027343749993 -28.304380682962783, 16.457519531250003 -28.652030630362262, 16.853027343749993 -29.20971322586816, 17.116699218749986 -29.688052749856776, 17.292480468750014 -30.353916372297064, 17.644042968750003 -31.052933985705163, 17.885742187500014 -31.353636941500987, 18.17138671874999 -31.672083485607402, 18.369140625 -32.17561247849935, 18.347167968749986 -32.76880048488168, 18.01757812500001 -32.84267363195431, 17.929687500000014 -32.99023555965109, 18.039550781249993 -33.247875947924385, 18.369140625 -33.46810795527896, 18.479003906250017 -33.925129700072, 18.237304687500007 -34.10725639663118, 18.369140625 -34.379712580462204, 18.61083984375001 -34.08906131584997, 18.80859374999999 -34.21634468843463, 18.764648437499993 -34.41597338448186, 19.028320312500014 -34.52466147177173, 19.40185546874999 -34.57895241036948, 19.643554687500003 -34.70549341022544, 20.061035156250003 -34.831841149828676, 20.43457031250001 -34.50655662164561, 21.027832031250014 -34.45221847282654, 21.70898437500001 -34.41597338448186, 22.126464843750014 -34.10725639663118, 22.499999999999993 -34.08906131584997, 22.807617187500014 -34.30714385628804, 23.269042968750017 -34.21634468843463, 23.48876953125001 -33.99802726234877, 23.862304687499986 -34.05265942137599, 24.41162109374999 -34.21634468843463, 24.873046874999993 -34.21634468843463, 25.026855468750007 -34.03445260967644, 25.46630859374999 -34.05265942137599, 26.037597656250007 -33.815666308702774, 26.696777343749993 -33.77914733128647, 27.31201171875001 -33.50475906922609, 28.01513671874999 -32.953368145799345, 28.652343749999993 -32.565333160841035, 29.619140625 -31.59725256170666, 30.256347656249996 -31.034108344903487, 30.695800781250014 -30.315987718557892, 30.937499999999993 -29.85970144212676, 31.728515625000003 -28.979312036722437, 32.36572265625001 -28.63274679922588, 32.38769531249999 -28.130127737874002, 32.58544921875 -27.664068965384512, 32.95898437500001 -26.922069916732795))
', 4326)
SELECT @c as sa, 'blue' as [Color], 1 as [Thickness];

DECLARE @d geography;
SET @d = geography:: STGeomFromText('
POLYGON((16.479492187499986 -28.613459424004418, 17.138671875000003 -28.226970038918342, 17.578124999999986 -28.690587654250684, 18.4130859375 -28.806173508854804, 19.20410156250001 -28.921631282421316, 19.863281249999993 -28.420391085674303, 20.17089843749999 -22.105998799750576, 21.049804687499993 -22.024545601240326, 21.137695312499993 -18.437924653474393, 22.763671875000014 -18.06231230454674, 21.533203125000017 -18.06231230454674, 19.467773437500003 -17.978733095556183, 18.45703125 -17.43451055152288, 14.985351562499991 -17.476432197195503, 13.754882812499993 -17.476432197195503, 13.403320312500001 -17.14079039331665, 12.524414062499994 -17.266727823520504, 11.777343750000014 -17.266727823520504, 11.90917968750001 -18.271086109608877, 12.788085937499987 -18.812717856407786, 13.623046874999998 -20.385825381874276, 13.886718749999991 -21.453068633086783, 14.633789062500003 -22.59372606392932, 14.545898437500007 -24.04646399966657, 14.853515624999998 -25.403584973186706, 15.292968749999987 -26.824070780470204, 15.512695312500012 -27.56672143040971, 15.600585937500008 -26.70635985763354, 16.479492187499986 -28.613459424004418))
', 4326)
SELECT @d as namibia, 'blue' as [Color], 1 as [Thickness];

Spatial Data Types


Geometry Data Type Examples


STArea()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g, @g.STArea() as [AreaInUnits], 0.1 as [Thickness];

STAsBinary()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 3)', 0);
SELECT @g, @g.STAsBinary(), 0.1 as [Thickness];

STAsText()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 3)', 0);
SELECT @g, @g.STAsText() as [WellKnownText], 0.1 as [Thickness];

STBoundary()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 0 2, 2 0)', 0);
SELECT @g, @g.STBoundary(), 0.1 as [Thickness];

/*
Boundary is defined by the OGC as follows:
- Point and MultiPoint instances do not have a boundary.
- LineString and MultiLineString boundaries are formed by the start points and end points, removing those that occur an even number of times.
*/

STBuffer()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 4 0)', 0);
SELECT @g, @g.STBuffer(1), 0.1 as [Thickness];

STCentroid()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g, @g.STCentroid(), 0.1 as [Thickness];

STContains()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POINT(1 1)', 0);
SELECT @g, @h, @g.STContains(@h), 0.1 as [Thickness];

STConvexHull()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 1 1, 2 2, 2 0, 0 0))', 0);
SELECT @g, 'Original' AS [Display], 'Blue' as [Color], 0.2 as [Thickness]
UNION ALL
SELECT @g.STConvexHull(), 'Hull', 'Green' as [Color], 0.1 as [Thickness];


STCrosses()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0)', 0);
SET @h = geometry::STGeomFromText('LINESTRING(0 0, 2 2)', 0);
SELECT @g, @h, @g.STCrosses(@h), 0.1 as [Thickness];


STDifference()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT 'Original Polygons' as [Display], @g, @h, 'Green' as [Color], 0.2 as [Thickness]
UNION ALL
SELECT 'First minus second', null, @g.STDifference(@h), 'Blue' as [Color], 0.1 as [Thickness]
UNION ALL
SELECT 'Second minus first', null, @h.STDifference(@g), 'Orange' as [Color], 0.05 as [Thickness];
STDimension
DECLARE @temp table ([name] varchar(10), [geom] geometry);
INSERT INTO @temp values ('LineString', geometry::STGeomFromText('LINESTRING(0 0, 3 3)', 0));
INSERT INTO @temp values ('Polygon', geometry::STGeomFromText('POLYGON((0 0, 3 0, 0 3, 0 0))', 0));
INSERT INTO @temp values ('Point', geometry::STGeomFromText('POINT(3 3)', 0));
SELECT [name], [geom], [geom].STDimension() as [dim], 0.1 as [Thickness]
FROM @temp;

STDisjoint()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('POINT(1 1)', 0);
SELECT @g, @h, @g.STDisjoint(@h), 0.1 as [Thickness];

STDistance
DECLARE @g geometry;
DECLARE @h geometry;
DECLARE @i geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POINT(10 10)', 0);
SET @i = geometry::STGeomFromText('LINESTRING(2 2,10 10)', 0);
SELECT @g, @g.STDistance(@h), 0.1 as [Thickness], 'Green' as [Color]
UNION ALL
SELECT @i, @i.STLength(), 0.1 as [Thickness], 'Gray' as [Color] -- demonstration line
UNION ALL
SELECT @h, null, 0.3 as [Thickness], 'Red' as [Color];


STEndPoint()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g, @g.STEndPoint(), 0.1 as [Thickness];

STEnvelope()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 3)', 0);
SELECT @g, @g.STEnvelope(), 0.1 as [Thickness];

STEquals()

DECLARE @g geometry
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('MULTILINESTRING((4 2, 2 0), (0 2, 2 0))', 0);
SELECT @h, @h.STEquals(@g), 'Blue' as [Color], 0.2 as [Thickness]
UNION ALL
SELECT @g, @g.STEquals(@h), 'Orange' as [Color], 0.1 as [Thickness];

STExteriorRing()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g, 0.2 as [Thickness]
UNION ALL
SELECT @g.STExteriorRing(), 0.1 as [Thickness];

STGeometryN()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('MULTIPOINT(0 0, 13.5 2, 7 19)', 0);
SELECT @g, 'Red' AS [Color], 1.5 as [Thickness]
UNION ALL
SELECT @g.STGeometryN(2), 'Yellow' AS [Color], 0.75 as [Thickness];

STGeometryType()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 0);
SELECT @g, @g.STGeometryType() as [GeometryType], 0.1 as [Thickness];


STInteriorRing()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g, @g.STInteriorRingN(1) AS [FirstInteriorRing], 0.1 as [Thickness];

STIntersection()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g, 'Blue' as [Color], 0.3 as [Thickness]
UNION ALL
SELECT @h, 'Green' as [Color], 0.2 as [Thickness]
UNION ALL
SELECT @g.STIntersection(@h), 'Orange' as [Color], 0.1 as [Thickness];

STIntersects()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('POINT(1 1)', 0);
SELECT @g, @h, @g.STIntersects(@h), 0.1 as [Thickness];

STIsClosed()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g, @g.STIsClosed() AS [IsClosed], 0.1 as [Thickness];

STIsEmpty()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON EMPTY', 0);
SELECT @g, @g.STIsEmpty(), 0.1 as [Thickness];

STIsRing()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0, 0 0)', 0);
SELECT @g, @g.STIsRing(), 0.1 as [Thickness];


STIsSimple()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 0 2, 2 0)', 0);
SELECT @g, @g.STIsSimple(), 0.1 as [Thickness];

STIsValid()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g, @g.STIsValid(), 0.1 as [Thickness];

STLength()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g, @g.STLength(), 0.1 as [Thickness];


STNumGeometries()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('MULTIPOINT(0 0, 13.5 2, 7 19)', 0);
SELECT @g.STNumGeometries() as [NumberOfGeometries], @g, 1 as [Thickness];


STNumInteriorRing()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g.STNumInteriorRing() as [NumberOfInteriorRings], @g, 0.1 as [Thickness];


STNumPoints()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g.STNumPoints() as [NumberOfPoints], @g, 0.1 as [Thickness];


STOverlaps()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g, @h, @g.STOverlaps(@h) as [DoesItOverlap], 0.1 as [Thickness];

STPointN()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g, @g.STPointN(2), 0.1 as [Thickness];


STPointOnSurface()

-- Returns an arbitrary point located within the interior of a geometry instance

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @g, @g.STPointOnSurface(), 0.1 as [Thickness];

STRelate()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('POINT(5 5)', 0);
SELECT @g, @h, @g.STRelate(@h, 'FF*FF****'), 0.1 as [Thickness];

STSrid()

--The first example creates a geometry instance with the SRID value 13 and uses STSrid to confirm the SRID.

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 13);
SELECT @g.STSrid


/*
--The second example uses STSrid to change the SRID value of the instance to 23 and then confirms the modified SRID value.
SET @g.STSrid = 23;
SELECT @g.STSrid;
*/


STStartPoint()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g, @g.STStartPoint(), 0.1 as [Thickness];

STSymDifference()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g, 'Green' as [Color], 0.2 as [Thickness]
UNION ALL
SELECT @h, 'Green' as [Color], 0.2 as [Thickness]
UNION ALL
SELECT @g.STSymDifference(@h).STGeometryN(1), 'Blue' as [Color], 0.1 as [Thickness]
UNION ALL
SELECT @g.STSymDifference(@h).STGeometryN(2), 'DarkBlue' as [Color], 0.1 as [Thickness];

STTouches()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 2, 2 0, 4 2)', 0);
SET @h = geometry::STGeomFromText('POINT(1 1)', 0);
SELECT @g.STTouches(@h) as [DoesItTouch]
, @g.STIntersects(@h) as [DoesItIntersect]
, @g, @h, 0.1 as [Thickness];

--Two geometry instances touch if their point sets intersect, but their interiors do not intersect.


STUnion()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g, @h, @g.STUnion(@h), 0.1 as [Thickness];

STWithin()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
SET @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g, @h, @g.STWithin(@h), 0.1 as [Thickness];

STX()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT(3 8)', 0);
SELECT @g.STX, 0.1 as [Thickness];

STY()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT(3 8)', 0);
SELECT @g.STY, 0.1 as [Thickness];

BufferWithTolerance()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POINT(3 3)', 0);
SELECT @g, 0.1 as [Thickness], 'Red' as [Color]
UNION ALL
SELECT @g.BufferWithTolerance(1, .5, 0), 0.1 as [Thickness], 'Green' as [Color]
UNION ALL
SELECT @g.BufferWithTolerance(2, .5, 0), 0.05 as [Thickness], 'Blue' as [Color]
UNION ALL
SELECT @g.BufferWithTolerance(2, .2, 0), 0.05 as [Thickness], 'Orange' as [Color];

InstanceOf()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('MULTIPOINT(0 0, 13.5 2, 7 19)', 0);
SELECT @g, @g.InstanceOf('GEOMETRYCOLLECTION'), 2 as [Thickness];

Reduce()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 0 1, 1 0, 2 1, 3 0, 4 1)', 0);
SELECT @g, 'Original' as [Display], 'Blue' AS [Color], 0.2 AS [Thickness]
UNION ALL
SELECT @g.Reduce(.75), 'Reduced' as [Display], 'Red' AS [Color], 0.1 AS [Thickness]


ToString()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 0 1, 1 0)', 0);
SELECT @g, @g.ToString(), 0.1 as [Thickness];



Geography Data Type Examples


STArea()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SELECT @g, @g.STArea(), 'Blue' as [Color], 10 as [Thickness];

STAsBinary()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g.STAsBinary();

STAsText()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING( -122.360 47.656, -122.343 47.656)', 4326);
SELECT @g.STAsText();

STBuffer()

DECLARE @h geography;
SET @h = geography::STGeomFromText('POINT(-118.24 33.56)', 4326);
SELECT @h, 8 as [Thickness], 'Green' AS [COLOR]
UNION ALL
SELECT @h.STBuffer(5100000), 1 as [Thickness], 'Blue' AS [COLOR];
/*
DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g.STBuffer(1).ToString(); */

STDifference()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON((122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SET @h = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @h, @g.STDifference(@h), 4 AS [Thickness];

STDimension()

DECLARE @temp table ([name] varchar(10), [geom] geography);

INSERT INTO @temp values ('Point', geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326));
INSERT INTO @temp values ('LineString', geography::STGeomFromText('LINESTRING( -122.360 47.656, -122.343 47.656)', 4326));
INSERT INTO @temp values ('Polygon', geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));

SELECT [name], [geom], [geom].STDimension() AS [dim], 4 AS [Thickness]
FROM @temp;


STDisjoint()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g, @h, @g.STDisjoint(@h);

STDistance()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SET @h = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g, @h, @g.STDistance(@h);


STEndPoint()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.STEndPoint()

STEquals()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('GEOMETRYCOLLECTION(POLYGON((-122.368 47.658, -122.338 47.649, -122.338 47.658, -122.368 47.658, -122.368 47.658)), LINESTRING(-122.360 47.656, -122.343 47.656), POINT (-122.35 47.656))', 4326);
SET @h = geography::STGeomFromText('POLYGON((-122.368 47.658, -122.338 47.649, -122.338 47.658, -122.368 47.658, -122.368 47.658))', 4326);
SELECT @g, @h, @g.STEquals(@h);

STGeometryN()

DECLARE @g geography;
SET @g = geography::STGeomFromText('MULTIPOINT(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.STGeometryN(2);

STGeometryType()

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SELECT @g, @g.STGeometryType();

STIntersection()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SET @h = geography::STGeomFromText('LINESTRING( -122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @h, @g.STIntersection(@h);

STIntersects()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649 , -122.348 47.658 , -122.358 47.658 , -122.358 47.653 ))', 4326);
SET @h = geography::STGeomFromText('LINESTRING( -122.360 47.656, -122.343 47.656 )', 4326);
SELECT @g, @h, @g.STIntersects(@h);

-- note this example appears incomplete on the MSDN page

STIsClosed()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649 , -122.348 47.658 , -122.358 47.658 , -122.358 47.653 ))', 4326);
SELECT @g, @g.STIsClosed();

STIsEmpty()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POLYGON EMPTY', 4326);
SELECT @g, @g.STIsEmpty();

STLength()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.STLength();

STNumGeometries()

DECLARE @g geography;
SET @g = geography::STGeomFromText('MULTIPOINT((-122.360 47.656), (-122.343 47.656 ))', 4326);
SELECT @g, @g.STNumGeometries();

STNumPoints()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.STNumPoints();

STPointN()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.STPointN(2)

STSrid()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g.STSrid;
/*
--The second example uses STSrid to change the SRID value of the instance to 4267
--(NAD27) and then confirms the modified SRID value.
SET @g.STSrid = 4267;
SELECT @g.STSrid;*/

STStartPoint()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.STStartPoint()

STSymDifference()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658 , -122.358 47.653 ))', 4326);
SET @h = geography::STGeomFromText('POLYGON((-122.351 47.656, -122.341 47.656, -122.341 47.661, -122.351 47.661, -122.351 47.656))', 4326);
SELECT @g, @h, @g.STSymDifference(@h)

STUnion()

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON(( -122.358 47.653, -122.348 47.649 , -122.348 47.658 , -122.358 47.658 , -122.358 47.653 ))', 4326);
SET @h = geography::STGeomFromText('POLYGON((-122.351 47.656, -122.341 47.656 , -122.341 47.661 , -122.351 47.661 , -122.351 47.656 ))', 4326);
SELECT @g, @h, @g.STUnion(@h)

AsGml()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326);
SELECT @g.AsGml();

AsTextZM()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100 10.3 12)', 4326);
SELECT @g.STAsText();
SELECT @g.AsTextZM()

BufferWithTolerance()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g, @g.BufferWithTolerance(1, .5, 0);

InstanceOf()

DECLARE @g geography;
SET @g = geography::STGeomFromText('MULTIPOINT(-122.360 47.656, -122.343 47.656)', 4326);
SELECT @g, @g.InstanceOf('GEOMETRYCOLLECTION');

IsNull()

/* IsNull can be used to test whether a geography instance is null. This can produce somewhat confusing results, returning 0 if the instance is not null, but null if the instance is null.
This method is primarily used by the SQL Server infrastructure; it is not recommended that you use IsNull to test whether a geography instance is null.*/

Lat()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g, @g.Lat;

Long()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g, @g.Long;

M (measure)

DECLARE @g geography;
SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100 10.3 12)', 4326);
SELECT @g, @g.M;

NumRings()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649 , -122.348 47.658 , -122.358 47.658 , -122.358 47.653 ), (-122.357 47.654 , -122.357 47.657 , -122.349 47.657 , -122.349 47.650 , -122.357 47.654 ))', 4326);
SELECT @g, @g.NumRings();

RingN()

DECLARE @g geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649 , -122.348 47.658 , -122.358 47.658 , -122.358 47.653 ), (-122.357 47.654 , -122.357 47.657 , -122.349 47.657 , -122.349 47.650 , -122.357 47.654 ))', 4326);
SELECT @g, @g.RingN(2)

ToString()

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING( -122.360 47.656, -122.343 47.656 )', 4326);
SELECT @g, @g.ToString()

Z (elevation)

DECLARE @g geography;
SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100 10.3 12)', 4326);
SELECT @g, @g.Z;

View Created Polygon On Google Map

[script type="text/javascript"]
var map = null;
var mapClickListener = null;

var routePoints = new Array();
var routeMarkers = new Array();
var routeOverlays = new Array();
var totalDistance = 0.0;
var lineIx = 0;
var mylistener;
function load() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"), { draggableCursor: 'default', draggingCursor: 'pointer' });
map.setCenter(new GLatLng(45.0, 7.0), 3);
var customUI = map.getDefaultUI();
customUI.controls.maptypecontrol = false;
customUI.controls.menumaptypecontrol = true;
map.setUI(customUI);

geocoder = new GClientGeocoder();
GEvent.addListener(map, "mousemove", function(point) {
mousemovepoint = point;
var LnglatStr6 = point.lng().toFixed(6) + ', ' + point.lat().toFixed(6);
var latLngStr6 = point.lat().toFixed(6) + ', ' + point.lng().toFixed(6);
});

DrawRoute();
}
}

function DrawRoute() {

var strValue = '<%=strGeoPoints%>';
var strItems = strValue.split('$');

var dist = 0;
if (!routePoints[lineIx]) {
routePoints[lineIx] = Array();
routeMarkers[lineIx] = Array();
}

for (i = 0; i < strItems.length; i++) {
var item = new String(strItems[i]);
var index = item.indexOf(',');

if (index != -1) {
var point = new GLatLng(item.substring(index + 1), item.substring(0, index));
routePoints[lineIx].push(point);
plotRoute();

}
}
plotRoute();

}


function plotRoute() {

if (routeOverlays[lineIx]) {
map.removeOverlay(routeOverlays[lineIx]);
}
routeOverlays[lineIx] = new GPolyline(routePoints[lineIx], '#C602C8', 3, 1);
map.addOverlay(routeOverlays[lineIx]);
}
[/script]

[body onload="load()" onunload="GUnload()"]
[form id="form1" runat="server"]
[div id="map" style="width: 950px; height: 600px"]
[/div]
[/form]
[/body]
[strGeoPoints = "72.949219,29.036961$ 73.388672,29.954935$ 73.916016,30.145127$ 73.916016,30.410782$ 74.223633,30.751278$ 74.575195,31.052934$ 74.575195,31.466154$ 74.487305,31.728167$ 74.663086,31.989442$ 75.19043,32.175612$ 75.146484,32.435613$ 74.663086,32.583849$ 74.619141,33.063924$ 74.135742,33.211116$ 73.608398,33.211116$ 73.388672,34.016242$ 73.300781,34.415973$ 73.520508,34.597042$ 73.78418,34.813803$ 74.135742,35.137879$ 73.652344,35.317366$ 73.652344,35.56798$ 73.256836,35.56798$ 73.081055,35.817813$ 72.509766,35.817813$ 72.729492,36.173357$ 72.905273,36.385913$ 73.212891,36.5626$ 73.520508,36.774092$ 73.916016,36.738884$ 73.916016,36.949892$ 74.355469,36.949892$ 74.707031,36.985003$ 75.234375,36.985003$ 75.585938,36.914764$ 75.849609,36.597889$ 76.333008,36.315125$ 76.816406,36.102376$ 77.255859,35.746512$ 77.651367,35.532226$ 78.178711,35.639441$ 78.793945,35.85344$ 79.145508,35.995785$ 79.584961,35.85344$ 80.024414,35.56798$ 80.112305,35.209722$ 80.112305,34.885931$ 79.628906,34.415973$ 79.40918,34.089061$ 78.881836,33.797409$ 78.969727,33.431441$ 79.145508,32.953368$ 79.277344,32.583849$ 78.881836,32.324276$ 78.530273,32.546813$ 78.618164,32.063956$ 78.837891,31.466154$ 79.145508,31.090574$ 79.541016,31.052934$ 80.288086,30.637912$ 80.771484,30.183122$ 81.166992,30.107118$ 81.430664,30.334954$ 81.870117,30.297018$ 82.177734,30.107118$ 82.529297,29.840644$ 83.012695,29.61167$ 83.320313,29.42046$ 83.583984,29.267233$ 84.067383,29.267233$ 84.287109,28.844674$ 84.814453,28.652031$ 85.253906,28.652031$ 85.297852,28.265682$ 85.737305,28.033198$ 87.319336,27.916767$ 88.286133,27.955591$ 88.681641,28.033198$ 89.296875,27.683528$ 89.560547,28.07198$ 90.307617,28.188244$ 90.74707,27.994401$ 91.582031,27.955591$ 92.109375,27.80021$ 92.548828,27.994401$ 92.944336,28.459033$ 93.735352,28.72913$ 94.350586,29.113775$ 94.96582,29.152161$ 95.932617,29.305561$ 96.328125,29.036961$ 96.723633,28.690588$ 97.338867,28.149503$ 97.163086,27.839076$ 96.943359,27.605671$ 96.987305,27.176469$ 96.328125,27.332735$ 95.800781,26.980829$ 95.229492,26.863281$ 95.009766,26.076521$ 94.526367,25.363882$ 94.790039,25.125393$ 94.438477,24.647017$ 94.086914,23.885838$ 93.295898,24.126702$ 93.383789,23.281719$ 93.032227,22.998852$ 93.032227,22.22809$ 92.636719,21.983801$ 92.460938,22.390714$ 92.373047,23.039298$ 92.329102,23.604262$ 92.021484,23.684774$ 91.582031,23.079732$ 91.142578,23.684774$ 91.230469,24.126702$ 91.845703,24.487149$ 92.197266,24.806681$ 92.329102,25.125393$ 91.713867,25.125393$ 90.966797,25.284438$ 90.175781,25.284438$ 89.912109,25.244696$ 89.604492,25.244696$ 89.296875,24.647017$ 89.648438,24.046464$ 89.868164,23.765237$ 90.395508,23.241346$ 90.65918,22.674847$ 90.615234,22.268764$ 90.087891,21.779905$ 89.472656,21.820708$ 88.901367,22.065278$ 88.549805,22.268764$ 88.022461,22.105999$ 87.890625,21.739091$ 87.319336,21.493964$ 86.835938,21.453069$ 86.835938,20.797201$ 86.572266,20.055931$ 85.517578,19.808054$ 84.990234,19.394068$ 84.375,18.646245$ 83.540039,17.978733$ 82.836914,17.518344$ 82.177734,17.224758$ 82.30957,16.636192$ 81.518555,16.383391$ 80.991211,15.876809$ 80.332031,15.623037$ 80.112305,15.114553$ 80.288086,14.179186$ 80.288086,13.368243$ 80.200195,12.511665$ 79.716797,11.910354$ 79.848633,11.135287$ 79.672852,10.401378$ 79.189453,10.09867$ 78.837891,9.449062$ 78.354492,9.145486$ 78.222656,8.581021$ 77.519531,8.015716$ 76.948242,8.233237$ 76.464844,8.971897$ 76.420898,9.709057$ 76.157227,10.444598$ 75.849609,11.307708$ 75.234375,12.039321$ 74.663086,13.154376$ 74.443359,14.221789$ 73.78418,15.326572$ 73.432617,16.256867$ 73.037109,17.769612$ 72.817383,19.020577$ 72.817383,20.220966$ 72.861328,20.920397$ 72.773438,21.739091$ 72.685547,22.105999$ 72.333984,22.146708$ 72.158203,21.534847$ 71.806641,21.125498$ 71.103516,20.797201$ 70.664063,20.797201$ 70.048828,21.207459$ 69.65332,21.534847$ 69.125977,21.983801$ 68.818359,22.309426$ 69.169922,22.471955$ 69.829102,22.471955$ 70.3125,22.593726$ 70.356445,22.998852$ 70.048828,23.120154$ 69.477539,22.917923$ 68.950195,22.958393$ 68.598633,23.563987$ 68.90625,24.20689$ 69.609375,24.20689$ 70.180664,24.246965$ 70.883789,24.287027$ 71.103516,24.766785$ 70.708008,25.482951$ 70.576172,25.720735$ 70.224609,25.720735$ 70.136719,25.878994$ 70.136719,26.509905$ 69.477539,26.74561$ 69.56543,27.215556$ 70.092773,27.722436$ 70.356445,28.033198$ 70.708008,27.877928$ 71.806641,28.07198$ 72.114258,28.304381$ 72.246094,28.72913$ 72.641602,28.921631$ 72.949219,29.036961";]

How to encrapt Qery string

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
If Session("SessionUserName") IsNot Nothing Then
strUserId = Session("SessionUserName").ToString()
Dim strUid = b64encode(strUserId)
lnk1.NavigateUrl = "ShowHoroscope.aspx?UI=" + strUid + "&Id=1"
End If

End If

End Sub



Public Shared Function b64encode(ByVal StrEncode As String) As String

Dim encodedString As String

encodedString = (Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(StrEncode)))

Return (encodedString)

End Function

Public Shared Function b64decode(ByVal StrDecode As String) As String

Dim decodedString As String

decodedString = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(StrDecode))

Return decodedString

End Function

Tuesday, May 25, 2010

How to create dynamic control (button)

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

public partial class ashtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button myButton = null;
for (int i = 0; i < 5; i++)
{
myButton = new Button();
myButton.ID = "myButton" + i;
myButton.Text = "myButton" + i;
myButton.Click += new System.EventHandler(this.myButtons_Click);
pnlButton.Controls.Add(myButton);
}
}

protected void myButtons_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
Response.Write("Button id " + btn.ID + " clicked");
}
}
}

Tuesday, March 30, 2010

Get Nth Max Salary

Data Base used for the query processing :-

Table Name Employee
pkid Name Salary
1 apekshit 5000
2 gaurav 6000
3 sunil 7000
4 rajnish 6500
5 amit 8000

For finding Nth highest salary from a table without using TOP, GROUP BY AND ROW COUNT or any other SQL Keywords,

There are two options for this :-

1. OPTION

SELECT TOP 1 SALARY FROM (SELECT DISTINCT TOP N SALARY
FROM EMPLOYEE ORDER BY SALARY DESC)a
ORDER BY SALARY



2. OPTION

SELECT MAX(SALARY) FROM EMPLOYEE as E1
WHERE N=(SELECT COUNT(DISTINCT SALARY)
FROM EMPLOYEE E2 WHERE E1.SALARY<=E2.SALARY)

PASS THE NUMBER N VALUE...

Tuesday, March 23, 2010

Find Active IP Addresses Only with Mac Address

private void FillGrid()
{
Process netsend = new Process();
netsend.StartInfo.FileName = "net.exe";
netsend.StartInfo.CreateNoWindow = true;
netsend.StartInfo.Arguments = "view";
netsend.StartInfo.RedirectStandardOutput = true;
netsend.StartInfo.UseShellExecute = false;
netsend.StartInfo.RedirectStandardError = true;
netsend.Start();

StreamReader sr = new StreamReader(netsend.StandardOutput.BaseStream, netsend.StandardOutput.CurrentEncoding);

DataTable myDataTable = new DataTable();

DataColumn myDataColumn;
DataRow myDataRow;



myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "ServerName";
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "IpAddress";
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "MacAddress";
myDataTable.Columns.Add(myDataColumn);

ArrayList Name = new ArrayList();
ArrayList IpAddress = new ArrayList();

int i = 0;
string sss = "";
try
{
while ((sss = sr.ReadLine()) != null)
{
if (sss.StartsWith("\\"))
{
myDataRow = myDataTable.NewRow();
Name.Add(sss.Substring(2).Substring(0, sss.Substring(2).IndexOf(" ")).ToUpper());
if (Name[i].ToString() != "PS1")
{
myDataRow["ServerName"] = Name[i].ToString();
IPHostEntry myIP = System.Net.Dns.GetHostByName(Name[i].ToString());
IPAddress[] addr = myIP.AddressList;
myDataRow["IpAddress"] = addr[0].ToString();
myDataRow["MacAddress"] = GetMacAddress(addr[0].ToString());
myDataTable.Rows.Add(myDataRow);
i++;
}
}
}
dataGrid1.DataSource = myDataTable;

}
catch { }
}

Find All Ip Addresses on Lan

private void button1_Click(object sender, EventArgs e)
{
DirectoryEntry de = new DirectoryEntry();
ArrayList computer_name = new ArrayList();

de.Path = "WinNT://" + "yoursite.com";
foreach (DirectoryEntry d in de.Children)
{
if (d.SchemaClassName == "Computer")
{
computer_name.Add(d.Name);
}
}

foreach (string cn in computer_name)
{
GetAllIP(cn);

}
}


public void GetAllIP(string args)
{
try
{
string strHostName = "";
ArrayList ipaddr = new ArrayList();
ArrayList hostname = new ArrayList();

IPHostEntry ipEntry = Dns.GetHostByName(args);

IPAddress[] addr = ipEntry.AddressList;

int i = 0;

listBox1.Items.Add(addr[i].ToString());
listBox2.Items.Add(args);

}
catch { }
}

Wednesday, March 10, 2010

Remove Duplicate Records From Table With Sql Server 2005

--FOR CREATING TABLE USE THIS QUERY

CREATE TABLE Duplicate(
ID INT,
FNAME VARCHAR(10),
MNAME VARCHAR(10)
)

--FOR INSERT VALUES IN CREATED TABLE USE THIS QUERY

INSERT INTO Duplicate VALUES(1, 'AAA','CCC')
INSERT INTO Duplicate VALUES(2, 'BBB','DDD')
INSERT INTO Duplicate VALUES(1, 'AAA','CCC')
INSERT INTO Duplicate VALUES(2, 'BBB','DDD')
INSERT INTO Duplicate VALUES(1, 'AAA','CCC')
INSERT INTO Duplicate VALUES(2, 'BBB','DDD')
INSERT INTO Duplicate VALUES(3, 'BCB','DGD')

--FOR DISPLAY ALL RECORDS USE THIS QUERY

SELECT * FROM Duplicate

--FOR SELECT DUPLICATE DATA USE THIS QUERY

WITH CTE as(
SELECT ROW_NUMBER() OVER(PARTITION BY ID, FName, Mname ORDER BY (SELECT 1)) AS RowID,
*
FROM Duplicate
)
DELETE FROM CTE WHERE RowID > 1

--FOR DELETE DUPLICATE DATE USE THIS QUERY

WITH CTE as(
SELECT ROW_NUMBER() OVER(PARTITION BY ID, FName, MName ORDER BY (SELECT 1)) AS RowID,
*
FROM Duplicate
)
SELECT ID, FName, MName
FROM CTE
WHERE RowID > 1

Thursday, March 4, 2010

How to add click event for dynamically generated button

Hi,

In this article iam going to discuss about adding onclick

event to a dynamically generated button.See the code below

TextBox txt = new TextBox();
Button btn = new Button();
Label lbl = new Label();
protected void Page_Load(object sender, EventArgs e)
{
lbl.Text = "Enter Text : ";
lbl.Font.Bold = true;

this.Form.Controls.Add(lbl);
this.Form.Controls.Add(txt);
btn.Text = "btnsave";
btn.Attributes.Add("runat", "server");
this.Form.Controls.Add(btn);
btn.Click += new System.EventHandler(this.Button_Click);


}

protected void Button_Click(object sender, EventArgs e)
{
Response.Write(txt.Text);

}

Thanks & Regards
Umar Daraj

Microsoft .NET Framework 4.0

Microsoft .NET Framework

The Microsoft .NET Framework is a software framework that can be installed on computers running Microsoft Windows operating systems. It includes a large library of coded solutions to common programming problems and a virtual machine that manages the execution of programs written specifically for the framework. The .NET Framework is a Microsoft offering and is intended to be used by most new applications created for the Windows platform.

In this articles we talk about Microsoft .NET Framework 4.0

What’s new in .NET Framework 4.0?

1. Next versions of Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) will provide better support for Web 2.0 technologies like REST, POX, and ATOM.

2. Performance and Scalability of WCF and WF are expected to increase by minimum 10X.

3. New workflow models.

4. Seamless integration between WCF and WF including a new Visual Designer.

5. Parallel Programming framework using PLINQ, Task Parallel Library and Coordination Data Structures to better utilize power of multi-processor and multi-core machines.

6. Build declarative applications with WF, WCF and WPF using XAML. So, XAML is no more only for WPF and WF.

7. WCF enhancements:

1. RESTful enhancements

1. Simplifying the building of REST Singleton & Collection Services, ATOM Feed and Publishing Protocol Services, and HTTP Plain XML Services using WCF

2. WCF REST Starter Kit to be released on Codeplex to get early feedback

2. Messaging enhancements

1. Transports - UDP, MQ, Local in-process

2. Protocols - SOAP over UDP, WS-Discovery, WS-Business Activity, WS-I BP 1.2

3. Duplex durable messaging

3. Correlation enhancements

1. Content and context driven, One-way support

4. Declarative Workflow Services

1. Seamless integration between WF and WCF and unified XAML model

2. Build entire application in XAML, from presentation to data to services to workflow

8. WF enhancements:

1. Significant improvements in performance and scalability

1. Ten-fold improvement in performance

2. New workflow flow-control models and pre-built activities

1. Flowcharts, rules

2. Expanded built-in activities – Power Shell, database, messaging, etc.

3. Enhancements in workflow modeling

1. Persistence control, transaction flow, compensation support, data binding and scoping

2. Rules composable and seamlessly integrated with workflow engine

4. Updated visual designer

1. Easier to use by end-users

2. Easier to rehost by ISVs

3. Ability to debug XAML

Thanks & Regards
Umar Daraj

Wednesday, March 3, 2010

Mvc architecture

Model View Controller

Model view controller is nothing but a design pattern used to achieve customizability in our application. Change is the only thing in the world, which will never change. All the products that we develop to our clients will undergo many changes. To accommodate these changes we should concentrate more on our design. Directly jumping in to the code may give quick solutions to our problem but they will not solve our future problems of customizability and re-usability. So friends in this article we will discuss about MVC a most popular design pattern, which helps us to overcome most of our problems. Initially we may feel that this design pattern will need more time before starting the development. Yes it is true, but the time, which we spend on design, will give some fruitful benefits.

Good Layering Approach

MVC follows the most common approach of Layering. Layering is nothing but a logical split up of our code in to functions in different classes. This approach is well known and most accepted approach. The main advantage in this approach is re-usability of code. Good example of layering approach is UI, Business Logic, and Data Access layer. Now we need to think how we can extend this approach to give us another great advantage customizability. The answer to this is using Inheritance. Inheritance is one of the powerful concepts in oops. .Net supports this in a nice way.

Visual Inheritance in .Net

Before getting in to Visual Inheritance, we will discuss the basic difference between Inheritance and Interfaces.

Inheritance -- what it is?
Interfaces -- How it should be? (It’s a contract)

Both these answers target the classes, which we are designing in our application. Many of us know this definition but we will not use this extensively in our application including me. Unless we work in a design, which uses these concepts to a great extent, we will not agree this. Many of the Microsoft products use this to a great extent. For example how Commerce Server pipeline components achieves the customizability. Commerce Server allows us to create our own component in com and allow us to add it as one of a stage in the pipeline. How they achieve this, only through interfaces. We need to create a com component and the com component should implement a interface. All the objects in our .Net Framework class libraries are inherited from System.Object. This tells us the importance of Inheritance.

Jumping into Visual Inheritance

Let's try out one of .Net most interesting feature, Visual Inheritance. What exactly is it? Now we do know what is inheritance right? No I don't mean the money that you get from your parents/grand parents, I'm talking about class and interface inheritance, which saves you the headache of re-typing code and provides the luxury of code reusability. Now wouldn't it be nice if the same feature that applies to classes and their methods could also be applied to the GUI forms that we create, I mean create a base form with our corporate logo so that it appears on all of the firm's screens? Well such a feature now exists! Yes, you've guessed it, its Visual Inheritance! Visual Inheritance alone will not solve our problem of extensibility. This will help us to start digging our mind for solution. Cool guys now we will directly jump in to our problem with a good example.

Problem Statement

I have an employee master screen, which takes Name and Age as inputs and save the same in our database.



Lets assume this is my basic functionality in my product, which serves most of my customer’s requirement. Now let us assume one of my new client asks for a change in this form. He needs an additional field in this form, which takes the employee’s address and stores the same in the database. How will we achieve this? Normally we will create a new screen which will also has a textbox to get the address input, in the same time we will also add another column in the employee master table and supply the new binaries and modified table and stored procedure scripts to the client. Do you think this is the right approach? If you ask me this is one of the crude way to satisfy an customer. Requirement like this will be very easy to re-write the entire code. But think if a customer asks a change in Payroll Calculation logic. Re-writing code for some specific customers will not helps us in long run. This will end up in maintaining separate – separate Visual source safe for each and every client. These types of solutions are very difficult to handle and after some time we will feel that we have a messy code.

The right way to solve this problem is by having a good design pattern in place and make sure that the entire team clearly understands the design and implements the same in their code. We can solve this by doing a layered approach using MVC and Visual Inheritance.

Solution to our problem

1. Don’t alter the table add another table which stores the additional columns like address and the junction/extended table should have a foreign key relationship with our main employee table.
2. Create a new Inherited form, which inherits from our main employee master screen. To use Visual Inheritance we need to change the Access Modifiers to Protected from Friend. By default VS .Net puts the access modifier as friend.

View Layer

Our View /UI layer should only have UI related validations in it. We should not have any Business Logic in to it. This gives us the flexibility to change the UI at any time and we can also have different UI for different customers. We can even have web based UI for some of the clients.

Controller /Director

Controller is the layer, which responds to the events in the UI. For example Save button click on my employee master screen. This layer should act as an intermediate between our View and Model. Initially we may think that this layer is not necessary. I am also not convinced with this layer. But still we need to think. May be after some days we will get answer for this. This layer will act as just event redirector.

Model

This layer has all our business logic. This is the most important layer. This layer will have our core functionality. This layer should be designed in such a way that out core and complex logic should be designed as functions and these function should be marked as overridable so that the inheriting classes can re-use the logic or override the logic. We should not make all the functions in the layer as overridable this may raise some security threats.

Database Operations

All the database operations should be done in the Model base class. All the inheriting classes should call this method to do database updates. The design can be like this. In my EmpModel base class I will have a protected array list which will store all the objects which needs to get updated. All the classes which inherits this class should add there objects to this array list and they should call the base class Update method. This helps us to do all the db operations in a single transaction.

For example in our example, we should create an EmployeeBase class, which will have properties for name and age. Our EmpModelBase should have a new instantiated EmployeeBase object and our view should fill the object properties. Finally the view will call the controller’s Save method and the controller should call the Model save method, there we will add the Employee object to the array list and the function should call the mybase.Update method. This method should loop through the array list and it should fire corresponding db update statements. This is just an example. We need to enhance this depending upon our requirement.

Conclusion

1. Layering Approach helps us a lot and we need to enhance it to get full customizability.
2. We need to enhance this with all our design knowledge.
3. No IDE enforces these patterns, it is up to us to do clean and disciplined way of coding.
4. Once we are used to these approaches / patterns then we are addicted to it.


Thanks & Regards
Umar Daraj