private void btnNewAccount_Click(object sender, System.EventArgs e)
{
MainForm Acnt = new MainForm();
String Username = null, Password = null;
String ExistingUsername;
bool UniqueUsername = true;
if( Acnt.ShowDialog() == DialogResult.OK )
{
Username = Acnt.Username;
Password = Acnt.Password;
XmlTextReader xtr = new XmlTextReader("credentials.xml");
while(xtr.Read() )
{
switch(xtr.NodeType)
{
case XmlNodeType.Element:
ExistingUsername = xtr.GetAttribute("username");
if( Username.Equals(ExistingUsername) )
UniqueUsername = false;
break;
}
}
xtr.Close();
if( UniqueUsername == true )
{
// Declare an XmlDocument that will be used to add a new item
XmlDocument XmlDoc = new XmlDocument();
try
{
// Get the XML file and load it in the XmlDocument variable
XmlDoc.Load("credentials.xml");
// Create the new element
XmlElement Elm = XmlDoc.CreateElement("credential");
// Create its attributes
Elm.SetAttribute("username", Username);
Elm.SetAttribute("password", Password);
// Add the new element to the file...
XmlDoc.DocumentElement.AppendChild(Elm);
// ... and save the document
XmlDoc.Save("credentials.xml");
MessageBox.Show("The account has been created!" +
"\nYou can use your new account to login!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
MessageBox.Show("The username you provided is already being used!!!");
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
String strUsername = this.txtUsername.Text;
String strPassword = this.txtPassword.Text;
String Username, Password;
bool GoodLogin = false;
if( strUsername.Equals("") )
{
MessageBox.Show("Invalid or empty username." +
"\nPlease provide a username");
this.txtUsername.Focus();
return;
}
if( strPassword.Equals("") )
{
MessageBox.Show("Invalid or empty password." +
"\nPlease type a valid password");
this.txtPassword.Focus();
return;
}
XmlTextReader xtr = new XmlTextReader("credentials.xml");
while(xtr.Read() )
{
switch(xtr.NodeType)
{
case XmlNodeType.Element:
Username = xtr.GetAttribute("username");
Password = xtr.GetAttribute("password");
if( strUsername.Equals(Username) )
{
if( strPassword.Equals(Password) )
GoodLogin = true;
else
GoodLogin = false;
}
break;
}
}
if( GoodLogin == true )
{
MainForm FM = new MainForm();
this.Hide();
FM.ShowDialog();
Close();
}
else
MessageBox.Show("Invalid Credential");
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
Close();
}
|