Pages

Search This Blog

Thursday 26 June 2014

Code for mail ....

Hi friends, today in this session I will show the code for sending mail.

Mail code was useful to us in many situations like recover password and if you want to send login details to the user after registration.

Here I wrote a code for recover password where the user enters his email id to get username and password to his mail.... 

Here in my code I had took one textbox (to enter user mail id), two label (to specify enter mail and message as ‘sent’ or ‘invalid email’) and one button (to submit mail id)

Here is the code for button onclick function:-

using System.Net.Mail;

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
           DataSet ds = new DataSet();
//Server connection      
 using (SqlConnection con = new SqlConnection("DataSource=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial
Catalog=blood_bank;Integrated Security=True"))
            {

                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT                         USER_NAME,PASSWORD FROM users Where MAIL_ID= '" +  txtEmail.Text.Trim() + "'", con);
            
               SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtEmail.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtEmail.Text);
                Msg.Subject = "Your Password Details";
                Msg.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " + ds.Tables[0].Rows[0]["USER_NAME"] + "<br/><br/>Your Password: " + ds.Tables[0].Rows[0]["PASSWORD"] + "<br/><br/>";
                Msg.IsBodyHtml = true;
                // your remote SMTP server IP.
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
             
             // Mail will be sent from the below username          
             smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com""your password");
                
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                
               //Msg = null;
                lbltxt.Text = "Your Password Details Sent to your mail";
               
                // Clear the textbox values & where Textbox id="txtEmail"
                txtEmail.Text = "";
            }
            else
            {
                lbltxt.Text = "The Email you entered not exists.";
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
   } 

you can rate my class on right side top of my blog ..... 

                                                                         -Your's santoshklpkl

Saturday 21 June 2014

Database connection in ASP.NET



Hi friends, in this session I will say how to add database into our visual studio. So you can use that database tables in your application.

Let us take an example and I will show how to add database into our application.

Here I have created a database which is named as “blood_bank” and my server name was “sqlexpress”. I will show how to add that database into our application....




That’s it, Now you can use your database tables in your application...... J

 You can rate my ASP.NET classes on right side top of my blog.......


                                                                             -Your's santoshklpkl

How to create a sample project in ASP.NET ??

Hi friends, in this session I say “How to create a website/project using .ASP.net”.

Before saying about that I just want to say you to install Visual studio 2008/2010..... (http://download.cnet.com/Microsoft-Visual-Studio-2010-Professional/3000-2212_4-10618634.html) this is the link to download VS2010.....

There is no much difference between VS2008(3.5) and VS2010(4.0) the framework they uses was different ....

Here there is a sample program to create a project ....






You can rate my class on right side top of my blog ....

Thank you for watching video and wait for more classes.

                                                            -Your's Santoshklpkl

Logout test...

Hi friends, I think you are practising and creating a web application using my tips and with help of “Google”.

If you say “Yes” I had almost done it. Then I want to test your application. Cool I won’t ask to send your web application to me ..... ;) I just say something to try in your application.

Just press “Logout” (See session class) button in your application. You may think “what?? That’s it ?? what you have tested in my application??” .

Wait .... Wait .... Now press back arrow in your keyboard. Then see what happens in your application.

“Yes” that’s it....

Today I will say “what has to be done in our project after pressing logout button so that it never navigates back page again”.

Normally, we will create a session and assign user value while user login in to the application and then we will make session value into “Null” or we will “expire” session.

Normally in logic we will write “if session value equal to null goes to home page”.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] != null)
        {

    Label.Text = "Welcome" + " " + Session["Username"].ToString();
        }

        else
        {
       
        Label.Text = "expired";

       Response.Redirect("./home.aspx");

        }
    }

I too write the same logic but sometimes to make session value null it takes some “millsec”. If session has expired also if we press back arrow button it navigates to before page in our application.

So here I had a solution to overcome this problem.... J We will disable the functionality in the browser. We will do this by writing a code in “javascript” in “head” tag.

<script type = "text/javascript" >
    function changeHashOnLoad() {
        window.location.href += "#";
        setTimeout("changeHashAgain()", "50");
    }

    function changeHashAgain() {
        window.location.href += "1";
    }

    var storedHash = window.location.hash;
    window.setInterval(function () {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
        }
    }, 50);

</script>


We will call the “changeHashOnLoad” function in body.

<body onload="changeHashOnLoad();">


Simple now your application is perfect. 

                                                               -Your's Santoshklpkl

Wednesday 18 June 2014

Display & Edit a value from database.....

Hi friends, in this post I will show you “How to display a value in the label from Database”

Before saying that I will say at what situations we need of this??
Let us take an example “Facebook” view profile.. In that situation we need to display your details in the labels. Your details will be there in the database we must bring them from database with the help of “Session value” (See the class about Session) we will bring details of a particular person.

Here is the code ....

update.aspx:

<table style="height: 250px; width: 376px">
                                      <tr>
                                           <center>
                                                 <font color="#890208" size="3">
                                                        <h1>                                                                  Here You Can Update Your Details.
                                                          </h1>
                                                    </font>
                                                </center>
                                            </tr>
                                            <tr>
                                                <td class="style3">
                                                    <asp:Label ID="Label1" runat="server" Text="Mail ID" ForeColor="#890208"></asp:Label>
                                                </td>
                                                <td class="style3">
                                                    <asp:Label ID="lbmail" runat="server"></asp:Label>
                                                </td>
                                                <td class="style3">
                                                    <asp:Button runat="server" ID="Editmail" Text="Edit" OnClick="Editmail_Click" CausesValidation="false" />
                                                </td>
                                                <td class="style3">
                                                    <asp:TextBox ID="tblogin" runat="server"> </asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="reqlogin" runat="server" ControlToValidate="tblogin"
                                                        ErrorMessage="Enter Mail ID">*</asp:RequiredFieldValidator>
                                                </td>
                                                <td>
                                                    <asp:Button ID="updatedetails" Text="Update" runat="server" CausesValidation="True"
                                                        OnClick="updatedetails_Click" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="style2">
                                                    <asp:Label ID="Label2" runat="server" Text="Phone Number" ForeColor="#890208"> </asp:Label>
                                                </td>
                                                <td class="style2">
                                                    <asp:Label ID="lbphn" runat="server"></asp:Label>
                                                </td>
                                                <td class="style2">
                                                    <asp:Button ID="Editphn" runat="server" Text="Edit" OnClick="Editphn_Click" CausesValidation="false" />
                                                </td>
                                                <td class="style2">
                                                    <asp:TextBox ID="tbphn" runat="server"> </asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbphn"
                                                        ErrorMessage="Enter Phone Number">*</asp:RequiredFieldValidator>
                                                </td>
                                                <td>
                                                    <asp:Button ID="updatephn" runat="server" Text="Update" CausesValidation="True" OnClick="updatephn_Click" />
                                                    <asp:ValidationSummary runat="server" ID="vs" ShowMessageBox="true" ShowSummary="false" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                                <td>
                                                    <asp:Button ID="cancel" runat="server" Text="CANCEL" CausesValidation="false" OnClick="cancel_Click" />
                                                </td>
                                            </tr>
                                        </table>


update.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.IO;

public partial class update : System.Web.UI.Page
{
    string str = "Data Source=SANTOSHKLPKL-HP\\SQLEXPRESS;Initial Catalog=blood_bank;Integrated Security=True";
    SqlConnection con = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        tblogin.Visible = false;
        tbphn.Visible = false;
        updatedetails.Visible = false;
        updatephn.Visible = false;
        cancel.Visible = false;

        string uname = Session["Username"].ToString();
        lbname.Text = Session["Username"].ToString();
        con.ConnectionString = str;
        con.Open();
        SqlCommand cm = new SqlCommand("Select * from users where USER_NAME='"+uname+"'", con);
        SqlDataReader dr;
        dr = cm.ExecuteReader();
        if (dr.Read())
        {
           
            lbmail.Text = dr["MAIL_ID"].ToString();
            lbphn.Text = dr["PHONE_NUMBER"].ToString();
            con.Close();
        }
       
    }
   
    protected void Editmail_Click(object sender, EventArgs e)
    {
      
        tblogin.Visible = true;
        updatedetails.Visible = true;
        cancel.Visible = true;
       
    }
    protected void Editphn_Click(object sender, EventArgs e)
    {
        tbphn.Visible = true;
        updatephn.Visible = true;
        cancel.Visible = true;
       
    }
    protected void updatedetails_Click(object sender, EventArgs e)
    {
        con.ConnectionString = str;
        // “updatemail” is the procedure I had created where I wrote the query for update of mail.
        SqlCommand cm1 = new SqlCommand("updatemail", con);
        cm1.CommandType = CommandType.StoredProcedure;
        SqlParameter x1 = new SqlParameter("@uname", lbname.Text);
        SqlParameter x2 = new SqlParameter("@newmail", tblogin.Text.Trim());

        cm1.Parameters.Add(x1);
        cm1.Parameters.Add(x2);
        con.Open();

        SqlDataReader dr1;
        dr1 = cm1.ExecuteReader();
        con.Close();
        con.Open();
        cm1.ExecuteNonQuery();
        con.Close();
        Response.Redirect("./update.aspx");
    }
    protected void updatephn_Click(object sender, EventArgs e)
    {
        con.ConnectionString = str;
        // “updatephone” is the procedure where I wrote the query for updating phone number
        SqlCommand cm2 = new SqlCommand("updatphone", con);
        cm2.CommandType = CommandType.StoredProcedure;
        SqlParameter x1 = new SqlParameter("@uname", lbname.Text);
        SqlParameter x2 = new SqlParameter("@newphn", tbphn.Text.Trim());

        cm2.Parameters.Add(x1);
        cm2.Parameters.Add(x2);
        con.Open();

        SqlDataReader dr2;
       
            dr2 = cm2.ExecuteReader();
            con.Close();
            con.Open();
            cm2.ExecuteNonQuery();
            con.Close();
            Response.Redirect("./update.aspx");
       
    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("./Display.aspx");
    }
}



You can rate my class on top right side of my blog .....

                                                                    -Your's Santoshklpkl



Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget