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
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){
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){
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).

var IsValid=-1;
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”;
}
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")
{
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")
{
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 string
s, 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