Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.Net Music Player
    text
    copied!<p>I was following a guide to create a simple music directory.</p> <p>It reads from a folder and put the music names on a grid, then add a "play button" on grid that should send the file to a player.</p> <p>here is the code behind:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.IO; using System.Text; namespace WebApplication8 { public partial class MusicPlayer : System.Web.UI.Page { public string mySrc; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getSongName(); } } public void getSongName() { DirectoryInfo di = new DirectoryInfo("your music directory here"); if (di.Exists == true) { DataTable dt = new DataTable(); dt.Columns.Add("SongName"); foreach (FileInfo fi in di.GetFiles("*.mp3", SearchOption.AllDirectories))//get filename from this directory { DataRow dr = dt.NewRow(); dr["SongName"] = fi.Name;//show only song name excluding path dt.Rows.Add(dr); } grid.DataSource = dt; grid.DataBind(); } } protected void grid_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);"; e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);"; } protected void grid_SelectedIndexChanged(object sender, EventArgs e) { DirectoryInfo di = new DirectoryInfo("your music directory here"); mySrc = di + grid.SelectedDataKey.Values["SongName"].ToString(); } } } </code></pre> <p>and here is the front page code: </p> <pre><code> %@ Page Language="C#" AutoEventWireup="true" CodeBehind="MusicPlayer.aspx.cs" Inherits="WebApplication8.MusicPlayer" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript"&gt; var oldgridSelectedColor; function setMouseOverColor(element) { oldgridSelectedColor = element.style.backgroundColor; element.style.backgroundColor = '#dcdcdc'; element.style.cursor = 'hand'; } function setMouseOutColor(element) { element.style.backgroundColor = oldgridSelectedColor; element.style.textDecoration = 'none'; } &lt;/script&gt; &lt;style type="text/css"&gt; &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt; &lt;/asp:ScriptManager&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False"&gt; &lt;Triggers&gt; &lt;asp:AsyncPostBackTrigger ControlID="grid" EventName="Load"&gt;&lt;/asp:AsyncPostBackTrigger&gt; &lt;/Triggers&gt; &lt;ContentTemplate&gt; &lt;object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" id="player" class="musicplayer-media"&gt; &lt;param name="url" value="&lt;% =mySrc %&gt;" /&gt; &lt;param name="src" value="&lt;% =mySrc %&gt;" /&gt; &lt;param name="name" value="musicplayer" /&gt; &lt;param name="showcontrols" value="true" /&gt; &lt;param name="animationatStart" value="true" /&gt; &lt;param name="volume" value="0" /&gt; &lt;!--[if !IE]&gt;--&gt; &lt;object type="video/x-ms-wmv" data="&lt;% =mySrc %&gt;" class="musicplayer-media"&gt; &lt;param name="src" value="&lt;% =mySrc %&gt;" /&gt; &lt;param name="name" value="musicplayer" /&gt; &lt;param name="autostart" value="true" /&gt; &lt;param name="controller" value="true" /&gt; &lt;param name="volume" value="0" /&gt; &lt;/object&gt; &lt;!--&lt;![endif]--&gt; &lt;/object&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:GridView ID="grid" runat="server" BackColor="#CC9900" ForeColor="#663300" OnRowDataBound="grid_RowDataBound" OnSelectedIndexChanged="grid_SelectedIndexChanged" DataKeyNames="songname"&gt; &lt;Columns&gt; &lt;asp:ButtonField CommandName="Select" Text="Play" /&gt; &lt;/Columns&gt; &lt;SelectedRowStyle BackColor="#66CCFF" BorderColor="Black" BorderStyle="Solid" /&gt; &lt;/asp:GridView&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The issue i'm having is this: I can load the content on a page and show then, but when I click on play button on gridview, it gives me an arror:</p> <pre><code>mySrc = di + grid.SelectedDataKey.Values["SongName"].ToString(); NullReferenceException was unhandled by user code. </code></pre> <p>I can see that mySrc is returning a null value, but when I "look" inside di and grid.selected, there are values in there.</p> <p>Can someone help me with this and tell me what Am I doing wrong?</p> <p>Thanks in advance and sorry about my poor english, its not my native language.</p> <p><strong>Edit:</strong> I found out that on my front page code, the KeyDataName was set as "songname", I changed it to SongName to match the code behind, and now it dont give me an error anymore, but just dont do anything. I click on play and nothing happens.</p> <p>Thanks again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload