Tuesday, 4 October 2011

Get IP to Country ,State , city info in asp.net ,C#



First register on this site http://api.ip2locationapi.com/ to get API Key

protected void GetUserLocation()
{
try
{
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == null)
{
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
}
DataTable dt = UserManage.GetLocationnew(ipaddress);
if(dt.Rows.Count > 0)
{
Location = dt.Rows[0]["countryName"].ToString();
CountryID = Common.GetCountryID(Location);
StateID = Common.GetStateID(dt.Rows[0]["region"].ToString());
}
}
catch (Exception ex)
{
AppBase.handleException("Form:changevote, Function:GetUserLocation", ex);
}

}

public static DataTableGetLocationnew(string ipaddress)
{
DataSet ds = new DataSet();
try
{
DataTable dt = new DataTable();
WebRequest wreq = WebRequest.Create("http://api.ip2locationapi.com/?user= &format=xml&ip=" + ipaddress + "");
WebResponse wres = wreq.GetResponse();
XmlTextReader xmlRead = new XmlTextReader(wres.GetResponseStream());
ds.ReadXml(xmlRead);
}
catch (Exception ex)
{
AppBase.handleException("Form:_Default, Function:GetLocation", ex);
}
returnds.Tables[0];
}


Tuesday, 27 September 2011

Read Pdf form field and convert to excel using asp.net ,C#

convert Pdf  form field and to excel file using asp.net ,C#


Download itextsharp.dll file


 

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Reflection;
using Acrobat;
usingSystem.Collections;
usingiTextSharp.text.pdf;
using System.IO;
usingSystem.Data.SqlClient;

private void button3_Click(objectsender, EventArgs e)
{

stringpdfTemplate = "C:/Users/yudi/Desktop/pdftoexcelsunil.pdf";
PdfReaderpdfReader = new PdfReader(pdfTemplate);
AcroFieldsfields = pdfReader.AcroFields;
string fval = fields.GetField("First name");
string lval = fields.GetField("last name");

string address = fields.GetField("address");
string city = fields.GetField("city");

string state = fields.GetField("state");
string email = fields.GetField("email");

SqlConnectioncon = new SqlConnection("Data Source=.;Initial Catalog=gltest;User ID=sunil;Password=sunil");
con.Open();
SqlCommandcmd = new SqlCommand("insert into pdftable(firstname,lastname,address,city,state,email) values('"+fval+"','"+lval+"','"+address+"','"+city+"','"+state+"','"+email+"') ",con);

cmd.ExecuteNonQuery();
con.Close();
int coun=   pdfReader.AcroForm.Fields.Count;

//PdfDocument pdfdoc = new PdfDocument();
byte[] bytes = System.IO.File.ReadAllBytes("C:/Users/yudi/Desktop/pdftoexcelsunil.pdf");

System.IO.File.WriteAllBytes("C:/Users/yudi/Desktop/newpdftoexcelsunil.xls", bytes);

save("newpdftoexcelsunil");


}
public void save(stringfilename)
{
SqlConnectioncon = new SqlConnection("Data Source=.;Initial Catalog=gltest;User ID=sunil;Password=sunil");
con.Open();
//try
{
System.Windows.Forms.SaveFileDialogopendlg = new System.Windows.Forms.SaveFileDialog();
opendlg.FileName = filename;
opendlg.Filter = "Excel Spreadsheets (*.xls)|*.xls";
opendlg.DefaultExt = "xls";
if(opendlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StringBuildersbrHTML = new StringBuilder();
SqlCommand dc = new SqlCommand("select * from pdftable", con);
SqlDataReaderdr;
dr = dc.ExecuteReader();
//Making HTML
sbrHTML.Append("
");
sbrHTML.Append("
");
sbrHTML.Append("
");
for (int i = 0; i < dr.FieldCount; i++)
{
sbrHTML.Append("
");
}
sbrHTML.Append("
");
while(dr.Read())
{
sbrHTML.Append("
");
for (int i = 0; i < dr.FieldCount; i++)
{
sbrHTML.Append("
");
}
sbrHTML.Append("
");
}

dr.Close();
sbrHTML.Append("
sql data
" + dr.GetName(i) + "
" + good_value(dr.GetValue(i).ToString()) + "
");
sbrHTML.Append("

Reported by sunil"
);
sbrHTML.Append("

Date: "
+ System.DateTime.Now + ""
);

//ENDOF MAKING HTML
//WRITING AS AN XSL
StreamWriterswXLS = new StreamWriter(opendlg.FileName);
swXLS.Write(sbrHTML.ToString());
swXLS.Close();
con.Close();
}
}


}
private string good_value(stringinput)
{
return ((input == "" || (input[0] >= '0' && input[0] <= '9')) ? "'": "") + input;
}

Tuesday, 21 June 2011

as keyword in c sharp


as 

The as operator is used to perform certain types of conversions between compatible reference types . The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception

Example


class ClassA { }
class ClassB { }
 
class MainClass
{
    static void Main()
    {
        object[] objArray = new object[6];
        objArray[0] = new ClassA();
        objArray[1] = new ClassB();
        objArray[2] = "hello";
        objArray[3] = 123;
        objArray[4] = 123.4;
        objArray[5] = null;
 
        for (int i = 0; i < objArray.Length; ++i)
        {
            string s = objArray[i] as string;
            Console.Write("{0}:", i);
            if (s != null)
            {
                Console.WriteLine("'" + s + "'");
            }
            else
            {
                Console.WriteLine("not a string");
            }
        }
    }
}
/*
Output:
0:not a string
1:not a string
2:'hello'
3:not a string
4:not a string
5:not a string
*/
 
 

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger