Showing posts with label Sample. Show all posts
Showing posts with label Sample. Show all posts

Sunday, June 19, 2011

Sample to find Area and perimeter of a Polygon

Hi,
Today tried a sample to fetch Area and perimeter of a surface using ArcGIS API with Google maps. It is easy to use, we just need to join the points for the respective surface for which we need to find the Area & perimeter and double click. I always like Central park in Newyork, Thought let us try the tool there... It works perfect... please find the link & screen shot below.

Live Sample: http://www.kuwaitfresh.com/find_area.html

Monday, November 8, 2010

Sample to Connect DB2 through DOTNET

Dear Reader,
Here is a sample to connect DB2 through .Net Applicaiton, Create for a friend, thought this must be useful for people looking for it.

Code:
------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data;

namespace DB2_ODBC_Sample
{
class Program
{
static void Main(string[] args)
{
try
{
OdbcCommand cmd = null;
OdbcConnection con = null;
OdbcDataReader myReader = null;

con = new OdbcConnection("DSN=[YOURDSN];UID=[YOUR USERNAME];PWD=[YOUR PASSWORD];" + "Driver={IBM DB2 ODBC DRIVER};");
cmd = new OdbcCommand();
cmd.Connection = con;

cmd.CommandText = "SELECT * FROM SOMETABLE fetch first 5 rows only";

cmd.CommandTimeout = 0;
con.Open();
myReader = cmd.ExecuteReader(System.Data.CommandBehavior.KeyInfo);
aawservice objaawservice = new aawservice();
DataSet objDataSet = objaawservice.ConvertDataReaderToDataSet(myReader);
con.Close();
for (int i = 0; i < objDataSet.Tables[0].Rows.Count; i++)
{
Console.WriteLine("\n");
Console.WriteLine(objDataSet.Tables[0].Rows[i][2].ToString());

}
Console.Write("\nPress [Enter] to Exit ... ");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

}
class aawservice
{
public DataSet ConvertDataReaderToDataSet(System.Data.Odbc.OdbcDataReader reader)
{
DataSet dataSet = new DataSet();
do
{
// Create data table in runtime
DataTable schemaTable = reader.GetSchemaTable();
DataTable dataTable = new DataTable();

if (schemaTable != null)
{
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
DataRow dataRow = schemaTable.Rows[i];

// Create a column name as provided in Schema
string columnName = (string)dataRow["ColumnName"];

// Define Column Type here
DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);

//Adding Column to table
dataTable.Columns.Add(column);
}

dataSet.Tables.Add(dataTable);

// Fill the data table from reader data

while (reader.Read())
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < reader.FieldCount; i++)
dataRow[i] = reader.GetValue(i);
dataTable.Rows.Add(dataRow);
}
}

else
{
// No records were returned
DataColumn column = new DataColumn("RowsAffected");
dataTable.Columns.Add(column);
dataSet.Tables.Add(dataTable);
DataRow dataRow = dataTable.NewRow();
dataRow[0] = reader.RecordsAffected;
dataTable.Rows.Add(dataRow);
}
}
while (reader.NextResult());
return dataSet;
}
}

Monday, November 9, 2009

Sample to display Image on mouse hover.

Created the following sample for one of a small web requirement.

Hosting it since it could be useful for some one who is looking for it...

Sample Link: http://checkthiz.com/publicfiles/ImageHover.html

Hope it helps

Fauzi ~ 4Z

Monday, October 5, 2009

How to embed image in C# Email

Hi...

I had an interesting requirement to generate daily reports based on the accumulated sales data. And this Email will be sent to the respective Managers. Thought it would give a better picture if the email have the appropriate Brand images embedded on necessary places in report.

Following code was very much useful to embed an image in a system generated e-mail through C#.

Code:

Table objTable = new Table();

objTable.BorderStyle = BorderStyle.Solid;

objTable.BorderWidth = 1;

objTable.GridLines = GridLines.Both;

objTable.CellPadding = 5;

objTable.CellSpacing = 1;

objTable.BorderColor = System.Drawing.ColorTranslator.FromHtml("#035681");

objTable.Style.Add("Font-family", "Verdana");

objTable.Style.Add("Font-size", "14");

//Welcome TABLE CODE:

TableRow objTableRow_Welcome = new TableRow();

TableCell objTableCell_welcome1 = new TableCell();

objTableCell_welcome1.ColumnSpan = 1;

// Embeddind Image ID which is created below.

objTableCell_welcome1.Text = "<img src=\"cid:SampleImage\">";

