Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have tried this code and it is not working. The YouTube video path is inserted but the video is not displayed.

Database table script:

SQL
USE [videosample]
GO
/****** Object:  Table [dbo].[Table_1]    Script Date: 05/14/2014 15:53:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_1](
	[ID] [int] NULL,
	[VLink] [varchar](max) NULL,
	[UserID] [varchar](50) NULL,
	[LikeCount] [int] NULL,
	[ViewCount] [nchar](10) NULL,
	[UploadDate] [date] NULL,
	[Image] [varchar](max) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO


Code behind for Default2.aspx.cs:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    string s1 = ConfigurationManager.ConnectionStrings["db"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(s1);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Table_1 (userID,VLink,UploadDate,Image)values(@UserID,@VLink,@UploadDate,@Image)", con);
        string s = TextBox2.Text;
        string g = "embed/";
        string o = "watch?v=";
        string m = s.Replace(o, g);
        string[] arrUrl = s.Split('=');
        string h = arrUrl[arrUrl.GetUpperBound(0)];
        string r = "http://img.youtube.com/vi" + h + "/0.jpg";
        cmd.Parameters.AddWithValue("@VLink", m);

        cmd.Parameters.AddWithValue("@UserID", TextBox1.Text);

        cmd.Parameters.AddWithValue("@UploadDate", DateTime.Now);
        cmd.Parameters.AddWithValue("@Image", r);

        cmd.ExecuteNonQuery();
        con.Close();

        Label1.Text = "Upload sucess";
        Label1.ForeColor = System.Drawing.Color.Green;

        TextBox1.Text = "";
        TextBox2.Text = "";




    }
   

   
    protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (e.CommandName == "Redirect")
            {

                Session["ID"] = e.CommandArgument;
                Response.Redirect("Default2.aspx");
            }
        }
    }
    protected void DataList1_ItemCommand(object sender, EventArgs e)
    {

    }
}


ASPX:

XML
<asp:Label ID="Label1" runat="server" Text="Enter username"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Enter url"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" style="margin-top: 49px"
            Width="357px"></asp:TextBox>
        <asp:Label ID="Label3" runat="server"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
        <br />
        <br />
        <br />
    <asp:DataList id="DataList1" runat ="server" RepeatDirection ="Horizontal"
            RepeatColumns ="3" OnItemCommand ="DataList1_ItemCommand"
            onselectedindexchanged="DataList1_ItemCommand">
    <ItemTemplate>
    <div>
        <table>
                <tr>
                    <td>
                            <asp:LinkButton runat="server" ID="linkbutton1" CommandArgument='<%#Eval("ID") %>' CommandName="Redirect">
                            <asp:Image ID="Image1" ImageUrl='<%#Eval("Image") %>' runat="server" Height="150px" Width="300px" />

                            </asp:LinkButton>

                      </td>
                </tr>
                <tr>
                    <td>
                        posted By:
                        <asp:Label runat="server" ID="sample"><%#Eval("UserID") %></asp:Label>
                        On:
                        <asp:Label runat="server" ID="posteddate"><%#Eval("UploadDate") %></asp:Label>
                    </td>
                </tr>
           </table>
    </div>
    </ItemTemplate>
    </asp:DataList>



Code behind for Default3.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Default3 : System.Web.UI.Page
{
    string s1 = ConfigurationManager.ConnectionStrings["db"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(s1);
        con.Open();
        SqlCommand cmd = new SqlCommand("select VLink from Table_1 where ID='" + Session["ID"].ToString() + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataList1.DataSource = ds;
            DataList1.DataBind();
        }
    }
    protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (e.CommandName == "Redirect")
            {

                Session["ID"] = e.CommandArgument;
                Response.Redirect("Default3.aspx");
            }
        }
    }
}


ASPX:

XML
<asp:DataList ID="DataList1" runat="server" RepeatColumns="Horizontal"  OnSelectedIndexChanged="DataList1_ItemCommand">

 <ItemTemplate>
 <iframe runat="server" id="detail" src='<%#Eval("VLink") %>'  height="420px" width="490px"></iframe>
 </ItemTemplate>
 </asp:DataList>
Posted
Updated 20-May-14 0:30am
v6
Comments
DamithSL 14-May-14 6:51am    
do you have values for ID column in your database? where you set ID in your insert code?
DamithSL 14-May-14 8:25am    
go to Default3.aspx.cs and put a breakpoint at the start of the page load method. run your application on debug mode and check the session value and data return from sql adapter.
and try with
SqlCommand cmd = new SqlCommand("select VLink from Table_1 where ID=" + Session["ID"].ToString() + "", con);
ZurdoDev 14-May-14 8:21am    
Why did you include the sql? You need to clean up this question and post just the relevant code so we can make sense of where you are stuck.
CHill60 20-May-14 6:32am    
I have changed you title from ALL CAPITALS to lower case. All capitals is considered rude and will put people off from looking at your question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900