Search This Blog

Tuesday, September 28, 2010

Application to speak the text written in the textbox using C#.Net

Application to speak the text in the textbox using C#.Net

Design:


                               
Design the form as shown above with one TextBox and three Buttons,
Set the ‘textBox1’ Properties as follows:
Dock: Top,
Multiline: True.

->Now  goto ‘Project’ Menu -> Select ‘AddReference’-> Click on ‘COM’ tab,
    Select ‘Microsoft Speech Object Library’ COM component -> OK

                    

->Now goto code window and include ‘using SpeechLib’ namespace

Code:

using System;
using System.Windows.Forms;
using SpeechLib;//include this namespace

namespace TextSpeaker
{
    public partial class TextSpeakerForm : Form
    {
        public TextSpeakerForm()
        {
            InitializeComponent();
        }

        private void btnSpeak_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim().Length > 0)
            {
                SpVoice obj = new SpVoice();
                obj.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault);
            }
            MessageBox.Show("Plz. write some text in the TextBox","Info.",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Output:
            Write some text in the textbox and press ‘speak’ button

                                 

Monday, September 20, 2010

sending e-mail to gmail using asp.net web application

Design:


----------------------
using System;
using System.Net;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    MailMessage msgobj;
    protected void btnSend_Click(object sender, EventArgs e)
    {
        SmtpClient serverobj = new SmtpClient();
        serverobj.Credentials = new NetworkCredential(TextBox1.Text, TextBox2.Text);
        serverobj.Port = 587;
        serverobj.Host = "smtp.gmail.com";
        serverobj.EnableSsl = true;

        msgobj = new MailMessage();
        msgobj.From = new MailAddress(TextBox1.Text, "email demo", System.Text.Encoding.UTF8);
        msgobj.To.Add(TextBox3.Text);

        msgobj.Priority = MailPriority.High;
        msgobj.Subject = TextBox4.Text;
        msgobj.Body = TextBox5.Text;
        msgobj.Attachments.Add(new Attachment(MapPath("accept.jpg")));
        msgobj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
        serverobj.Send(msgobj);
        LabelOutput.Text = "mail sent successfully...!";

    }
}

------------------

Output:

Thursday, September 16, 2010

Inserting an Image into SQL server directly from ManagementStudio(without using frontend app.)

Syntax:

insert into <tablename>(<column-name>) (select * from openrowset(bulk N'<path of image>',single_blob) as <column-name>)


eg:

create table picture (photo image)

insert into picture(photo) (select * from openrowset(bulk N'e:\prash\sunrise.jpg',single_blob) as photo)

inserting & retrieving images from database without using stored procedures in c#.net



Note:
Goto Project Menu
->Add Reference -> select 'Microsoft.VisualBasic' from .NET tab;
 include namespace ‘using Microsoft.VisualBasic’(To get input box)
---------
Add OpenFileDialog control to the form, which appears below the form
Add PictureBox Control and change properties as following:
   ->BorderStyle=Fixed3D; SizeMode=StrechImage
---------------------------------------
Code:
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.VisualBasic;

