MY bLOG

Saturday, 23 November 2013

How to upload file and save it in database?

I would like to explain about  upload images and save it in database you have already seen to upload an images .

In database:
create table & stored procedure:
CREATE TABLE [dbo].[photoalbum](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Images] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO
stored procedure:
create procedure sp_insert(@Name nvarchar(50),@Images nvarchar(MAX))
as
begin
insert into photoalbum values(@Name,@Images)

end
In .aspx page:

<body>
    <form id="form1" runat="server">
    <div>
    <h2 align="center">Upload and Save</h2>
        <table align="center">
<tr>
<td>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
       <td>
<asp:TextBox  ID="TXT_NAME" runat="server"></asp:TextBox>
</td>
</tr>
       <tr>
<td>
Image:</td><td><asp:FileUpload ID="fileupload1" runat="server" />
</td>
</tr>
        <tr>
<td colspan="2" align="center">
           <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></td>
</tr>
        </table>
    <asp:Label ID="lbl_text" runat="server"></asp:Label>
    </div>
    </form>
</body>
you can add an Images folder in the solution Explorer
In .aspx.cs page:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class TouploadandView : System.Web.UI.Page
    {
        string strcon=ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
            SqlConnection con=new SqlConnection(strcon);
            if (fileupload1.HasFile)
            {
                if (fileupload1.PostedFile.ContentType == "image/jpeg" || fileupload1.PostedFile.ContentType == "image/gif" || fileupload1.PostedFile.ContentType == "image/x-png" && fileupload1.PostedFile.ContentLength == 1024)
                {
                    string path = "~/Images/" + fileupload1.FileName;
                    fileupload1.SaveAs(Server.MapPath(path));
                  string getfile=path;
                  SqlCommand cmd = new SqlCommand("sp_insert", con);
                    cmd.CommandType=CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Name",TXT_NAME.Text.Trim());
                    cmd.Parameters.AddWithValue("@Images",getfile);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    lbl_text.Text="Image upload successfull";
                    con.Close();
                }
            }
            }
                   catch(Exception ex)
            {
                   lbl_text.Text=ex.Message;


                   }
}
}
}


i hope you understand this article
Happy coding.........

Thursday, 14 November 2013

Captcha Image

In this article i would like to explain about captcha image code here
Right click on solution Explorer add new item
Code in .aspx pge:

<h1 align="center">captcha image</h1>
    <table align="center">
<tr>
<td>Prove You are not a robot:</td>
<td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        <td><asp:TextBox ID="txtrobocomp" runat="server"  Font-Names="Vivaldi" 
                        Font-Size="X-Large" Font-Bold="True" BackColor="#CC99FF" ForeColor="#CC0066" 
                        Width="148px" Height="33px"></asp:TextBox></td>
<td>
            <asp:ImageButton ID="ImageButton1" runat="server" 
                ImageUrl="~/images/14635845-recycling-reload-white-transparent-and-glossy-web-interface-icon-with-reflection.jpg" 
                Width="30px" Height="30px" AlternateText="refresh" 
                onclick="ImageButton1_Click" />
            </td>
<td>
                <asp:ImageButton ID="ImageButton2" runat="server"  
                    ImageUrl="~/images/download.jpg" Height="30px" Width="30px" 
                    onclick="ImageButton2_Click"/></td>
                    <td>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" 
                            ControlToValidate="TextBox1" ControlToCompare="txtrobocomp" 
                            ErrorMessage="Please enter correct Text" ForeColor="Red"></asp:CompareValidator></td>

            </tr>

            <tr><td colspan="6" align="center">
                <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" 

                    style="height: 26px" /></td></tr></table>

Code in .aspx.cs page: 

Add Namespace for string buider
 using sytem.Text;

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(RandomNumber(10, 99));
                builder.Append(RandomString(3, true));
                builder.Append(RandomString(2, false));
                txtrobocomp.Text = builder.ToString();
            }
        }
        private int RandomNumber(int min, int max)
        {
            Random rd = new Random();
            return rd.Next(min, max);
        }

        private string RandomString(int size, bool lowerCase)

        {
            StringBuilder builder = new StringBuilder();
            Random rd = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rd.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomNumber(10, 99));
            builder.Append(RandomString(3, true));
            builder.Append(RandomString(2, false));
            txtrobocomp.Text = builder.ToString();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

        {
            SpVoice sp = new SpVoice();
            sp.Volume = 90;
            sp.Speak(txtrobocomp.Text);
         
        }
hope you understand .
In this article we have seen captcha image using asp.net with refresh and speech text.

Wednesday, 13 November 2013

speech recognition

Speech recognition :
This article will explain  how to convert entered text into speech format so here i will explain speech recognition using asp.net.

At first we have to create a single web application and then ->Add New Item in Solution Explorer

and then you have to add reference for speech library which was shown in above

We have to add reference at COM and select Microsoft Speech Object Library and click on Ok.
The speech library is added to our reference
Then at first we have to add namespace like using SpeechLib;
create an object of spvoice class in .aspx page.
 <table>
<tr>
<td>Type your Text:</td>
<td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
    <td><asp:ImageButton ID="ImageButton2" ImageUrl="~/images/download.jpg" 
            AlternateText="sppech" runat="server" Height="25px" Width="25px" 
            onclick="ImageButton2_Click" /></td>
</tr>
</table>
Write below code in.aspx.cs page in a button click event
 protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            SpVoice sp = new SpVoice();
            sp.Volume = 100; 
                sp.Speak(TextBox1.Text);
        }

write some text in textboxand then click on image button  to convert text into speech converter.
I hope you  understand the concept of speech recognition.