Upload youtube video and video thumbnail in database using asp.net
Create database like this
<article class="module width_full">
<header><h3> Add Video</h3></header>
<div class="row">
<div class="col-lg-12">
<div class="module_content">
<table class="xx" cellpadding="5px" style="line-height:30px">
<colgroup>
<col width="25%" />
<col width="25%" />
<col width="25%" />
<col width="25%" />
</colgroup>
<tr>
<td colspan="2">
<span style="font-size:20PX">Video</span>
</td>
<td colspan="2" align="right">
<asp:Label runat="server" ID="lbltime"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:HiddenField runat="server" ID="hdf1" />
</td>
</tr>
<tr>
<td>
Id
</td>
<td>
<asp:TextBox runat="server" ID="txtid" Enabled="false"></asp:TextBox>
</td>
<td>
Name
</td>
<td>
<asp:TextBox runat="server" ID="txtname"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td valign="top">
Video Value
</td>
<td valign="top">
<asp:TextBox runat="server" ID="txtvalue"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtvalue" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
<td valign="top">
Discription
</td>
<td valign="top">
<asp:TextBox runat="server" ID="txtdisc" TextMode="MultiLine" Height="100px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
</td>
<td colspan="2" align="right">
<asp:Button runat="server" ID="btnsave" CssClass="btn" Text="Save"
onclick="btnsave_Click" />
<asp:Button runat="server" ID="btnupdate" CssClass="btn" Text="Update"
onclick="btnupdate_Click" />
</td>
</tr>
<tr>
<td colspan="4">
<asp:Label runat="server" ID="lblmsg" ForeColor="Red" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td colspan="4" align="center">
<asp:GridView ID="GrdEmp" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" AllowPaging="True" class="table table-striped table-bordered table-hover"
onpageindexchanging="GrdEmp_PageIndexChanging"
onrowdeleting="GrdEmp_RowDeleting"
onselectedindexchanged="GrdEmp_SelectedIndexChanged" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Horizontal">
<Columns>
<asp:TemplateField HeaderText="Select" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LnkSelect" runat="server" CausesValidation="False"
CommandName="Select" Text="Select" CommandArgument='<%#Bind("id")%>' ToolTip="Edit Here"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="LnkDelete" OnClientClick="javascript:return confirm('Are you sure want to delete?')" runat="server" CausesValidation="true" CommandName="Delete" Text="Delete" ImageUrl="img/Cancel.jpg" CommandArgument='<%#Bind("id")%>' Height="18px" ToolTip="Delete"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" >
<ItemTemplate>
<asp:Image runat="server" ID="img" ImageUrl='<%#Bind("Image")%>' Width="150"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Title" DataField="title" />
<asp:BoundField HeaderText="Value" DataField="value" />
<asp:BoundField HeaderText="Description" DataField="Desci" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</div>
</div>
</article>
</section>
code behind C#
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Net;
public partial class Admin_AddVideo : System.Web.UI.Page
{
private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
bindgrd(); DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
bindgrd();
lbltime.Text = indianTime.ToString("h:mm:tt");
}
public string getYouTubeThumbnail(string YoutubeUrl)
{
string youTubeThumb = string.Empty;
if (YoutubeUrl == "")
return "";
if (YoutubeUrl.IndexOf("=") > 0)
{
youTubeThumb = YoutubeUrl.Split('=')[1];
}
else if (YoutubeUrl.IndexOf("/v/") > 0)
{
string strVideoCode = YoutubeUrl.Substring(YoutubeUrl.IndexOf("/v/") + 3);
int ind = strVideoCode.IndexOf("?");
youTubeThumb = strVideoCode.Substring(0, ind == -1 ? strVideoCode.Length : ind);
}
else if (YoutubeUrl.IndexOf('/') < 6)
{
youTubeThumb = YoutubeUrl.Split('/')[3];
}
else if (YoutubeUrl.IndexOf('/') > 6)
{
youTubeThumb = YoutubeUrl.Split('/')[1];
}
return "http://img.youtube.com/vi/" + youTubeThumb + "/mqdefault.jpg";
}
void uploadvideo()
{
try
{
con.Open();
string thumbnail = getYouTubeThumbnail("https://www.youtube.com/v/" + txtvalue.Text+"").ToString();
SqlCommand cmd = new SqlCommand("insert into tbl_video values (@title,@Image, @value, @Desci)", con);
cmd.Parameters.AddWithValue("@title", txtname.Text);
cmd.Parameters.AddWithValue("@Image", thumbnail.ToString());
cmd.Parameters.AddWithValue("@value", "https://www.youtube.com/v/" + txtvalue.Text + "?version=3&loop=1&playlist=" + txtvalue.Text);
cmd.Parameters.AddWithValue("@Desci", txtdisc.Text);
cmd.ExecuteNonQuery();
lblmsg.Text = "Video Has Been Uploaded";
con.Close();
}
catch (Exception ex)
{
}
}
void bindgrd()
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_Video order by id desc", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GrdEmp.DataSource = dt;
GrdEmp.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
GrdEmp.DataSource = dt;
GrdEmp.DataBind();
int columncount = GrdEmp.Rows[0].Cells.Count;
GrdEmp.Rows[0].Cells.Clear();
GrdEmp.Rows[0].Cells.Add(new TableCell());
GrdEmp.Rows[0].Cells[0].ColumnSpan = columncount;
GrdEmp.Rows[0].Cells[0].Text = "There is no record in your DataBase..";
}
con.Close();
}
catch (Exception ex)
{
}
}
protected void GrdEmp_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
btnsave.Enabled = false;
LinkButton LnkSelect = (LinkButton)GrdEmp.SelectedRow.FindControl("LnkSelect");
string id = LnkSelect.CommandArgument.ToString();
hdf1.Value = id;
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tbl_video where id = " + id + "", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtid.Text = dt.Rows[0]["id"].ToString();
txtname.Text = dt.Rows[0]["title"].ToString();
txtvalue.Text = dt.Rows[0]["value"].ToString();
txtdisc.Text = dt.Rows[0]["Desci"].ToString();
// txtJdate.Text = dt.Rows[0]["Em_Joiningdate"].ToString();
//txtSlary.Text = dt.Rows[0]["jt"].ToString();
//ddlPosition.Items[0].Value= dt.Rows[0]["Position"].ToString();
}
else
{
lblmsg.Text = "There is no record..";
}
con.Close();
}
catch (Exception ex)
{
//lblmsg.ForeColor = Color.Red;
lblmsg.Text = "Error..!!!";
}
finally
{
con.Close();
}
}
protected void GrdEmp_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
GridViewRow grdRow = (GridViewRow)GrdEmp.Rows[e.RowIndex];
ImageButton LnkDelete = (ImageButton)grdRow.FindControl("LnkDelete");
con.Open();
string id = LnkDelete.CommandArgument.ToString();
SqlCommand cmd = new SqlCommand("delete from tbl_video where id = " + id + "", con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
// lblmsg.ForeColor = Color.Red; ;
lblmsg.Text = "Record has been deleted";
}
con.Close();
bindgrd();
}
catch (Exception ex)
{
//lblmsg.ForeColor = Color.Red; ;
lblmsg.Text = "Error";
}
finally
{
con.Close();
}
}
protected void GrdEmp_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GrdEmp.PageIndex = e.NewPageIndex;
bindgrd();
}
void update()
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("update tbl_video set name=N'" + txtname.Text + "', value='" + txtvalue.Text + "', time='" + lbltime.Text + "' where id = '" + txtid.Text + "'", con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblmsg.Text = "Record Has been Updated";
}
con.Close();
}
catch (Exception ex)
{
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
uploadvideo();
bindgrd();
txtname.Text = "";
txtvalue.Text = "";
}
protected void btnupdate_Click(object sender, EventArgs e)
{
update();
bindgrd();
txtvalue.Text = "";
txtname.Text = "";
}
void ClearInputs(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
((TextBox)ctrl).Text = string.Empty;
ClearInputs(ctrl.Controls);
}
}
}
This code help to save Youtube video thumbnail and video to sql server .
It is also very useful article
ReplyDelete