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:
USE [videosample]
GO
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:
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:
<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
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:
<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>