Tuesday, 5 February 2013

Langauge Translator using c# in asp.net.



In this post I have explained Langauge translator using c# in asp.net.

Description:-

Language translator is used in every web site or web application. The benifite of translator is user can read or see website in their own language. It is easy to understand the content in own language. In this article I have used microsoftTranslator appId to translate the language in specific language.


Now open your visual -> open new website -> click ok.

Design your page like this:-

Default.aspx:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 50%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Enter Text</td>
                <td align="left">
                    <asp:TextBox ID="txtValue" runat="server" Height="79px" Width="460px"
                        TextMode="MultiLine" BackColor="#CCCCCC"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Select Langauge</td>
                <td align="left">
                    <asp:DropDownList ID="ddlLangauge" runat="server">
                        <asp:ListItem Text="Hindi" Value="hi"></asp:ListItem>
                        <asp:ListItem Text="French" Value="fr"></asp:ListItem>
                        <asp:ListItem Text="German" Value="de"></asp:ListItem>
                        <asp:ListItem Text="Chinese Simplified" Value="zh-CN"></asp:ListItem>
                        <asp:ListItem Text="Japanise" Value="ja"></asp:ListItem>
                        <asp:ListItem Text="Swedish" Value="sv"></asp:ListItem>
                        <asp:ListItem Text="Russian" Value="ru"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:Button ID="btnTranslate" runat="server" Text="Translate"
                        onclick="btnTranslate_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    Translated Langauge:</td>
                <td align="left" bgcolor="#CCCCCC">
                    <asp:Label ID="ltTranslatetxt" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Code Behind Page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void Translate(string textvalue, string to)
    {
        string appId = "A70C584051881A30549986E65FF4B92B95B353A5";
        string from = "en";
        string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=" + appId + "&text=" + textvalue + "&from=" + from + "&to=" + to;
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
        WebResponse response = null;
        try
        {
            response = httpWebRequest.GetResponse();
            using (Stream stream = response.GetResponseStream())
            {
                System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                string translation = (string)dcs.ReadObject(stream);
                ltTranslatetxt.Text = translation + ".";
            }
        }
        catch (WebException e)
        {
            ProcessWebException(e, "Failed to Translate");
        }
        finally
        {
            if (response != null)
            {
                response.Close();
                response = null;
            }
        }
    }
    private void ProcessWebException(WebException e, string message)
    {
        ltTranslatetxt.Text = message + ": :" + e.ToString();
        string strResponse = string.Empty;
        using (HttpWebResponse response = (HttpWebResponse)e.Response)
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                {
                    strResponse = sr.ReadToEnd();
                }
            }
        }
        ltTranslatetxt.Text = "Http status code=" + e.Status + ",error message=" + strResponse;
    }

    protected void btnTranslate_Click(object sender, EventArgs e)
    {
        Translate(txtValue.Text,ddlLangauge.SelectedItem.Value.ToString());
    }
    string lng = @"ca,ca-es,da,da-dk,de,de-de,
            en
            en-au
            en-ca
            en-gb
            en-in
            en-us
            es
            es-es
            es-mx
            fi
            fi-fi
            fr
            fr-ca
            fr-fr
            it
            it-it
            ja
            ja-jp
            ko
            ko-kr
            nb-no
            nl
            nl-nl
            no
            pl
            pl-pl
            pt
            pt-br
            pt-pt
            ru
            ru-ru
            sv
            sv-se
            zh-chs
            zh-cht
            zh-cn
            zh-hk
            zh-tw";
}

Output:-



Click Here to Download the code.


Monday, 4 February 2013

Online bus Reservation project

Hi friends here i have attached Online bus reservation project for Beginner .

First see the whole project and then make this project yourself (be honestly).

Hope you will enjoy from this project. Please spread this blog among your friends.Don't forget to comment because your comment will useful to other.

Import Excel Data in Gridview in asp.net using c#.


In this article I have explained  how to import excel data in GridView using c# in asp.net.

First create an excel data base file. Give it a name like “registration.xlsx”.
Make  table structure like this in excel file.

ID
Name
Designation
1
sushil
Soft
2
pradeep
Engg
3
Rahul
HOD

After that  Open Visual Studio -> open new website ->  Click on Empty Web Application -> Right click on Project file -> Add New Item -> click on web form -> click ok.

Default.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Import Excel file in GridView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
<tr>
<td>
<span style="color: Red">*</span>Attach Excel file
</td>
<td>
<asp:FileUpload ID="fileuploadExcel" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Export" onclick="btnSend_Click"  />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
    </div>
    </form>
</body>
</html>

Code Behind Page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class Default : System.Web.UI.Page
{
    private String strConnection = "Data Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        //file upload path
        string path = fileuploadExcel.PostedFile.FileName;
        //Create connection string to Excel work book
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
        //Create Connection to Excel work book
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]", excelConnection);
        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
        //Give your Destination table name
        sqlBulk.DestinationTableName = "reg";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();
    }
}

Debuge the code and run .

Sunday, 3 February 2013

Create simple VirtualKeyBoard using JavaScript in asp.net.


In this article ,I have explained how to create VirtualKeyBoard using JavaScript in Asp.Net.

Description:-
VirtualKeyBoard is basically used for security purpose in web page. This is useful when you are login in secure page or some important page or in paypal transaction page.  In this example I have design a keybaoard in  which keyboard numbers are shuffled at every page laod.

Open Visual studio -> create new website -> open Empty website -> add a new page .

Default.aspx:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function Load() {
            var array = new Array();
            while (array.length < 10) {
                var temp = Math.round(Math.random() * 9);
                if (!contain(array, temp)) {
                    array.push(temp);
                }
            }
            for (i = 0; i < 10; i++) {
                var btn = document.getElementById("btn" + i);
                btn.value = array[i];
            }
        }
        function contain(array, num) {
            for (var i = 0; i < array.length; i++) {
                if (array[i] == num) {
                    return true;
                }
            }
            return false;
        }
        function input(e) {
            var tbInput = document.getElementById("tbInput");
            tbInput.value = tbInput.value + e.value;
        }
        function del() {
            var tbInput = document.getElementById("tbInput");
            tbInput.value = tbInput.value.substr(0, tbInput.value.length - 1);
        }
    </script>
   
</head>
<body onload="Load()">
    <form id="form1" runat="server">
    <div>
    <input id="tbInput" type="text" />
    </p>
    <div id="virtualKeyboard">
    <input id="btn1" type="button" onclick="input(this);" />
    <input id="btn2" type="button" onclick="input(this);" />
    <input id="btn3" type="button" onclick="input(this);" />
    <br />
    <input id="btn4" type="button" onclick="input(this);" />
    <input id="btn5" type="button" onclick="input(this);" />
    <input id="btn6" type="button" onclick="input(this);" />
    <br />
    <input id="btn7" type="button" onclick="input(this);" />
    <input id="btn8" type="button" onclick="input(this);" />
    <input id="btn9" type="button" onclick="input(this);" />
    <br />
    <input id="btn0" type="button" onclick="input(this);" />
    <input id="btnDel" type="button" value="Backspace" onclick="del();" />
    </div>
    </div>
    </form>
</body>
</html>

Debuge the code.

OutPut:-