objTableRow_Welcome.Cells.Add(objTableCell_welcome1);

objTable.Rows.Add(objTableRow_Welcome);

SmtpClient smtp = new SmtpClient("YOUR SMTP CLIENT");

MailAddress sender = new MailAddress(" FAUZI @ CHENNAI.com", "Fauzi");

MailAddress recipient = new MailAddress(" FAUZI @ INDIA.com", "Recipient");

MailMessage m = new MailMessage(sender, recipient);

m.Subject = "Embed image with C# mail";

// Define the plain text alternate view and add to message

string plainTextBody = "Best viewed in Email client(Outlook) that supports HTML messages";

AlternateView plainTextView =

AlternateView.CreateAlternateViewFromString(

plainTextBody, null, MediaTypeNames.Text.Plain);

m.AlternateViews.Add(plainTextView);

//Rendering Text from Created HTML TABLE in runtime

StringBuilder htmlBody = new StringBuilder();

StringWriter objStringWriter = new StringWriter(htmlBody);

HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);

objTable.RenderControl(objHtmlTextWriter);

//Creating an Alternate view

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody.ToString(), null, MediaTypeNames.Text.Html);

// Embedding Image here

LinkedResource sampleImage = new LinkedResource("logo.gif", MediaTypeNames.Image.Jpeg);

sampleImage.ContentId = "SampleImage";

htmlView.LinkedResources.Add(sampleImage);

//Adding the created Alternate view

m.AlternateViews.Add(htmlView);

// Finally, configure smtp or alternatively use the

// system.net mailSettings

smtp.Send(m);


Hope it helps...

Please remember to give appropriate path of the image file.


Best Regards

Fauzi


Wednesday, August 19, 2009

Sample to connnect DB2

Hi...

So far we have written applications to talk to various Databases. This is the first time, wrote a C# .Net console application to fetch records from DB2. Following is the sample code.

Code:

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.Odbc;

class Program

{

static void Main()

{

OdbcCommand cmd = null;

OdbcConnection con = null;

OdbcDataReader rdr = null;

try

{

con = new OdbcConnection("DSN={LibName};UID={User Name};PWD={Password};" + "Driver={IBM DB2 ODBC DRIVER};");

cmd = new OdbcCommand();

cmd.Connection = con;

cmd.CommandText = "SELECT * FROM SOMETABLE";

cmd.comCommandTimeout = 20;

con.Open();

rdr = cmd.ExecuteReader(System.Data.CommandBehavior.SingleResult);

int intCount = 0;

while (rdr.Read())

{

intCount++;

Console.WriteLine(rdr.GetString(2).ToString() + ", " + rdr.GetString(3) + " ->" + intCount.ToString());

}

con.Close();

Console.ReadLine();

}

catch (OdbcException myException)

{

}

}

}


Regards
Fauzi

Tuesday, July 14, 2009

Read MS Access through .Net

Hi,

I had a requirement to create a application to retrieve data from legacy MS-Access application.
I was looking for a sample in google. could'nt find one. So posting it so that it could be useful for people who is looking for the same.

I have created a DNS for the mdb file. used System.Data.Odbc; and rest are similar to how we do for SQL. following is the sample code.

static void Main(string[] args)

{

//Read Data from Access

OdbcConnection ODBCConnection = new System.Data.Odbc.OdbcConnection("DSN=DNS_Test_MSACCESS;UID=sam;PWD=mam;");

// ODBCConnection= new OdbcConnection(ConnString);

ODBCConnection.Open();

OdbcCommand ODBCCommand = new OdbcCommand("select * from Table1", ODBCConnection);

OdbcDataReader ODBCDataReader = ODBCCommand.ExecuteReader();

string s = "";

while (ODBCDataReader.Read())

{

s = s + "- " + ODBCDataReader.GetString(1);

}

Console.WriteLine(s);

ODBCConnection.Close();

}


Sunday, June 14, 2009

MOSS: Free Master pages templates for SharePoint

Hi

You can download free Master page samples for SharePoint from Microsoft website.

Click to view the download page

The Link also has the instructions to setup.

Regards
Fauzi

Wednesday, June 3, 2009

Sample MOSS webpart & deployment

Hi Folks...

Seems people search for Sample MOSS webpart and land into my previous blog where i have written how to develop a webpart from your XP machine. So let us provide what people came looking for....

