Sunday, July 8, 2012

My new blog/forum ProgramFreaks.com

Join my new blog/forum www.ProgramFreaks.com - Da Vincis of CODE......

Saturday, July 31, 2010

YouTube videos on ASP.NET page with AJAX

For embedding YouTube videos on ASP.NET web pages we need Literal control, DropDownList (its optional)  and of course code, here the code is in C#.


see Screen Shot:

Front End: File name : Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
        <br />
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div style="width: 649px; position: relative; top: -1px; left: 177px">
                    <br />
                    <br />
                    <asp:DropDownList ID="lstSelectVideo" runat="server" AutoPostBack="True" Height="16px"
                        Width="220px" OnSelectedIndexChanged="lstSelectVideo_SelectedIndexChanged">
                        <asp:ListItem Selected="True" Value="66TuSJo4dZM">Inception Movie Trailer</asp:ListItem>
                        <asp:ListItem Value="C6RU5y2fU6s">The Expandables Trailer </asp:ListItem>
                        <asp:ListItem Value="RXZY_XRjABs">Despicable Me Trailer </asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <!--This control is to display video -->
                    <asp:Literal ID="litVideo" runat="server"></asp:Literal>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


 


Back end file name : Default.aspx.cs
//****************************************************************************
// Module           :   Default.aspx.cs
// Type             :   ASP.NET web page code behind
// Developer        :   Kirtisagar Malshet (Suraj)
// DateCreated      :   07/31/2010
// LastModified     :   07/31/2010


//****************************************************************************
// TERMS OF USE     :   ALL YouTube CONTENT IS SHOWN AS DEMO SAMPLE ONLY
//                  :   You can use it at your sole risk
//****************************************************************************

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

public partial class _Default : System.Web.UI.Page
{
    VideoExtractor ve = new VideoExtractor(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        litVideo.Text = ve.getVideo(lstSelectVideo.SelectedValue);
    }

      
    protected void lstSelectVideo_SelectedIndexChanged(object sender, EventArgs e)
    {
               
       litVideo.Text= ve.getVideo(lstSelectVideo.SelectedValue);

     }
}


Code file: VideoExtractor.cs
//****************************************************************************
// Module           :   VideoExtractor.cs
// Type             :   ASP.NET code 
// Developer        :   Kirtisagar Malshet (Suraj)
// DateCreated      :   07/31/2010
// LastModified     :   07/31/2010


//****************************************************************************
// TERMS OF USE     :   ALL YouTube CONTENT IS SHOWN AS DEMO SAMPLE ONLY
//                  :   You can use it at your sole risk
//****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

/// 
/// Summary description for VideoExtractor
/// 
public class VideoExtractor
{
  public string getVideo(string strVideoId)
    {
       
        StringBuilder strb = new StringBuilder();
        strb.Append(@"<embed src='http://www.youtube.com/v/");

        // add video id
        strb.Append(strVideoId);

        strb.Append("&rel=0");

        // allow full screen
        strb.Append("&fs=1");

        // closing single quote after parameter list
        strb.Append("' ");
        
        strb.Append("type='application/x-shockwave-flash' ");

        // add id
        strb.Append("id='youTubePlayer" + DateTime.Now.Millisecond.ToString() + "' ");

        strb.Append("allowscriptaccess='always' enableJavascript ='false' ");

        // set parameters: allowfullscreen
        strb.Append("allowfullscreen='true' ");

        // set width 16:9 Ratio
        strb.Append("width='" + "640" + "' ");

        // set height
        strb.Append("height='" + "360" + "' ");
        
        strb.Append(@"></embed>");

        // get final script
       string vid = strb.ToString();

       return vid;
       

    }
}



Download the complete source code here

Friday, April 30, 2010

"Modified" Rotating ads automatically for every 10 minutes in ASP.Net C#

Found some solution to write compact code for the Rotating ads. This topic is to explain how well we can code this is called CLEVER programming.
In the previous topic there is If -Else ladder,



Now,
if (time == 0)
      tempTime = 1;
   else
       tempTime = (int)(Math.Floor((double)((time - 1) / 10)))+1;




Here Math.Floor cannot be used on integer so converting integer to double explicitly is necessary and then whole expression is converted to integer explicitly because "tempTime" is a integer variable,

The above code is replacement over this:

if (time >= 00 && time <= 10) //
              tempTime = 1; 
            else
                if (time >= 11 && time <= 20)
                    tempTime = 2;
                else
                    if (time >= 21 && time <= 30)
                        tempTime = 3;
                    else
                        if (time >= 31 && time <= 40)
                            tempTime = 4;
                        else
                            if (time >= 41 && time <= 50)
                                tempTime = 5;
                            else
                                   tempTime = 6;









Monday, April 5, 2010

Sending Email from your website, Feed back form, ASP.NET, C#

