Wednesday, 15 May 2013

Automatic Scroll down in Multiline TextBox in asp.net using JavaScript.

Introduction:-


In this article , I have described how to automatic scroll down in Multiline textbox in asp.net using  JavaScript . ScrollBar will persistent when you entering data in textbox or adding data in textbox.

Design page:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    window.onload = function () {
        var textarea = document.getElementById('<%=TextBox2.ClientID %>');
        textarea.scrollTop = textarea.scrollHeight;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" Text="Sample Text"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
<hr />
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Height="100"
            ontextchanged="TextBox2_TextChanged"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Code Behnid Page:-

protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox2.Text += TextBox1.Text + "\n";
    }


Run the code and check output.

How to select Only string values from ArrayList using Link in Asp.net.

Introduction:-


In this article I have explained how to select only string values from ArrayList using Linq.

Code:-

Don't forget to add namespace:-
using System.Linq;

Write the following code in page load:-

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = string.Empty;
        ArrayList al = new ArrayList { "Hello", 200, "World", false, 100 };
        var onlyStr = al.OfType<string>();
        Console.WriteLine("Printing Only Strings");
        foreach (var str in onlyStr)
         Label1.Text +=   str.ToString()+"\n";
      
    }

For Console write the following Code:-

ArrayList al = new ArrayList { "Hello", 200, "World", false, 100 };
        var onlyStr = al.OfType<string>();
        Console.WriteLine("Printing Only Strings");
        foreach (var str in onlyStr)
 Console.WriteLine(str);
Console.ReadLine();
}



Run the code and check the result.

Tuesday, 14 May 2013

How to bind TextBox with Label in asp.net using jquery.

Introduction:-


Bind TextBox with label in asp.net using c#  means while you are writing inside textbox at the same time data will show on label. For this i have used jquery function.

Design Page:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BindTextBoxWithLabel.aspx.cs" Inherits="BindTextBoxWithLabel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){
    $("input[type=text]").keypress(function(){
    var lbl=document.getElementById("Label1");
    var text=document.getElementById('<%=TextBox1.ClientID%>').value;
    lbl.innerHTML=text;
    });
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       Write Something In Textbox : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        See the result in label at same time :<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>


Run the code and see the Output.

Wednesday, 8 May 2013

What is difference between variable.toString() method and Convert.toString(variable) in asp.net using c#.

The basic difference  is  "variable.toString()" does not handle NULL value it throws null exception error but Convert.toString(variable) handle NULL value  even if variable value becomes null.

Example:-

// throws a null exception error
        string str;
        int i = null;
        str = i.ToString();
 // return empty string values for str and does not throw exception
        string str;
        int i = null;
        str = Convert.ToString(i);

How to find IPAddress of your system in asp.net using c#.

Hi friends, In this article i have described how to know your system IP Address in asp.net using c#.

Write the following in the page_load or whereevr you want.

string strMachineName = System.Net.Dns.GetHostName();
        string IPAddress = System.Net.Dns.GetHostAddresses(strMachineName).GetValue(1).ToString();
        Label1.Text = "Client machine name: " + strMachineName;

        Label2.Text = "Client Machine Ip Address :" +" "+ IPAddress ;


Saturday, 9 February 2013

Crystal Report Example in asp.net.


In this post I have explained crystal report simple example in asp.net.

Description:-

Crystal report is application program used to design and generate  reports from wide range of data source. If you are new in crystal report and using  visual studio 2010 , you will face with some problem regarding crystal report because crystal report has not in build in VS 2010. So see my previous post How to install crystal report in VS 2010.Lets start with practical approach.

First Create a database table like this:-

Create table EmpDeatils(EmpID int primary key, EmpName varchar(50),Country varchar(50));

Put some dummy data in the table.

Now Open Visual Studio 2010 -> Open New Website -> Click on Empty Web application -> give a project name -> click ok.

Now right click on project -> add new items -> click on web page -> give name -> click ok.

Again right click on project -> add new items -> click on crystal report -> click ok.

After that crystal report will prompt crystal report gallery window in that select blank option and click ok.


After that click on crystal report menu bar under the database -> database expert.

After click on Database expert  , Database expert window will open in that select Create new section.

After that select Microsoft OLE DB Provider for SQL and click next.

After that enter your server name and database name and click next

After that again new prompt window will come and click on finish button.
After clicking the finish button now database will loaded in OLEDB section. Select your database  -> select dbo -> select your table (which you want).

Now open the table and move the selected table and click ok.

After that database fields in Field Explorer populated with your required data table .
Now drag and drop the required fields from the data table to the report details section.

Now open your design page and drag and drop CrystatReportViewer control from Reporting  tab.
Now select crystalReportViewer and click on smart tag in the right hand side and choose new report source.

When you click on New Report Source , a new window will open . Click on dropdown list and select crystalReport.rpt file.

After that your design page will look like this.
<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
            AutoDataBind="True"  Width="1104px" GroupTreeImagesFolderUrl="" Height="1202px"
            ReportSourceID="CrystalReportSource1" ToolbarImagesFolderUrl=""
            ToolPanelWidth="200px"/>
        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
            <Report FileName="CrystalReport.rpt">
            </Report>
        </CR:CrystalReportSource>
    </div>
    </form>
</body>
</html>

Run you application . Your report will like this.

How to install crystal report in Visual studio 2010.


Problem with Visual studio 2010 is that it does not have in build crystal report. First time I also get confused. When I click on crystal report in VS 2010 then it gives CrystalReport.htm file. In that Microsoft had written some line you can Download Crystal Report from SAP website. So either you click on that link to download crystal Report or Below I have given a link to download.



After Downloading the Crystal Report, You install the Crystal Report and Restart you VS 2010.

I Hope you will Enjoy.  

Thursday, 7 February 2013

Disable copy , cut and paste using c# in asp.net.


In this post I have explain how to disable copy , cut and paste using c# in asp.net without using java script.
Sometimes it is necessary to disable copy ,cut and paste function for textbox in web page or website. It is basically used in re-inter password or email account in registration page . So let’s do practically .

Open visual studion -> click on file -> open new website -> click on empty website ->  gie a project name  -> right click on solution -> add new item -> click on web page -> click ok.

Drage  a textbox on design page and write the three main  command in textbox.

1.) oncopy="return false"
2.) oncut="return false"
3.) onpaste="return false"

Design you page like this:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" oncut="return false"
            onpaste="return false" oncopy="return false" TextMode="MultiLine"
            BackColor="#669999" Height="103px" Width="445px"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Debuge the code and run.

Click here to download code. 

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.