Click here to Skip to main content
Click here to Skip to main content

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

, 3 Mar 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

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

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:

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:

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

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

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

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

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)

Share

About the Author

HeldinBayoumy
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
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralWant to know whether Flash can interact with a remote database without server side script PinmemberAKMeenaChandrashekhar1-May-11 22:40 
Questionsending keys to flash Pinmemberbenjis25-Sep-08 21:44 
AnswerRe: sending keys to flash PinmemberHossam Aldin13-Dec-08 6:26 
Generalvery interesting Pinmembersadekz22-Jul-08 15:25 
Questionerror to import activeX flash Pinmemberricnelhu4-Jul-07 0:15 
QuestionTransparent ? Pinmemberhamid_m8-Mar-07 6:32 
GeneralFlash Resize Pinmemberbraber25-Jul-06 8:21 
QuestionPortable to Web? PinmemberDennis McMahon15-Mar-06 10:00 
AnswerRe: Portable to Web? PinmemberHossam Aldin15-Mar-06 12:38 
GeneralRe: Portable to Web? PinmemberDennis McMahon15-Mar-06 14:46 
GeneralFlash Movie Error Pinmember| Muhammad Waqas Butt |4-Mar-06 8:39 
GeneralRe: Flash Movie Error PinmemberHossam Aldin4-Mar-06 9:57 
GeneralRe: Flash Movie Error Pinmember| Muhammad Waqas Butt |4-Mar-06 10:03 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 4 Mar 2006
Article Copyright 2006 by HeldinBayoumy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid