Pages

Thursday, August 15, 2013

How to send mail from Gmail address to any mail address using C# code and SMTP.

It is easy to send mail from your email account to another email using gmail smtp server, Here the sample code in C#,  you should have to add the following class files in includes,

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

Here is the complete code,

            MailMessage mail = new MailMessage();
            string from = "fromemailaddress@gmail.com"; // your gmail address
            string pass = "yourpassword";  // your gmail password
            mail.From = new MailAddress(from);
            mail.To.Add("tomailaddress@gmail.com");  //to mail address
            mail.Subject = "This is test mail from smtp";
            mail.Body = "This is my test mail sending from SMTP ";
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.EnableSsl = true;
            smtp.Credentials = new NetworkCredential(from, pass);
            try
            {
                smtp.Send(mail);

            }
            catch (Exception ex)
            {
                throw new System.Exception(ex.Message);
            }

Thursday, July 25, 2013

How do validate Email address using jQuery?

Here is example for validating user email id using jquery

HTML
<input type="text" id="email"></input> 
<input type="button" value="test" id="click"/>

jQuery
$("#click").click(function (e) {
 var emailReg = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;;
  if( !emailReg.test( $("#email").val() ) ) {
    alert("Invalid email id");  
    return false;
  } else {
    alert("Valid email id");  
    return true;
  }    
});

Thursday, June 27, 2013

How to dynamically add a CSS class for an HTML element

Here is another wonderful feature of jQuery, you can add or remove CSS class dynamically using jQuery addClass() and removeClass() methods. For example, if you want to change the background color of a DIV element for a bit of time, you can add that CSS class property on the fly.
see the example below,
HTML
<div id ="notice" class="">Please notice this area</div>
<button>Add Class</button>
jQuery
$("button").click(function(){
    $("#notice").addClass("divcls");
    setTimeout(function() { $("#notice").removeClass("divcls") }, 1000);
 });
CSS
.divcls{background-color:red;font-weight:bold;width:150px}

Tuesday, June 25, 2013

How to read data from a JSON file using jQuery (getJSON() method)

Here is the example how to read a simple JSON file data using jQuery getJSON() method,

Sample JSON file structure, 

{
"news1": "This is a first sample news feed",
"news2": "This is a second sample news feed",
"news3": "This is a third sample news feed"
}
this file you can store as .js or .json extension or even .txt (for this example, I store it as .js extension in script folder)

HTML 
<ul>
  <li><div id="news1" style="height:100px;width:auto"></div> </li>
  <li><div id="news2" style="height:100px;width:auto"></div></li>
  <li><div id="news3" style="height:100px;width:auto"></div> </li>
</ul>
jQuery
<script type="text/javascript">
 $(document).ready(
  function() {
      $.getJSON('script/news1.js', function(jd) {
   $('#news1').append('<p>' + jd.news1 + '</p>');
   $('#news2').append('<p>' + jd.news2 + '</p>');
   $('#news3').append('<p>' + jd.news3 + '</p>');  
   });  
 });
</script> 

Monday, June 24, 2013

Cloning an HTML element using JQuery Clone() function.

Its just  an another amazing feature of JQuery. You can simply duplicate an HTML element using the Clone() function. for example, if you want to add new Table Row dynamically in to a Table, user can use Clone() function. Here is an example code,

HTML
<form>
    <table border="1" id="experiance">
        <tr>
            <th>Sl No</th>
            <th>Language</th>
            <th>Experiance</th>
            <th>Proficient</th>
        </tr>
        <tr>
            <td> <input type="text" /> </td>
            <td> <input type="text" /></td>
            <td> <input type="text" /></td>
            <td>
                <select>
                     <option value = "1">Beginner</option>/>
                     <option value = "2">Itermediate</option>/> 
                     <option value = "3">advanced</option>/>
                </select> 
            </td>
        </tr>
    </table>
    <input id="add" type="button" value="Add New Row" />
</form>
JQuery
$("#add").on('click', function () {
    var rows = 1;    
    var lastRow;
    lastRow = $('#experiance tr').last().clone().appendTo("#experiance");      
});
DEMO

Saturday, June 22, 2013

Shadow effect on DIV when mouse Hover using CSS3

CSS 

div.shadow {
 width: 300px;
 margin: 20px;
 border: 1px solid #ccc;
 padding: 10px;
 }

div.shadow:hover {
 -moz-box-shadow: 0 0 5px rgba(0,0,0,0.5);
 -webkit-box-shadow: 0 0 5px rgba(0,0,0,0.5);
 box-shadow: 0 0 5px rgba(0,0,0,0.5);
 }


HTML
<div class="shadow">
This Simple example, gow to make shadow effect for DIV on mouse hover.
For this example, we can use CSS3 and HTML.
</div>

DEMO

Saturday, June 15, 2013

How to modify an HTML object's CSS property dynamically by using JQyery?

If user want to change the CSS property of an HTML Objects on the fly, you can use JQuery,
For example, if you want to change the color of all the text in a <DIV> element,

HTML
 <div id="change">Make Me in Red Color</div>
 <button id="color">Change Color</button>

JQuery

$(document).ready(
 function() {
  $("#color").click(
   function() {
    $("#change").css("color", "red");
   });
 });

DEMO

How to toggle two images dynamically using JQuery.?