Following is a sample web part i created some time back which fetches data from database and displays in a table. The web part class should always inherit Webpart(System.Web.UI.WebControls.WebParts) as base class. For webparts we usually write the code to override any events like Render, RenderWebPart or RenderContent.
In the sample code i have overrided Render event which takes the parameter System.Web.UI.HtmlTextWriter object.

The Database operations i have used Application blocks from Enterprise Library

Namespace:

System.Web.UI.WebControls
;
System.Web.UI.WebControls.WebParts;

Code: There are appropriate comments give to explain its operation

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web;

using System.Data;

using System.Data.Sql;

using System.Data.SqlClient;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.ApplicationBlocks.Data;

namespace Newwebpart1

{

public class Newwebpart1 : WebPart

{

protected override void CreateChildControls()

{

base.CreateChildControls();

//Control can be created here...

}

protected override void Render(System.Web.UI.HtmlTextWriter writer)

{

//We Create a SQL connection

SqlConnection objSqlConnection = new SqlConnection("Data Source=44.44.44.44;Initial Catalog=<YOUDB>;Persist Security Info=True;User ID=<USERNAME>;Password=<PASSWORD>");

//We execute a SQL Query & get a Dataset in return

DataSet objDataSet = SqlHelper.ExecuteDataset(objSqlConnection, System.Data.CommandType.Text, "SELECT [COL1],[COL2] FROM [DB].[dbo].[TABLENAME]");

//We are creating a Table in runtime

Table objTable = new Table();

objTable.BorderStyle = BorderStyle.Solid;

objTable.BorderWidth = 1;

objTable.GridLines = GridLines.Both;

objTable.CellPadding = 1;

objTable.CellSpacing = 1;

objTable.BorderColor = System.Drawing.Color.Blue;

//Creating Header here:

TableRow objTableRow_Header = new TableRow();

TableCell objTableCell_Title1 = new TableCell();

objTableCell_Title1.Text = objDataSet.Tables[0].Columns[0].Caption;

TableCell objTableCell_Title2 = new TableCell();

objTableCell_Title2.Text = objDataSet.Tables[0].Columns[1].Caption;

objTableRow_Header.Cells.Add(objTableCell_Title1);

objTableRow_Header.Cells.Add(objTableCell_Title2);

objTable.Rows.Add(objTableRow_Header);

//Rows are added based on the number of records

for (int i = 0; i < objDataSet.Tables[0].Rows.Count; i++)

{

TableRow objTableRow_Content = new TableRow();

TableCell objTableCell_Column1 = new TableCell();

objTableCell_Column1.Text = objDataSet.Tables[0].Rows[i][0].ToString();

TableCell objTableCell_Column2 = new TableCell();

objTableCell_Column2.Text = objDataSet.Tables[0].Rows[i][1].ToString();

objTableRow_Content.Cells.Add(objTableCell_Column1);

objTableRow_Content.Cells.Add(objTableCell_Column2);

objTable.Rows.Add(objTableRow_Content);

}

//Writing content inside webpart with the help of object created for

//HtmlTextWriter

writer.Write("<div style='overflow:auto'>");

//Add the table which was created in runtime from the above code.

objTable.RenderControl(writer);

writer.Write("</div>");

}

}

}


Reference

Download Project: http://www.checkthiz.com/publicfiles/Download webpart.zip

Steps to deploy a webpart:

1. Right click on project file and select properties and strong name key for the Project.


2. Build the solution (Ctrl+ Shit +B)

3. The Assembly (dll) will be created in the bin folder.

4. Move the assembly to the GAC ( just drag & drop in C:\WINDOWS\assembly)

5. In the web.config of the Web application (Sharepoint Website) place the code in SafeControls tag with the PublicKeyToken found in the Assembly.

Sample:
SafeControl Assembly=", Version=1.0.0.0, Culture=neutral, PublicKeyToken=ASDFASDRTERFSDF" Namespace="" TypeName="*" Safe="True"

6. Reset IIS

7. Go to Site Action -> Settings -> Webparts -> New

8. You will find your webpart in the URL: http://YOURMOSSSERVER/_layouts/NewDwp.aspx .Select the checkbox for the respective webpart & click Populate Gallery Button.

9. Now go to any page in the site and in Edit mode you will be able to find the webpart to add it in your page.... Thats it...

Note: With the help of Web Part templates in visual studio the above steps are made much more easier! you can just click on RUN button. The studio it-self will deploy & reset the IIS, you can directly go to the page and add the Web part! Love you Templates :)

Hope it helps... try and have fun :)

Regards
Fauzi ~4Z