I was thinking to make the email part better in ByJoel.com, before it was just a link to Microsoft Outlook. I've added the email form and set up the SMTP, now I can receive feedback directly to my inbox. see here : www.byjoel.com/FeedBack.aspx



Sending feed back email WebForm controls:
I have built the User Interface using ASP.Net WebForm controls as shown above. As you can see there are four text boxes, one multi line text box and a Submit button. I have also applied proper ASP.Net Validation controls to avoid errors. 
The "Name", "Email" and "Subject" text boxes are normal text boxes where user can insert the values. but "To" text box has read only property and email address of our web admin, so that user can not change the "To" address.      


Code: 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MailMessage objEmail = new MailMessage();
        objEmail.To = txtTo.Text;
        objEmail.From = txtEmail.Text;
        objEmail.Subject = txtSub.Text;
        objEmail.Body = "Name: " + txtName.Text + "

" + "Comments: " + txtComments.Text; objEmail.Priority = MailPriority.High; objEmail.BodyFormat = MailFormat.Html; // Make sure you have appropriate replying permissions from your local system. // or contact your web hosting and get the SMTP server name, I have added our SMTP server name SmtpMail.SmtpServer = "localhost"; try { SmtpMail.Send(objEmail); Response.Redirect("OnSale.aspx"); //for testing you can add Response.Write("Feed back sent"); //But it is not advised for working website, So i have redirected to other page. } catch (Exception exc) { Response.Redirect("Default.aspx"); //here exception object created but never used, //it can be used as Response.Write("Send failure: " + exc.ToString()); } }

Saturday, March 13, 2010

Rotating ads automatically for every 10 minutes in ASP.Net C#

I got an assignment to add a feature that rotates ads on ByJoel.com home page. This website is built on ASP.net and items displayed are not only just images but also information stored in database.


Whats the need to do this??
- Because when person visits www.ByJoel.com he see one set of items and if he hit the home page again after 10 minutes he sees different items in different departments. This features keeps the website refreshed with different items every 10 minutes As this is a E-commerce website so this feature is useful to attract customers

Logic:
There is UserControl file which sets the number of items to be displayed on home page ex: OnSale.ascx.cs, (C sharp codes are in separate file)
There is variable "howMany" which sets the number of items,


if (howMany == 8) 

How to fetch the time from server and extracting minutes and converting it to String??

DateTime tm = DateTime.Now;  // fetch the server time
curtime = String.Format("{0:mm}", tm); //select only minutes Note: "curtime" variable has to declared in this class 
time = Convert.ToInt32(curtime);  // converting to string note: "time" variable declared before.








Declare another string variable called tempTime.

if (time >= 00 && time <= 10) // time between 1 to 10 minutes at any hour. 
              tempTime = 1;
            else






                if (time >= 11 && time <= 20)
                    tempTime = 2;
                else
                    if (time >= 21 && time <= 30)
                        tempTime = 3;
                    else
                        if (time >= 31 && time <= 40)
                            tempTime = 4;
                        else
                            if (time >= 41 && time <= 50)
                                tempTime = 5;
                            else
                                   tempTime = 6;
        

Add time variables are in Access.cs file, this file used to access stored procedure for our website.

Then go to stored procedure and add the queries for different time slot. you can add any query to pull the desired items, TOP(8) means we will get only top 8 rows.

ex:
IF (@tempTime = 1)
 SELECT ASIN, StockStatus, ShowOnHomePage
 FROM OnSale
 WHERE (ShowOnHomePage > 0)
 ORDER BY ShowOnHomePage
 
IF (@tempTime = 2)
 SELECT TOP (8) DepartmentID, ASIN, Page, StockStatus
 FROM OnSale
 WHERE (DepartmentID = 1) AND (Page = 4)
 ORDER BY StockStatus DESC  
 
IF (@tempTime = 3)
 SELECT TOP (8) DepartmentID, ASIN, StockStatus
 FROM OnSale
 WHERE (DepartmentID = 2)
 ORDER BY StockStatus DESC
 
IF (@tempTime = 4)
 SELECT TOP (8) DepartmentID, ASIN, Page, StockStatus
 FROM OnSale
 WHERE (DepartmentID = 1) AND (Page = 5)
 ORDER BY StockStatus DESC
 
IF (@tempTime = 5)
 SELECT TOP (8) DepartmentID, ASIN, Page, StockStatus 
 FROM OnSale
 WHERE (DepartmentID = 1) AND (Page = 2) AND (SubPage = 3)
 ORDER BY StockStatus DESC
 
IF (@tempTime = 6)
 SELECT TOP (8) ASIN,  StockStatus
 FROM OnSale
 WHERE(TopPicks > 0)
 ORDER BY StockStatus DESC




Out put:
first 10 minutes:




Next 10 minutes :

Friday, March 5, 2010

ASP.NET chart control

I was thinking about how to report data in the form of charts for our project, Finally I found ASP.net chart control that renders in image format and its highly customizable. see here http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx



Ads by Amazon