User can toggle two images dynamically with out refresh the page, for that we can use JQuery, code snippet below,

HTML 
        <div id="pdiv" >
            <img id="slidimg" src="img1.jpg" alt="image1" width="186" height="448" />
        </div>

JQuery
                    $("#pdiv").click(
                    function() {
                        $("#pdiv").fadeToggle("slow", "swing",
                        function() {
                            $("#slidimg").attr("src", function(i, oldSrc) {
                            return oldSrc == 'img1.jpg' ? 'img2.jpg' : 'img1.jpg';                              
                            });
                            $("#pdiv").fadeToggle("slow", "swing");  
                        });
                    }
                );

DEMO
click below image to toggle the image

Monday, May 20, 2013

How to download a file from Web Server using ASP.NET

To Download a file from Web Server you can use the TransmitFile method of HttpResponse class. This function writes the file to an HTTP response output stream without buffering it in memory, see the example below,
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename="+strFileName);
Response.TransmitFile(Server.MapPath("~/FileUpload/" + strFileName));
Response.End();
In the above example, downloading the file as an attachment. 

How to add a new row of record to an XML file in ASP.NET

See the example first,
 XmlDocument xmldoc = new XmlDocument();
 xmldoc.Load(Server.MapPath("Users.xml"));
 XmlElement parentelement = xmldoc.CreateElement("users");
 XmlElement uname = xmldoc.CreateElement("UserName");
 XmlElement password = xmldoc.CreateElement("Password");
 XmlElement role = xmldoc.CreateElement("Role");
 uname.InnerText = txtUsrname.Text.Trim();
 password.InnerText = txtPass.Text.Trim();
 role.InnerText = txtRole.Text.Trim();
 parentelement.AppendChild(uname);
 parentelement.AppendChild(password);
 parentelement.AppendChild(role);
 xmldoc.DocumentElement.AppendChild(parentelement);
 xmldoc.Save(Server.MapPath("Users.xml"));
 lblresult.ForeColor = System.Drawing.Color.Green;
 lblresult.Text = " User "+txtUsrname.Text.Trim() + " added successfully";
 BindData();
The above example, XML file is using to keep authentication users list, it has three fields such as User Name, Password and Role. The above code add a new user to the XML file and shows the message. 

How to SELECT a specific row of data from a Data Table using a condition in ASP.NET

To retrieve a Data Row as per a filter condition you can use the SELECT method of Data Table. The SELECT method returns an array of all DataRow that match the filter condition. The SELECT method returns DataRow object. For Example,


var user = Users.Select("UserName='" + txtUsrname.Text.Trim() + "'");
The above example, "Users" is the Data Table, and trying to get the specific user as per your input on txtUsrname text field. For instance, if you want to check a particular user is already exist or not, first load all the users to a Data Table and filter the users table using the SELECT method, and check the array length using "if" condition. For example

DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath("Users.xml"));
DataTable Users = DS.Tables[0];
var user = Users.Select("UserName='" + txtUsrname.Text.Trim() + "'");
if (user.Length != 0)
{
 lblresult.ForeColor = System.Drawing.Color.Red;
 lblresult.Text = "User "+txtUsrname.Text + " Already exist";
 return;
}

Tuesday, April 2, 2013

How to find table names which have a specific column name in SQL Server?

In SQL Server Database, if you want to finds all the table names which have a specific column name field, you can use the JOIN query with "sys.tables" and "sys.columns" system tables. For example, if you want to find all the tables which have the column name "ITEM" use the following query,
SELECT t.name AS table_name,SCHEMA_NAME(schema_id) AS schema_name,c.name AS column_name 
FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name LIKE '%ITEM%' ORDER BY schema_name, table_name;


Thursday, March 28, 2013

How to put favicon (small image) in address bar of a web page?

First create a .ico image file. We could use the web site http://favicon.htmlkit.com/favicon to convert any picture to icon image. Now link that .ico image to your webpage using <link> tag in HTML <head> section. For example,
<link rel="shortcut icon" type="image/ico" href="~/Images/favicon.ico"/>

Tuesday, March 26, 2013

What is TCP Port?

Port is a 16-bit unique number that identifies  a specific software program on the server system. Ports are unique identifiers. For example, port 80 represents Web Server (HTTP) , port 21 represents FTP server and port 25 represents SMTP server. Without port numbers, the server system would have no way of knowing which application a client wanted to connect to. So, ports are just numbers that representing a server application. A server can have up to 65536 (0-65535) server application running. A TCP port numbers from 0 to 1023 are reserved for specific applications. Don't use these ports for your own custom server programs. 

How to pass parameter value for a subreport in Crystal Report

To set a parameter value for a parameter field in sub-report of a Crystal Report you may have to use the function

SetParameterValue(string parameterFieldName, object value, string subreport)
First create a ReportDocument object for main report and pass the parameter values to main report and subreports, for instance,
mainrpt = new ReportDocument();
ReportViewer.Enabled = true;
mainrpt.SetParameterValue("@ClmId", CLMID); // main report param
mainrpt.SetParameterValue("@Param1", ParamValue,"SubReport1.rpt"); //subreport param
mainrpt.SetParameterValue("@Param1", ParamValue,"SubReport2.rpt"); //subreport param
ReportViewer.ReportSource = mainrpt;
ReportViewer.Visible = true;