Monday, February 8, 2010

Login program in silverlight/ silverlight database connection

Dears,
Here a sample program to log in into a website using silverlight ....
We can't connect a database directly with silverlight, we use webservices to connect a silverlight
with database.
////silverlight
using System.Windows.Browser;
using System.Windows.Data;

using System.ServiceModel;
using SilverlightApplication.ServiceReference1;

private void btnlogin_Click(object sender, RoutedEventArgs e)
{
if (txt1.Text != "" && pwd.Password != "")
{
services.checkLoginCompleted += new System.EventHandler(logincheckcompleted);
services.checkLoginAsync(txt1.Text, pwd.Password);
}
else { MessageBox.Show("Username and Password shouldnot be blank"); }
}

public void logincheckcompleted(object sender, checkLoginCompletedEventArgs e)
{
if (e.Result != 0)
{
App appeg = (App)Application.Current;
appeg.clientid = Convert.ToInt32(e.Result);
//It's alternative for session, we can re-use the app.xaml variable in page1.xml also
Page1 p = new Page1();
this.Content = p;
}
else
{
MessageBox.Show("Invalid User Credentials");
}
}
////Webservice
[WebMethod]
public int checkLogin(string username, string password)
{
int retvalue=0;
retvalue =(int) frmclass.GetDBValue("select clientid from clientdetails where username='" + username + "' and password='" + password + "'");
return retvalue;
}
////class- here all database related functions lies.

public object GetDBValue(string sqlQuery)
{
object TheValue;
TheValue = 0;
OpenConnection();
db_Command.Connection = db_Connection;
db_Command.CommandText = sqlQuery;
db_Command.CommandType = CommandType.Text;
db_Reader = db_Command.ExecuteReader();
if (db_Reader.Read())
{
if (db_Reader[0] == null)
{ TheValue = 0; }
else
{ TheValue = db_Reader[0]; }
}
CloseConnection();
return TheValue;
}
public void OpenConnection()
{ try
{
if (db_Connection.State != ConnectionState.Open)
{ db_Connection.Open(); }
}
catch (Exception)
{ }
}
public void CloseConnection()
{
if (db_Connection.State != ConnectionState.Closed)
{ db_Connection.Close(); }
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home