Tuesday, 3 September 2013

Themes in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place TextBox on Page
5. Now Right-click on solution Explorer Click Add New Item and Select Skin file give a name and click Add and then click Ok. SkinFile.skin will be added to the App_Theme folder as shown below image.

6. Add Code to SkinFile.skin




<asp:TextBox  runat="server" BackColor="Blue" ></asp:TextBox>


7. In Apply_Theme.aspx Page Add Theme="SkinFile" as shown image below


8. Click F5 you will see TextBox with BackGround color Blue. 



Monday, 2 September 2013

Sending Email with Attachment 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

string path = FileUpload1.PostedFile.FileName.ToString();
        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; };
            if (File.Exists(path))
            {
                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment(path);
                mail.Attachments.Add(attachment);
            }
            smtp.Send(mail);
          
        }
        catch (Exception ex)
        {
            ex.ToString();
      
        }


    }

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");
    }
}

SQL server tricky questions