Tuesday, January 22, 2008

Send Web Page as Email

//Function which convert URL information as string
//Which is used latter to passed as body message.
private String readHtmlPage(string url)
{
String result;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}


//Code for sending webpage as email.
private void btnSend_Click(object sender, System.EventArgs e)
{
//URL is converted into string message.
String message=readHtmlPage(txtURL.Text);
String mailServer = "192.168.0.10"; //Change with your mail server address.


try
{
MailMessage Mailer = new MailMessage();
Mailer.From = txtFrom.Text;
Mailer.To = txtTo.Text;
Mailer.Subject = txtSubject.Text;
Mailer.Body = message;
Mailer.BodyFormat = System.Web.Mail.MailFormat.Html;
SmtpMail.Server(mailServer);
SmtpMail.Send(Mailer);
lblResult.Text = "Page successfully sent!";
}
catch(Exception ex)
{
lblResult.Text = "An error occurred: " + ex.ToString();
}

}

Random data from database

select * from employees order by NEWID()

Programmatically embed a picture inside an Excel Sheet

he following code uses EXCEL automation to open an existing workbook and embed an image.

In order to run this code, please add a COM reference to Microsoft Excel 11.0 Object Library in your project


object missingParam = Type.Missing;
ApplicationClass objExcel = new ApplicationClass();
Workbook workbook = objExcel.Workbooks.Open(@"c:\book1.xls", missingParam, missingParam, missingParam, missingParam,
missingParam, missingParam, missingParam, missingParam,
missingParam, missingParam, missingParam, missingParam,
missingParam, missingParam);
Worksheet worksheet = (Worksheet) workbook.Worksheets["Sheet1"];
Range cellRange = worksheet.get_Range("A2", "A2");
string picFilePath = @"d:\sailboat.jpg";
Image picObject = Image.FromFile(picFilePath);
System.Windows.Forms.Clipboard.SetDataObject(picObject, true);
worksheet.Paste(cellRange, picObject);
workbook.Save();
System.Windows.Forms.Clipboard.Clear();
objExcel.Quit();

Exporting the DataGrid to a Word file

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection ("data source=(local);initial catalog=Northwind;Pwd=xxxxxx;User ID=sa");
SqlCommand cmd = new SqlCommand ("Select LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDate, Address, City, Region, PostalCode, Country from Employees", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
myDataGrid.DataSource = ds.Tables[0];
myDataGrid.DataBind();
}

}

private void Button2_Click(object sender, System.EventArgs e)
{
// export to word
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

myDataGrid.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

Text to Speech Applications in C#


Microsoft always provides simple solutions for complex problems.

Microsoft introduced Speech SDK 5.1 to handle text to speech applications. You can download speech SDK from below microsoft Site.

http://www.microsoft.com/downloads/details.aspx?FamilyId=5E86EC97-40A7-453F-B0EE-6583171B4530&displaylang=en

After installed SpeechSDK51.exe, you need to do the following steps to create speech application.

1. Open new C# Web Application.
2. Add the Reference Microsoft object speech application
3. Add the namespace : using SpeechLib;
4. You should refer the below api for this application.

[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

Find the simple source code for speech application in web.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;
using SpeechLib;

public partial class _Default : System.Web.UI.Page
{
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

protected void Button4_Click(object sender, EventArgs e)
{
SpVoice spVoice = new SpVoice();
SpeechVoiceSpeakFlags svsp = new SpeechVoiceSpeakFlags();
spVoice.Speak(this.TextBox1.Text.ToString(),svsp);
}
}

Record Voice From Microphone

        
1. Open C#.net web applications. And added the blow namespace.

using Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;

2. Add the below API.
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

3. Create three Buttons and given the below name and text for the buttons.

1. Record
2. SaveStop
3. Read

1. Under Record Button Click paste the below Code:

// record from microphone
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);

2. Under Save / Stop button Click,


// stop and save
mciSendString("save recsound c:\\record.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
Computer c = new Computer();
c.Audio.Stop();

3. Under Read Button Click

Computer computer = new Computer();
computer.Audio.Play("c:\\record.wav", AudioPlayMode.Background);


Save and Execute it.

Monday, January 21, 2008

Read RSS Feeds and display in ASP.NET data grid

private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXml("http://www.dotnetbips.com/articles/rssfeed.aspx");
dgRSS.DataSource=ds.Tables[2].DefaultView;
dgRSS.DataBind();
}


private void dgRSS_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgRSS.CurrentPageIndex=e.NewPageIndex;
dgRSS.DataBind();
}