Tuesday, December 15, 2009

Send sms from asp.net

Hai,

Paste the following link to the add 'webreferance' window.
http://www.webservicex.net/SendSMS.asmx?wsdl

code
try
{
net.webservicex.www.SendSMS smsIndia = new net.webservicex.www.SendSMS();
net.webservicex.www.SMSResult smsre = new net.webservicex.www.SMSResult();

smsre= smsIndia.SendSMSToIndia("+91"+TextBox1.Text, "jineshrev@gmail.com", "Haiiii");

Label1.Text = smsre.Status; //"Message Send Succesfully";
}
catch (Exception ex)
{
Label1.Text = "Error in Sending message" + ex.ToString();
}

details will update later, Try it now...

Friday, December 11, 2009

Importing from excel sheet to program/dataset

Hai,

Try it...

using System.Data.OleDb;

string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:/Book11.xls; Extended Properties=Excel 8.0;";

using (OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open();
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "select * from [Sheet1$]";
//Name of the sheet(sheet1), '$' specify it is an excel sheet. otherwise consider as tablename.
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.Fill(ds);
//show it in a gridview
GridView1.DataSource = ds;
GridView1.DataBind();
Connection.Close();
}
}

Exporting data to excel sheet

Hai,

Make sure that your excel sheet in'.xls' format........

using System.Data.OleDb;

connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:/Book1.xls; Extended Properties=Excel 8.0;";

using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open();
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
try
{
command.CommandText = "drop table Sample1";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Sample1(FirstName Char(255), LastName char(255), Email char(255))";
command.ExecuteNonQuery();
}
catch(Exception ex)
{
command.CommandText = "CREATE TABLE Sample1(FirstName Char(255), LastName char(255), Email char(255))";
command.ExecuteNonQuery();
}
}
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('Anuraj','P','anuraj.p@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('sreekumar','VN','sreekumar.vn@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('jinesh','B','jinesh.b@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('ratheesh','S','ratheesh.s@example.com')";
command.ExecuteNonQuery();
}
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("select * from Sample1", Connection);
da.Fill(ds);


DataGrid dgGrid = new DataGrid(); object missing = Type.Missing;

Microsoft.Office.Interop.Excel.Application oExcel=new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks oBooks = (Microsoft.Office.Interop.Excel.Workbooks)oExcel.Workbooks;//;
Microsoft.Office.Interop.Excel._Workbook oBook = (Microsoft.Office.Interop.Excel._Workbook)(oBooks.Add(missing));

Microsoft.Office.Interop.Excel.Sheets oSheets = (Microsoft.Office.Interop.Excel.Sheets)oBook.Worksheets;//= new Microsoft.Office.Interop.Excel._Worksheet();
Microsoft.Office.Interop.Excel._Worksheet oSheet = (Microsoft.Office.Interop.Excel._Worksheet)(oSheets.get_Item(1));

Microsoft.Office.Interop.Excel.Range oRange = oSheet.get_Range("A1", missing);
oRange.Value2 = "First Name";

oRange = oSheet.get_Range("B1", missing);
oRange.Value2 = "Last Name";

oRange = oSheet.get_Range("C1", missing);
oRange.Value2 = "Email";

oRange = oSheet.get_Range("A1", "C1");
Microsoft.Office.Interop.Excel.Font oFont = oRange.Font;
oFont.Bold = true; int j=1;
for (int i = 0; i < ds.Tables[0].Rows.Count;i++)
{
j++;
oRange = oSheet.get_Range("A"+j, missing);
oRange.Value2 = ds.Tables[0].Rows[i][0];

oRange = oSheet.get_Range("B"+j, missing);
oRange.Value2 = ds.Tables[0].Rows[i][1];

oRange = oSheet.get_Range("C" +j, missing);
oRange.Value2 = ds.Tables[0].Rows[i][2];

}

oBook.SaveAs("D:/Book11.xls", missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);

oBook.Close(false,missing,missing);
oExcel.Quit();
}