Thursday, February 9, 2012

Auto generated Numbers in SQL Query

Auto generated Numbers in SQL Query

create table CountryMaster
(
id int identity(1,1),
cityname varchar(50),
countryname varchar(50)
)

select * from CountryMaster

insert into CountryMaster values('New Delhi','India')
insert into CountryMaster values('Mumbai','India')
insert into CountryMaster values('Kolkata','India')
insert into CountryMaster values('Chennai','India')

insert into CountryMaster values('London','UK')
insert into CountryMaster values('Amsterdam','UK')
insert into CountryMaster values('Southampton','UK')

insert into CountryMaster values('Washington','US')
insert into CountryMaster values('New York','US')
insert into CountryMaster values('Chicago','US')

insert into CountryMaster values('Canberra','Australia')
insert into CountryMaster values('NSW','Australia')
insert into CountryMaster values('Melbourn','Australia')

insert into CountryMaster values('Islamabad','Pakistan')
insert into CountryMaster values('Karachi','Pakistan')
insert into CountryMaster values('Hyderabad','Pakistan')

select id, cityname, countryname,
ROW_NUMBER() OVER (PARTITION BY [countryName] ORDER BY id) AS Seq
from CountryMaster


Result
-------
.............................................
id cityname countryname Seq
.............................................
27 Canberra Australia 1
28 NSW Australia 2
29 Melbourn Australia 3

17 New Delhi India 1
18 Mumbai India 2
19 Kolkata India 3
20 Chennai India 4

30 Islamabad Pakistan 1
31 Karachi Pakistan 2
32 Hyderabad Pakistan 3

21 London UK 1
22 Amsterdam UK 2
23 Southampton UK 3

24 Washington US 1
25 New York US 2
26 Chicago US 3
.............................................

select id, cityname, countryname,
ROW_NUMBER() OVER (ORDER BY id) AS Seq
from CountryMaster

Result
-------
.............................................
id cityname countryname Seq
.............................................
17 New Delhi India 1
18 Mumbai India 2
19 Kolkata India 3
20 Chennai India 4
21 London UK 5
22 Amsterdam UK 6
23 Southampton UK 7
24 Washington US 8
25 New York US 9
26 Chicago US 10
27 Canberra Australia 11
28 NSW Australia 12
29 Melbourn Australia 13
30 Islamabad Pakistan 14
31 Karachi Pakistan 15
32 Hyderabad Pakistan 16
.............................................


select id, cityname, countryname,
ROW_NUMBER() OVER(order by id) AS Seq
from CountryMaster order by cityname

List of few CMMi level 5 companies in HYD, BLR, Pune

---
HYD
---
TCS
WIPRO
MAHINDRA SATYAM
INFOSYS
HCL
COGNIZANT
UHG
WELLSFARGO FINANCE COMPANY
SIERRA ATLANTIC
INTELLIGROUP
SEMANTICSPACE
IGATE
KEANE
INFOTECH

DELL
ORACLE
ADP
CGI
DELOITTE
VIRTUSA
CSC
CONVERGYS
ACCENTURE
CAPGEMNI
POLARIS
SONATA
JDA
NCR
KENEXA

----
BLR
----
ITC INFOTECH
TESCO
HP
MPHASIS
L & T INFOTECH
COVANSYS
INTEL
ABB
MCAFEE
FIC
SASKEN
LOGICA
SAPIENT
THOMSON REUTERS
PHILIPS

IBM
HONEYWELL
PEROT SYSTEMS
MINDTREE
JP MORGAN

-----
PUNE
-----
TECH MAHINDRA
SYNTEL
BLUE STAR INFOTECH
MASTEK
HEXAWARE
3I INFOTECH (ICICI)

Google maps - code to display image from URL

protected void Page_Load(object sender, EventArgs e)

{

if (!(Page.IsPostBack))

{

// to get the image stream

//http://www.greywyvern.com/code/php/binary2base64

// google maps image

// http://code.google.com/apis/maps/documentation/staticmaps/

//// working url

////string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=0&size=320x169&maptype=roadmap";

///////// ++++++++++++++++++++++++++++++++++++++

// working

//string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=12&size=480x313&maptype=roadmap¢er=52.4871716,1.7153936&markers=52.4871716,1.7153936";

// not working

//string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=12&size=480x313&maptype=roadmap¢er=51.5017821,-0.1154330&markers=51.5017821,-0.1154330";

// old

//string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=0&size=320x169&maptype=roadmap";

string GoogleURL = "http://maps.google.com/maps/api/staticmap?sensor=false&zoom=12&size=480x313&maptype=roadmap¢er=-33.8389280,151.2085890&markers=-33.8389280,151.2085890&format=jpg";


/////////////////////////////////////string GoogleURL1 = HttpUtility.UrlDecode("http%3a%2f%2fmaps.google.com%2fmaps%2fapi%2fstaticmap%3fsensor%3dfalse%26zoom%3d0%26size%3d320x169%26maptype%3droadmap");

string strResponse = string.Empty;


WebRequest objWebRequest;

objWebRequest = WebRequest.Create(GoogleURL);

WebProxy proxy = new WebProxy();

objWebRequest.Proxy = proxy;

objWebRequest.UseDefaultCredentials = true;

objWebRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

objWebRequest.ContentType = "Content-Type: image/png";

StreamReader loResponseStream = new StreamReader(objWebRequest.GetResponse().GetResponseStream(), Encoding.UTF8);

var bytes = default(byte[]);

using (var memstream = new MemoryStream())

{

var buffer = new byte[512];

var bytesRead = default(int);

while ((bytesRead = loResponseStream.BaseStream.Read(buffer, 0, buffer.Length)) > 0)

{

memstream.Write(buffer, 0, bytesRead);

}

bytes = memstream.ToArray();

}

image1.Attributes["src"] = "data:image/png;base64," + Convert.ToBase64String(bytes);

//Response.Clear();

//Response.ContentType = "Image/png";

//Response.BinaryWrite(bytes);

}

}

LINQ SQL Injection

http://msdn.microsoft.com/en-us/library/bb399403.aspx

http://csharp-codesamples.com/2009/05/preventing-sql-injection-using-linq/

reading data through Excel

private DataTable ReadExcelData(string strFilePath, string strFileType)

{

DataTable dt = new DataTable();

try

{

if (strFileType.ToLower().Contains("xls") || strFileType.ToLower().Contains("xlsx"))

{

OleDbCommand excelCommand = new OleDbCommand();

OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter();

// for office 2003 and prior versions

//////////string excelConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFilePath + "; Extended Properties =Excel 8.0;";

/////// for office 2007 onwards and prior versions

string excelConnStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strFilePath + "; Extended Properties =Excel 12.0;";

// Microsoft.Jet.OLEDB.4.0 driver is only for uploading Microsoft-Excel 2003 and earlier versions.

OleDbConnection excelConn = new OleDbConnection(excelConnStr);

excelConn.Open();

DataTable dtSheetName = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

if (dtSheetName != null && dtSheetName.Rows.Count > 0)

{

string strSheetName = dtSheetName.Rows[0]["TABLE_NAME"].ToString();//gives first sheetname in the excel

excelCommand = new OleDbCommand("SELECT * FROM [" + strSheetName + "]", excelConn);//excelCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConn);

excelDataAdapter.SelectCommand = excelCommand;

excelDataAdapter.Fill(dt);

}

excelConn.Close();

}

}

catch (Exception objEx)

{

throw objEx;

}

finally

{

//Delete the saved file from server path after reading the data from the path.Because it is uploaded from client machine to database.We are saving to server to avoid errors.After process we are deleting here.

if (File.Exists(strFilePath))

File.Delete(strFilePath);

}

return dt;

}

shivprasad koirala URLS on WCF

http://www.codeproject.com/KB/WCF/WCFTransactions.aspx

http://www.codeproject.com/KB/WCF/WCFFAQPart5.aspx

http://www.codeproject.com/KB/aspnet/WCF.aspx

http://www.codeproject.com/KB/aspnet/WCFPart2.aspx

http://www.codeproject.com/KB/WCF/WCFFAQPart3.aspx

http://www.codeproject.com/KB/WCF/WCFTracingFAQ.aspx

sending emails through c# asp.net

web.config entry

<system.net>
<mailSettings>
<smtp from="yoursenderemail@corporate.com">
<network host="hostname" port="25" password="Password"
userName="PASSPORT2" defaultCredentials="false" /> </smtp>
</mailSettings>
</system.net>

Note: 1) Make sure from address domain name
2) IP address, port number should be valid

c# code

SmtpSection mailSettings = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
MailMessage MyMessage = new MailMessage();

string sendTo = this.txtEmailHard.Text;
string sendFrom = mailSettings.From;
string sendMessage = "hello now the time is : " + DateTime.Now.ToLongDateString();
MyMessage.To.Add(sendTo);
MyMessage.From = new MailAddress(sendFrom);
MyMessage.Subject = "Hi Welcome to eProfile";
MyMessage.Body = sendMessage;
MyMessage.IsBodyHtml = true;

this.lblComment.Text = mailSettings.Network.Host + " " + mailSettings.Network.Port;

SmtpClient emailClient = new SmtpClient(mailSettings.Network.Host, mailSettings.Network.Port);
if (mailSettings.Network.DefaultCredentials)
{
emailClient.UseDefaultCredentials = true;
}
else
{
emailClient.Credentials = new System.Net.NetworkCredential(mailSettings.Network.UserName, mailSettings.Network.Password);
}

emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
emailClient.Send(MyMessage);
return;
}
catch (SmtpException smtpx)
{
throw smtpx;
}
catch (Exception ex)
{
throw ex;
}