namespace prash_studentinfo
 {
  public partial class student_info : Form
  {
     public student_info()
     {
         InitializeComponent();
     }
    
     SqlConnection con;
     SqlCommand cmd;
     SqlDataAdapter adapter;
     DataSet ds; int rptr = 0;
     MemoryStream ms;
     byte[] photo_aray;

     private void student_info_Load(object sender, EventArgs e)
     {
         con = new SqlConnection("user id=sa;password=123;database=prash");
         loaddata();
         showdata();
     }

     void loaddata()
     {                 
         adapter = new SqlDataAdapter("select sno,sname,course,fee,photo from student",con);
         adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
         ds = new DataSet(); adapter.Fill(ds, "student");
     }

     void showdata()
     {
         if (ds.Tables[0].Rows.Count > 0)
         {
          textBox1.Text = ds.Tables[0].Rows[rptr][0].ToString();
          textBox2.Text = ds.Tables[0].Rows[rptr][1].ToString();
          textBox3.Text = ds.Tables[0].Rows[rptr][2].ToString();
          textBox4.Text = ds.Tables[0].Rows[rptr][3].ToString();
          pictureBox1.Image = null;
          if (ds.Tables[0].Rows[rptr][4]!=System.DBNull.Value)
          {
             photo_aray = (byte[])ds.Tables[0].Rows[rptr][4];
             MemoryStream ms = new MemoryStream(photo_aray);
             pictureBox1.Image = Image.FromStream(ms);
          }
         }
         else
             MessageBox.Show("No Records");
     }        
  
     private void browse_Click(object sender, EventArgs e)
     {
         openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*";
         DialogResult res = openFileDialog1.ShowDialog();
         if (res == DialogResult.OK)
         {
             pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
         }
     }

     private void newbtn_Click(object sender, EventArgs e)
     {
         cmd = new SqlCommand("select max(sno)+10 from student", con);
         con.Open();
         textBox1.Text = cmd.ExecuteScalar().ToString();
         con.Close();
         textBox2.Text = textBox3.Text = textBox4.Text = "";
         pictureBox1.Image = null;
     }

     private void insert_Click(object sender, EventArgs e)
     {
         cmd = new SqlCommand("insert into student(sno,sname,course,fee,photo) values("+textBox1.Text+",'"+textBox2.TabIndex+"','"+textBox3.Text+"',"+textBox4.Text+",@photo)", con);
         conv_photo();
         con.Open();
         int n = cmd.ExecuteNonQuery();
         con.Close();
         if (n > 0)
         {
             MessageBox.Show("record inserted");
             loaddata();
         }
         else
             MessageBox.Show("insertion failed");
     }

     void conv_photo()
     {
         //converting photo to binary data
         if (pictureBox1.Image != null)
         {
             //using FileStream:
                //FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                //byte[] photo_aray = new byte[fs.Length];
                //fs.Read(photo_aray, 0, photo_aray.Length); 
   
             //using MemoryStream:
             ms = new MemoryStream();
             pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
             byte[] photo_aray = new byte[ms.Length];
             ms.Position = 0;
             ms.Read(photo_aray, 0, photo_aray.Length);
             cmd.Parameters.AddWithValue("@photo", photo_aray);
         }
     }

     private void search_Click(object sender, EventArgs e)
     {
         try
         {
             int n = Convert.ToInt32(Interaction.InputBox("Enter sno:", "Search", "20", 100, 100));
             DataRow drow;           
             drow = ds.Tables[0].Rows.Find(n);
             if (drow != null)
             {
                 textBox1.Text = drow[0].ToString();
                 textBox2.Text = drow[1].ToString();
                 textBox3.Text = drow[2].ToString();
                 textBox4.Text = drow[3].ToString();
                 pictureBox1.Image = null;
                 if (drow[4] != System.DBNull.Value)
                 {
                     photo_aray = (byte[])drow[4];
                     MemoryStream ms = new MemoryStream(photo_aray);
                     pictureBox1.Image = Image.FromStream(ms);
                 }
             }
             else
                 MessageBox.Show("Record Not Found");
         }
         catch
         {
             MessageBox.Show("Invalid Input");
         }
     }

     private void update_Click(object sender, EventArgs e)
     {

         cmd = new SqlCommand("update student set sname='" + textBox2.Text + "', course='" + textBox3.Text + "', fee='" + textBox4.Text + "', photo=@photo where sno=" + textBox1.Text, con);
         conv_photo();       
         con.Open();
         int n = cmd.ExecuteNonQuery();
         con.Close();
         if (n > 0)
         {
             MessageBox.Show("Record Updated");
             loaddata();
         }
         else
             MessageBox.Show("Updation Failed");
     }

     private void delete_Click(object sender, EventArgs e)
     {
         cmd = new SqlCommand("delete from student where sno="+textBox1.Text, con);
         con.Open();
         int n = cmd.ExecuteNonQuery();
         con.Close();
         if (n > 0)
         {
             MessageBox.Show("Record Deleted");
             loaddata();
             rptr = 0;
             showdata();
         }
         else
             MessageBox.Show("Deletion Failed");
     }

     private void first_Click(object sender, EventArgs e)
     {
         rptr = 0; showdata();
         MessageBox.Show("First record");
     }

     private void previous_Click(object sender, EventArgs e)
     {
         if (rptr > 0)
         {
             rptr--; showdata();
         }
         else
             MessageBox.Show("First record");
     }

     private void next_Click(object sender, EventArgs e)
     {
         if (rptr < ds.Tables[0].Rows.Count - 1)
         {
             rptr++; showdata();            
         }
         else
             MessageBox.Show("Last record");
     }

     private void last_Click(object sender, EventArgs e)
     {
         rptr = ds.Tables[0].Rows.Count - 1;
         showdata(); MessageBox.Show("Last record");
     }

     private void exit_Click(object sender, EventArgs e)
     {
         this.Close();
     }       
   }
}
---------------------------
Query for creating table:
create table student(sno int primary key,sname varchar(50),cou

inserting & retrieving images from database using stored procedures in c#.net


Note:
Goto Project Menu
->Add Reference -> select 'Microsoft.VisualBasic' from .NET tab; include namespace ‘using Microsoft.VisualBasic’(To get input box)
---------
Add OpenFileDialog control to the form, which appears below the form
Add PictureBox Control and change properties as following:
   ->BorderStyle=Fixed3D; SizeMode=StrechImage
----------------------------------------------------
Code:
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.VisualBasic;

namespace prash_studentinfo
 {
  public partial class student_info : Form
  {
     public student_info()
     {
         InitializeComponent();
     }
    
     SqlConnection con;
     SqlCommand cmd;
     SqlDataAdapter adapter;
     DataSet ds; int rptr = 0;
     MemoryStream ms;
     byte[] photo_aray;

     private void student_info_Load(object sender, EventArgs e)
     {
         con = new SqlConnection("user id=sa;password=123;database=prash");
         loaddata();
         showdata();
     }

     void loaddata()
     {                 
         cmd = new SqlCommand("get_student", con);
         cmd.CommandType = CommandType.StoredProcedure; adapter = new SqlDataAdapter(cmd);
         adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
         ds = new DataSet(); adapter.Fill(ds, "student");
     }

     void showdata()
     {
         if (ds.Tables[0].Rows.Count > 0)
         {
          textBox1.Text = ds.Tables[0].Rows[rptr][0].ToString();
          textBox2.Text = ds.Tables[0].Rows[rptr][1].ToString();
          textBox3.Text = ds.Tables[0].Rows[rptr][2].ToString();
          textBox4.Text = ds.Tables[0].Rows[rptr][3].ToString();
          pictureBox1.Image = null;
          if (ds.Tables[0].Rows[rptr][4]!=System.DBNull.Value)
          {
             photo_aray = (byte[])ds.Tables[0].Rows[rptr][4];
             MemoryStream ms = new MemoryStream(photo_aray);
             pictureBox1.Image = Image.FromStream(ms);
          }
         }
         else
             MessageBox.Show("No Records");
     }        
  
     private void browse_Click(object sender, EventArgs e)
     {
         openFileDialog1.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*";
         DialogResult res = openFileDialog1.ShowDialog();
         if (res == DialogResult.OK)
         {
             pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
         }
     }

     private void clear_Click(object sender, EventArgs e)
     {
         textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = "";
         pictureBox1.Image = null;
     }        
     private void insert_Click(object sender, EventArgs e)
     {
         cmd = new SqlCommand("insert_student", con);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@sno", textBox1.Text);
         cmd.Parameters.AddWithValue("@sname", textBox2.Text);
         cmd.Parameters.AddWithValue("@course", textBox3.Text);
         cmd.Parameters.AddWithValue("@fee", textBox4.Text);

         conv_photo();       
         
         con.Open();
         int n = cmd.ExecuteNonQuery();
         con.Close();
         if (n > 0)
         {
             MessageBox.Show("record inserted");
             loaddata();
         }
         else
             MessageBox.Show("insertion failed");
     }

     void conv_photo()
     {
         //converting photo to binary data
         if (pictureBox1.Image != null)
         {
             //using FileStream: (will not work in updating record without changing photo)
             //FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
             //byte[] photo_aray = new byte[fs.Length];
             //fs.Read(photo_aray, 0, photo_aray.Length); 
   
             //using MemoryStream: (works for updating record without changing photo)
             ms = new MemoryStream();
             pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
             byte[] photo_aray = new byte[ms.Length];
             ms.Position = 0;
             ms.Read(photo_aray, 0, photo_aray.Length);
             cmd.Parameters.AddWithValue("@photo", photo_aray);
         }
     }

     private void search_Click(object sender, EventArgs e)
     {
         try
         {
             int n = Convert.ToInt32(Interaction.InputBox("Enter sno:", "Search", "20", 100, 100));
             DataRow drow;           
             drow = ds.Tables[0].Rows.Find(n);
             if (drow != null)
             {
                 textBox1.Text = drow[0].ToString();
                 textBox2.Text = drow[1].ToString();
                 textBox3.Text = drow[2].ToString();
                 textBox4.Text = drow[3].ToString();
                 pictureBox1.Image = null;
                 if (drow[4] != System.DBNull.Value)
                 {
                     photo_aray = (byte[])drow[4];
                     MemoryStream ms = new MemoryStream(photo_aray);
                     pictureBox1.Image = Image.FromStream(ms);
                 }
             }
             else
                 MessageBox.Show("Record Not Found");
         }
         catch
         {
             MessageBox.Show("Invalid Input");
         }
     }

     private void update_Click(object sender, EventArgs e)
     {
         cmd = new SqlCommand("update_student", con);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@sno", textBox1.Text);
         cmd.Parameters.AddWithValue("@sname", textBox2.Text);
         cmd.Parameters.AddWithValue("@course", textBox3.Text);
         cmd.Parameters.AddWithValue("@fee", textBox4.Text);

         conv_photo();
      
         con.Open();
         int n = cmd.ExecuteNonQuery();
         con.Close();
         if (n > 0)
         {
             MessageBox.Show("Record Updated");
             loaddata();
         }
         else
             MessageBox.Show("Updation Failed");
     }

     private void delete_Click(object sender, EventArgs e)
     {
         cmd = new SqlCommand("delete_student", con);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@sno", textBox1.Text);
         con.Open();
         int n = cmd.ExecuteNonQuery();
         con.Close();
         if (n > 0)
         {
             MessageBox.Show("Record Deleted");
             loaddata();
             rptr = 0;
             showdata();
         }
         else
             MessageBox.Show("Deletion Failed");
     }

     private void first_Click(object sender, EventArgs e)
     {
         rptr = 0; showdata();
         MessageBox.Show("First record");
     }

     private void previous_Click(object sender, EventArgs e)
     {
         if (rptr > 0)
         {
             rptr--; showdata();  
         }
         else
             MessageBox.Show("First record");
     }

     private void next_Click(object sender, EventArgs e)
     {
         if (rptr < ds.Tables[0].Rows.Count - 1)
         {
             rptr++; showdata();
         }
         else
             MessageBox.Show("Last record");
     }

     private void last_Click(object sender, EventArgs e)
     {
         rptr = ds.Tables[0].Rows.Count - 1;
         showdata(); MessageBox.Show("Last record");
     }

     private void exit_Click(object sender, EventArgs e)
     {
         this.Close();
     }
  }
}
---------------------------------
Query for creating table:
create table student(sno int primary key,sname varchar(50),course varchar(50),fee money,photo image)
---------------------------------
Stored procedures:

create procedure  get_student
as
select  sno,sname, course, fee, photo from student
---------------
create procedure insert_student
(@sno int, @sname varchar(50), @course varchar(50), @fee smallmoney,@photo image=null)
as
insert into student(sno, sname,course, fee, photo) values (@sno, @sname, @course, @fee, @photo)
---------------
create procedure  update_student
(@sno int, @sname varchar(50),@course varchar(50), @fee smallmoney,@photo image=null)
as
update student set sname=@sname, course=@course, fee=@fee, photo=@photo where sno=@sno
---------------
create procedure delete_student
(@sno int=null)
as
if not(@sno=null)
delete from student where sno=@sno