Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / C#
Article

Interaction Between a Flash Movie and the Database through the Hosting C# Application

Rate me:
Please Sign up or sign in to vote.
4.07/5 (10 votes)
3 Mar 2006CPOL4 min read 90.8K   5.8K   55   13
An article that shows how to get data from and to flash movie, to and from the database through the hosting C# application, using Flash Variables and fscommand
Sample screenshot

Introduction

In this article, we will see how we can establish an interaction channel between Flash and the Database through C# Windows Application. Here, we will create a simple application that adds a user (username, password), and then uses this information to login. However, I'll start by mentioning the requirements for creating this application.

Requirements

You will need the Macromedia Flash plug-in installed on your system, and you will also need Macromedia Flash software if you are required to create the Flash movie. You will also need Microsoft SQL SERVER 2000 engine installed, or Microsoft SQL SERVER software installed if you will create the database. Now we will move to the steps for creating the application.

Inside Macromedia Flash

Here, we have 2 way traffic, from the Macromedia Flash Movie to the database (through C# Win App) and from the database to the Flash Movie (through C# Win App). To send data (text) from Flash to any hosting application, we could use the FScommand function which takes two parameters: Command and Parameters. On the other hand, to send data (text) to the Flash Movie from the outside environment, you will have to re-assign a Flash variable to the specific value you want to transmit. Let's see how we will do this. We will start with what we are going to do inside Macromedia Flash.

1. The Registration

Sample screenshot

You will need to create two input text fields in your Flash movie (describing how to do this is beyond the scope of this article) and a nice button. Assign each of the text fields an instance name, let's say txt_RegUserName and txt_RegPassword. Now, as I said above, we will use the FScommand since we are sending data out of the Flash Movie. So, we will include the following Actionscript in the on(release) event for our nice button.

C#
on(release){
//Here, we are sending all the data in the same Fscommand response, separating
//User name and password with "/"
fscommand("Reg", _parent. txt_RegUserName.text+"/"+ _parent. txt_RegPassword.text);
}

Here we sent the data (username and password) in one chunk of data separated by / to the C# application.

2. The Login

You will need to create two input text fields and a nice button as in the Registration Flash Form. Assign each of the text fields an instance name, let's say txt_LogUserName and txt_LogPassword. We will be sending data here too. So, we will include the following Actionscript in the on(release) event for our nice button.

C#
on(release){
//Here, we are sending all the data in the same Fscommand response, separating
//User name and password with "/"
fscommand("Log", _parent. txt_LogUserName.text+"/"+ _parent. txt_LogPassword.text);
}

So, I bet you are asking “Where the hell will we be getting data from the outside to Flash?”. Ok, don't yell, I'll tell you now.

3. Getting Data from the Outside

We can do this in both the Registration and the login as a confirmation of successful registration or a successful login. Anyway, we will be doing the part of a successful login (You can do the other part as a practice… easy practice, I guess). Here, we will be using a Flash variable to collect the data sent to the Flash as I mentioned above. So, we will start by defining a var in the Main movie (_root), let's say we name it IsValid (Is it a valid login), and initialize it with -1 (-1 for nothing, 0 for failure, 1 for success).

Sample screenshot

C#
var IsValid=-1;//Used to Indicate if this is valid login information

So, now after clicking the button, return a nice message telling what is going on based on the value of the IsValid variable:

C#
If(_root.IsValid==0){
Message.text="Failed Login";
//or maybe wrong user name or password.
} 

Inside Visual Studio .NET

Refer to my other article Sending Data from a Flash Movie to the Hosting C# Application for information on how to include a Flash movie in the C# Windows Form. So, after you add the Flash shockwave object to the form, name it axShockwaveFlashInterface. Now go to events and create a handler for the event Fscommand. Inside the Fscommand event handler, include the following code:

C#
bool Isvalid=false;
string username="";
string password="";
if(e.command=="Reg")
//then the user clicked the registration button
{
string arg=e.args;
int delemIndex=arg.IndexOf("/");
username=arg.Substring(0,delemIndex);
password=arg.Substring(delemIndex+1);
Isvalid= AddUser(username,password);
}
else if(e.command=="Log")
//then the user clicked the Login button
{
string arg=e.args; int delemIndex=arg.IndexOf("/");
username=arg.Substring(0,delemIndex);
password=arg.Substring(delemIndex+1);
Isvalid=IsUser(username,password);
}
if(Isvalid)
axShockwaveFlashInterface.SetVariable("IsValid","1");
else
axShockwaveFlashInterface.SetVariable("IsValid", "0");

In the code above, I handled both the messages of login and registration. You will find that I used the function SetVariable that takes two strings, the variable name and value. This function sets the value of a certain Flash variable to a certain value. So, using this function, you can send data into a Flash movie. You can also use the opposite function GetVariable which gets the value of a certain variable inside Flash. You can use it to get values out of the Flash movie (alternative to the fscommand method). Now, let's move to the database stuff.

Microsoft SQL SERVER 2000 Database

We do not need a complex database design here; it’s all about a 3 column table. We will call it Users, and will include the fields (UserID, UserName, and Password). Create the queries for adding new user, and checking on old users. They should be as follows:

1. AddUser Procedure

SQL
CREATE PROCEDURE AddUser

@UserID INT OUTPUT,
@UserName VARCHAR(50),
@Password VARCHAR(50)

AS

INSERT INTO users
(
   UserName,
   Password
)
VALUES
(
   @UserName,
   @Password
)

Select @UserID=@@identity

2. IsUser Procedure

SQL
CREATE PROCEDURE IsUser

@UserID INT OUTPUT,
@UserName VARCHAR(50),
@Password VARCHAR(50)

AS

SELECT @UserID=UserID

FROM users

WHERE

UserName=@UserName AND
Password = @Password 

Then we will call them in the functions as follows (sorry for the spaghetti structure, and for neglecting the multi tier design).

3. AddUser Function

C#
private bool AddUser(string UserName, string Password)
{
SqlConnection con =new SqlConnection("server=(local);
    Trusted_connection=true;database=Users");
SqlCommand cmd=new SqlCommand("AddUser",con);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter Param_UserID=new SqlParameter("@UserID",SqlDbType.Int);
Param_UserID.Direction=ParameterDirection.Output;
cmd.Parameters.Add(Param_UserID);
SqlParameter Param_UserName=new SqlParameter("@UserName",SqlDbType.VarChar);
Param_UserName.Value=UserName;
cmd.Parameters.Add(Param_UserName);
SqlParameter Param_Password=new SqlParameter("@Password",SqlDbType.VarChar);
Param_Password.Value=Password;
cmd.Parameters.Add(Param_Password);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
int.Parse(Param_UserID.Value.ToString());
}
catch(Exception t)
{
return false;
}
if(Param_UserID.Value.ToString()=="0")
return false;
else
return true; 

4. IsUser Function

C#
private bool IsUser(string UserName, string Password)
{
SqlConnection con =new SqlConnection("server=(local);
    Trusted_connection=true;database=Users");
SqlCommand cmd=new SqlCommand("IsUser",con);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter Param_UserID=new SqlParameter("@UserID",SqlDbType.Int);
Param_UserID.Direction=ParameterDirection.Output;
cmd.Parameters.Add(Param_UserID);
SqlParameter Param_UserName=new SqlParameter("@UserName",SqlDbType.VarChar);
Param_UserName.Value=UserName;
cmd.Parameters.Add(Param_UserName);
SqlParameter Param_Password=new SqlParameter("@Password",SqlDbType.VarChar);
Param_Password.Value=Password;
cmd.Parameters.Add(Param_Password);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
int.Parse(Param_UserID.Value.ToString());
}
catch(Exception t)
{

return false;
}
if(Param_UserID.Value.ToString()=="0")
return false;
else
return true;
} 

Finally

So, finally we have an application that connects to the database, sends data from and to a Flash movie. I am sorry for not handling all the cases here and for the bad code structure. However, I hope this works for you. See you soon.

History

  • 4th March, 2006: Initial post

License

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


Written By
Product Manager
Egypt Egypt
Agile Evangelist, Change Embracer and Success Facilitator. Project Manager and IT professional with 8+ years of leadership experience - http://www.hossamaldin.com

Comments and Discussions

 
GeneralWant to know whether Flash can interact with a remote database without server side script Pin
AKMeenaChandrashekhar1-May-11 21:40
AKMeenaChandrashekhar1-May-11 21:40 
Questionsending keys to flash Pin
benjis25-Sep-08 20:44
benjis25-Sep-08 20:44 
AnswerRe: sending keys to flash Pin
HeldinBayoumy13-Dec-08 5:26
professionalHeldinBayoumy13-Dec-08 5:26 
Generalvery interesting Pin
sadekz22-Jul-08 14:25
sadekz22-Jul-08 14:25 
Hi Hossam,

very interesting article indeed. and your explanation is clear and easy to follow.
I just wrote a very similar article yesterday. but I was explaining how to connect ASP.NET with Actionscript through XML.
here is the address if you are interested :

http://www.codeproject.com/KB/aspnet/aspnetactionscript.aspx[^]

keep up the good work

cheers
Abdalla Sadek
Questionerror to import activeX flash Pin
ricnelhu3-Jul-07 23:15
ricnelhu3-Jul-07 23:15 
QuestionTransparent ? Pin
hamid_m8-Mar-07 5:32
hamid_m8-Mar-07 5:32 
GeneralFlash Resize Pin
braber25-Jul-06 7:21
braber25-Jul-06 7:21 
QuestionPortable to Web? Pin
Dennis McMahon15-Mar-06 9:00
Dennis McMahon15-Mar-06 9:00 
AnswerRe: Portable to Web? Pin
HeldinBayoumy15-Mar-06 11:38
professionalHeldinBayoumy15-Mar-06 11:38 
GeneralRe: Portable to Web? Pin
Dennis McMahon15-Mar-06 13:46
Dennis McMahon15-Mar-06 13:46 
GeneralFlash Movie Error Pin
| Muhammad Waqas Butt |4-Mar-06 7:39
professional| Muhammad Waqas Butt |4-Mar-06 7:39 
GeneralRe: Flash Movie Error Pin
HeldinBayoumy4-Mar-06 8:57
professionalHeldinBayoumy4-Mar-06 8:57 
GeneralRe: Flash Movie Error Pin
| Muhammad Waqas Butt |4-Mar-06 9:03
professional| Muhammad Waqas Butt |4-Mar-06 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.