මේක වැඩ කරනවා මම developed කරේ ,ඒ වගේම test කරලා බැලුවා කිහිප සැරයක්ම
හොදට වැඩ කරනවා.

නමුත් පිළිවෙලට යන්න මොකද Validation System එක ඒ තරම් හොද මදි.
ඔයාට ප්රශ්ණයක් තියෙනවා නම් කියන්න මම බලන්නම් මොකද කරන්න පුලුවන් කියලා.
මුලින්ම Database එකක් හදාගන්න මේක තියෙන්නේ SQL වලින්
Access කරන්න ඕන නම් SQL -> OleDb කරන්න
එකේ connection string එක මෙතනට දාන්න
connectionStr = ""
එච්චරයි තියෙන්නේ ඒ වගේම මේක වැඩ කරන්නේ Online තියෙනවා නම් විතරයි
මොකද ActiveX player එක Firefox,Opera,Safari වල වැඩ
කරන්නේ නැති නිසා වෙනම flashplayer එකක් use කරන්න වුනා මට.
මේ file 2ක් විතරයි ඕන වෙන්නේ. ඒ වගේම මේකේ database එකේ
columns හදනවා Automatically ඒකේ තියෙන්නේ id එක සහා path එක
විතරයි.
මුලින්ම යන්න මේ විදියට
Create database එක ඔබන්න එතනින් database එක හදනවා Application එකෙන්ම
ඊට පස්සේ Browse කියන button එක මගින් අදාළ file එක select කරලා
ඊට එහා පැත්තේ තියෙන Save button එකෙන් upload කරගන්න මේකෙන් වැඩ
දෙකක් වෙනවා එකක් තමයි file එක upload වෙන එක අනෙක database එකට
අදාළ file එකේ server path එක යන එක.
ඊට පස්සේ සෑහෙන තරම් records database එකේ තියෙන කොට Retrieve button
එක ඔබලා අදාළ files වල path ටික ගන්න පුලුවන්. දැන් මේ file එකක්
play කරගන්න ඕන නම් කෙළවරේම තියෙන listbox එකේ ඕන file එක
select කරලා Select කියන button එක ඔබන්න ඒකෙන් mp3 player එක
draw කරනවා HTML එක මත දැන් play කියන button එක ඔබලා music එක
අහන්න.
sound.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class sound : System.Web.UI.Page
{
String connectionStr = "";
protected void Page_Load(object sender, EventArgs e)
{
}
//############ EVENTS ###################################################################
protected void Retrieve(object sender, EventArgs e)
{
loadAllSoundFiles();
}
protected void createDatabase(object sender, EventArgs e)
{
proCreateTheDatabase();
}
protected void saveFile(object sender, EventArgs e)
{
proUploadFile();
}
protected void audioSelect_Click(object sender, EventArgs e)
{
string getSoundFile = allAudioFiles.SelectedValue;
makeMp3Player(getSoundFile);
}
//############################################################################################
//################## PROCEDURES ##############################################################
//<---------- Create The Database ------>
public void proCreateTheDatabase()
{
SqlConnection con = new SqlConnection(connectionStr);
String sqlCommand = "CREATE TABLE sound(soundID INT NOT NULL,soundPath VARCHAR(200) NOT NULL,PRIMARY KEY(soundID))";
SqlCommand cmd = new SqlCommand(sqlCommand, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script type='text/javascript'>alert('Database is created by Snowl.!');</script>");
}
catch (Exception error)
{
Response.Write("<script type='text/javascript'>alert('There is an error, perhaps you already made it mister.!');</script>");
}
}
//<---------- Upload The File to the Server ------>
public void proUploadFile()
{
String filePath = soundUpload.PostedFile.FileName;
if (!filePath.Equals("") && !filePath.Equals(null))
{
String serverPath = Server.MapPath("~/" + filePath.ToString());
proUpdateDatabase(serverPath);
soundUpload.PostedFile.SaveAs(serverPath);
}
else
{
Response.Write("<script type='text/javascript'>alert('You must first select a file to upload.! :P');</script>");
}
}
//<---------- Update The Database ------>
public void proUpdateDatabase(String filePath)
{
SqlConnection con = new SqlConnection(connectionStr);
String sqlCommand = "INSERT INTO sound(soundID,soundPath) VALUES(@soundID,@soundPath)";
SqlCommand cmd = new SqlCommand(sqlCommand, con);
int previousID = proGetPreviousId();
int newId = previousID + 1;
cmd.Parameters.AddWithValue("@soundID",newId);
cmd.Parameters.AddWithValue("@soundPath", filePath);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script type='text/javascript'>alert('Record is created by Snowl.!');</script>");
}
catch (Exception error)
{
Response.Write("<script type='text/javascript'>alert('There is an error in updating the database.');</script>");
}
}
//<------------ Get previous id's number --------->
public int proGetPreviousId()
{
int takeId = 0;
SqlConnection con = new SqlConnection(connectionStr);
String sqlCommand = "SELECT soundID FROM sound";
SqlCommand cmd = new SqlCommand(sqlCommand,con);
try
{
con.Open();
SqlDataReader read = cmd.ExecuteReader();
while(read.Read())
{
takeId = int.Parse(read["soundID"].ToString());
}
con.Close();
int lastId = takeId;
return lastId;
}
catch (Exception error)
{
return 0; //This means there is NO value so start from the beginning
}
}
// <---------- Retrieve the Sound file and put it with the mp3 player ---------->
public void makeMp3Player(string soundFileServerPath)
{
String fileToBePlayed = "";
if (soundFileServerPath.Contains("http://"))
{
int start = soundFileServerPath.LastIndexOf("//");
fileToBePlayed = soundFileServerPath.Substring(start+1);
}
else
{
int start = soundFileServerPath.LastIndexOf("\\");
fileToBePlayed = soundFileServerPath.Substring(start+1);
}
//Example of Local URL c:\file\mpfile.mp3
String mp3PlayerCommands = "<object type='application/x-shockwave-flash' data='http://flash-mp3-player.net/medias/player_mp3.swf' width='200' height='20'>" +
"<param name='movie' value='http://flash-mp3-player.net/medias/player_mp3.swf' />" +
"<param name='bgcolor' value='#ffffff' />" +
"<param name='FlashVars' value='mp3=" + fileToBePlayed + "' />" +
"</object>";
mp3PlayerDrawHere.InnerHtml = mp3PlayerCommands;
}
public void loadAllSoundFiles()
{
allAudioFiles.Items.Clear();//Clear All Previous items from the item List
SqlConnection con = new SqlConnection(connectionStr);
String sqlCommand = "SELECT soundPath FROM sound";
SqlCommand cmd = new SqlCommand(sqlCommand, con);
try
{
con.Open();
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
allAudioFiles.Items.Add(read["soundPath"].ToString());
}
con.Close();
}
catch (Exception error)
{
Response.Write("<script type='text/javascript'>alert('There is an error in searching in the database.');</script>");
}
}
//#############################################################################################
}
sound.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sound.aspx.cs" Inherits="sound" %>
<!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>
<script language="javascript" type="text/javascript">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnRetrieve" runat="server" onclick="Retrieve"
Text="Retrieve" />
<asp:FileUpload ID="soundUpload" runat="server" />
<asp:Button ID="btncreateDatabase" runat="server" onclick="createDatabase"
Text="Create The Database" />
<asp:Button ID="btnsaveFile" runat="server" onclick="saveFile"
Text="Save File" />
<asp:DropDownList ID="allAudioFiles" runat="server">
</asp:DropDownList>
<asp:Button ID="audioSelect" runat="server" onclick="audioSelect_Click"
Text="Select File" />
</div>
</form>
<div id="mp3PlayerDrawHere" runat="server">
</div>
</body>
</html>
//Snowl//