Monday, 2 September 2013

Sending Email in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Design UI like below image



5. Add namespaces
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;

6. Write Code on Send Button Click Event

 try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(txtto.Text);
            mail.From = new MailAddress("sapna@gmail.com");// your email
            mail.Subject = txtsubject.Text;

            string Body = null;
            Body = txtbody.Text;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Credentials = new System.Net.NetworkCredential("sapna@gmail.com", "123456");//your email and password
            smtp.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            smtp.Send(mail);
          
        }
        catch (Exception ex)
        {
            ex.ToString();
      
        }


Sunday, 1 September 2013

FileUpload Example in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place FileUpload Control and Button on Page
5. Create Folder in Website to Save File



6. Write Code on Upload Button Click Event
 protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string path=Server.MapPath("img/") + FileUpload1.PostedFile.FileName;
            FileUpload1.SaveAs(path);
        }
    }

7. Refresh Website and See Uploaded File in img Folder.

PlaceHolder Example (Dynamic Generate Controls ) in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place PlaceHolder Control on Page
5. Write Code on Page_Load  Event
 protected void Page_Load(object sender, EventArgs e)
    {
        Button btn = new Button();
        btn.Text = "This is Dynamically generated button";
        pl1.Controls.Add(btn);

    }

Multiview and View Example in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place MultiView on a page and two View inside Multiview Control
5. Set ActiveViewIndex Property of MultiView to 0




6. Add Code to Click Event of Next Button

 protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }

7. Add Code to Click Event of the Previous Button

    protected void Button4_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }

8. Full code

MultiviewDemo.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style3
        {
            width: 265px;
        }
        .style4
        {
            width: 271px;
        }
        .style5
        {
            width: 105px;
        }
        .style6
        {
            width: 125px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
            <asp:View ID="View1" runat="server">
                <table class="style1">
                    <tr>
                        <td class="style5">
                            Login Form</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td class="style5">
                            Username</td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" Width="216px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="style5">
                            Password</td>
                        <td>
                            <asp:TextBox ID="TextBox2" runat="server" Width="216px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="style5">
                            <asp:Button ID="Button1" runat="server" Text="Login" />
                        </td>
                        <td>
                            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Next" />
                        </td>
                    </tr>
                </table>
            </asp:View>
            <br />
            <asp:View ID="View2" runat="server">
                <table class="style1">
                    <tr>
                        <td class="style6">
                            Registration form</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td class="style6">
                            Name</td>
                        <td>
                            <asp:TextBox ID="TextBox3" runat="server" Width="216px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="style6">
                            Email</td>
                        <td>
                            <asp:TextBox ID="TextBox4" runat="server" Width="216px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="style6">
                            Address</td>
                        <td>
                            <asp:TextBox ID="TextBox5" runat="server" Width="216px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="style6">
                            Phone</td>
                        <td>
                            <asp:TextBox ID="TextBox6" runat="server" Width="216px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="style6">
                            <asp:Button ID="Button3" runat="server" Text="Submit" />
                        </td>
                        <td>
                            <asp:Button ID="Button4" runat="server" onclick="Button4_Click" 
                                Text="Previous" />
                        </td>
                    </tr>
                    <tr>
                        <td class="style6">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                </table>
            </asp:View>
        </asp:MultiView>
    </div>
    </form>
</body>
</html> 


MultiviewDemo.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
}



DatePicker in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place TextBox, Button and Calendar Control



5. Write Code on Page_Load Event
 protected void Page_Load(object sender, EventArgs e)
    {
        Calendar1.Visible = false;
    }

6. Write Code on Button Click Event

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (Calendar1.Visible == false)
        {
            Calendar1.Visible = true;
        }
        else
        {
            Calendar1.Visible = false;
        }
    }

7. Write Code on Calendar Event
 protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtdate.Text=Calendar1.SelectedDate.ToString("dd-MM-yyyy");
    }


8.Full Code
DatePicker.aspx Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 227px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                <asp:TextBox ID="txtdate" runat="server" Width="173px"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="..." />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Calendar ID="Calendar1" runat="server" 
                    onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    <div>
    
    </div>
    </form>
</body>
</html>






DatePicker..aspx.cs Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DatePicker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calendar1.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Calendar1.Visible == false)
        {
            Calendar1.Visible = true;
        }
        else
        {
            Calendar1.Visible = false;
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtdate.Text=Calendar1.SelectedDate.ToString("dd-MM-yyyy");
    }
}

Addition of 2 numbers in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page




4. Add Code to Click Event of Button Addition

 protected void Button1_Click(object sender, EventArgs e)

    {
        Label1.Text(Convert.ToInt32(txtnum1.Text)+Convert.ToInt32(txtnum2.Text)).ToString();

    }


ListBox Example

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place ListBox, TextBox and two Buttons on Page as the shown image below:



5. Add Code to Click Event of Button Add

 protected void Button1_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Add(txtitem.Text);
        txtitem.Text = "";
    }

6. Add Code to Click Event of Button Remove

 protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Remove(ListBox1.Items[ListBox1.SelectedIndex]);
    }

SQL server tricky questions