• (3 Pages)
  • +
  • 1
  • 2
  • 3

C# Login/Register system using SQL Compact

#16 xaragk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-October 13

Posted 03 October 2013 - 08:17 AM

this is a really well written article with much useful details..im new in c# so forgive me if i say anythng stypid.. im facing a problem which is forcing me to continue...
At this point: "and drag the Logins table from the Data Sources menu onto the tab. The table in which you are now looking at is called the DataGridView. On the DataGrid, click the menu arrow on the top right of it, and click the “Dock in parent container” button. This will fill your tab with the DataGrid."
i cant get which table i should drag...at the data sources i only have "loginsDataSet" which is not able to drag..(to drag it i just click the mouse and drag it right??)
Also i cant see any DataGridView or DataGrid but i suppose this is normal cause i havent done the previous step with the drag and drop...so i cant continue my project unfortunately...i would appreciate if u could help me..i also attach a printscreen if it helps you at all..thanks! Attached Image
Was This Post Helpful? 0
  • +
  • -

#17 bacileios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-October 13

Posted 16 October 2013 - 09:41 AM

 

private void loginButton_Click(object sender, EventArgs e)
{
    //Locals to hold values
    string username = loginUserName.Text;
    string password = HashPass(loginPassword.Text);


    //Loop through database
    foreach (DataRow row in loginsDataSet.Logins)
    {
        //And search for Username and Pass that match
        if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))
        {
            loginUserName.Text = String.Empty;
            loginPassword.Text = String.Empty;
            MessageBox.Show("Login Success");
            break;
        }
        //If not, then show this message.
        else
        {
            MessageBox.Show("Username/Password incorrect");
            break;
        }
    }
} 








Does anyone tested this ? I did it and it searches only the 1st Row for valid user/pass and then doesnt bother to search the rest of the table , it just gets into else shows the messabox and breaks, so basically if you dont have a valid user/pass in the fisrt row that code becomes useless? Do i missed something there?



I did this instead to make it work properly, maybe try/catch can work too?

 

            Boolean logfail = true;

            foreach (DataRow row in eofficeDataSet.admin)
            {
                if (row.ItemArray[1].Equals(username) && row.ItemArray[2].Equals(password))
                {
                    textBox1.Text = String.Empty;
                    textBox2.Text = String.Empty;
                    MessageBox.Show("Login Success");
                    logfail = false;
                    fmenu f2 = new fmenu();
                    f2.Show();    
                    break;
                }
            }

                   if (logfail==true)
                {

                     MessageBox.Show("Username/Password incorrect");
                }
                








Also when i was doing register the data was not saved in the database , adding this line

this.tableAdapterManager.UpdateAll(this.eofficeDataSet);

to get my new entry saved.


Do i missed something when i did the original code because i m really new to this stuff ...
Was This Post Helpful? 0
  • +
  • -

#18 TechCP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 13

Posted 19 December 2013 - 02:35 PM

I'm getting an errroorrrrr

Posted Image
Was This Post Helpful? 0
  • +
  • -

#19 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 12:52 PM

I have followed this post down to a T. I did not finish the whole thing just yet but I just finished adding the code for the click action on the register button. I have two errors and I am wondering what I am doing wrong. here is the code I have so far:
"
using System;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace LoginSystem
{
    public partial class Form1 : Form

    {
        public Form1()

        {
            InitializeComponent();

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void loginsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.loginsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.loginsDataSet);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'loginsDataSet.Logins' table. You can move, or remove it, as needed.
            this.loginsTableAdapter.Fill(this.loginsDataSet.Logins);

        }

        private void registerButton_Click(object sender, EventArgs e)
        {
	    AddUser(registerUserName.Text, registerPassword.Text, registerConfirmPassword.Text, registerEmail.Text);
        }

        private void AddUser(string username, string password, string confirmPass, string email)
        {

            //Local variables to hold values
            string smtpEmail = smtpUserNameTextBox.Text;
            string smtpPassword = smtpPasswordTextBox.Text;
            int smtpPort = (int)smtpPortNumericUD.Value;
            string smtpAddress = smtpAddressTextBox.Text;

            //Regex for making sure Email is valid
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);

            //Loop through Logins table
            foreach (DataRow row in loginsDataSet.Logins)
            {

                //And look for matching usernames
                if (row.ItemArray[0].Equals(username))
                {
                    //If one is found, show message:
                    MessageBox.Show("Username already exists");
                    return;
                }
            }

            //Confirm pass must equal password.
            if (password != confirmPass)
            {
                MessageBox.Show("Passwords do not match");
            }
            //Password must be at least 6 characters long
            else if (password.Length < 6)
            {
                MessageBox.Show("Password must be at least 6 characters long");
            }
            //If email is NOT valid
            else if(!match.Success)
            {
                MessageBox.Show("Invalid Email");
            }
            //If there is no usrename
            else if (username == null)
            {
                MessageBox.Show("Must have Username");
            }
            //If all is well, create the new user!
            else
            {
                loginsDataSet.LoginsRow newUserRow = loginsDataSet.Logins.NewLoginsRow();
                
                string EncryptedPass = HashPass(password);
                    
                newUserRow.Username = username;
                newUserRow.Password = EncryptedPass;
                newUserRow.Email = email;

                loginsDataSet.Logins.Rows.Add(newUserRow);
                registerUserName.Text = String.Empty;
                registerPassword.Text = String.Empty;
                registerConfirmPassword.Text = String.Empty;
                registerEmail.Text = String.Empty;
                MessageBox.Show("Thank you for registering!");
                if(String.IsNullOrWhiteSpace(smtpEmail) || String.IsNullOrWhiteSpace(smtpPassword) || String.IsNullOrWhiteSpace(smtpAddress) || smtpPort <= 0)
                {
                    MessageBox.Show("Email Configuration is not set up Correctly! \nCannot send email!");
                }
                else
                {
                    SendMessage(email.ToString(), username.ToString(), password.ToString());
                }
            }
          
        }
        public void SendMessage(string ToAddress, string ToName, string password)
        {
            var client = new SmtpClient(smtpAddressTextBox.Text, (int)smtpPortNumericUD.Value)
            {
                Credentials = new NetworkCredential(smtpUserNameTextBox.Text, smtpPasswordTextBox.Text),
                EnableSsl = true
            };
            client.Send(smtpUserNameTextBox.Text, ToAddress, "Thank You!", "Thank you for registering with us today! \n Your username/passwords are: \n \nUsername: "ToName.ToString() + "\nPassword: " + password.ToString(); 
        }
        public string HashPass(string password)
        {
            SHA256 sha = new SHA256CryptoServiceProvider();

            //compute hash from the bytes of text
            sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email));

            //gethash result after compute it
            byte[] result = sha.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }
    }
}

"

This post has been edited by modi123_1: 21 March 2014 - 01:10 PM
Reason for edit:: in the future - use the code tags button

Was This Post Helpful? 0
  • +
  • -

#20 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Posted 21 March 2014 - 01:11 PM

Quote

I have two errors

It always helps to tell folks your errors (as in the entire error text). Don't leave them guessing!
Was This Post Helpful? 0
  • +
  • -

#21 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 01:16 PM

The errors i am having are:
1 ) expected
2 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
3 The name 'email' does not exist in the current context.
Was This Post Helpful? 0
  • +
  • -

#22 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Posted 21 March 2014 - 01:25 PM

Quote

1 ) expected

As it is saying - you have a '(' but no closing ')' somewhere. Check back through your code.
I would look around line 132.



Quote

3 The name 'email' does not exist in the current context.


There seems to be some issues with your understanding of 'scope'.

134	        public string HashPass(string password)
...
139	            sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email));


Where is 'email' declared in scope of the function "hashpass"?



With 'adduser' you had it as a parameter.
051	        private void AddUser(string username, string password, string confirmPass, string email)

.. and I certainly do not see it declared as a global.
Was This Post Helpful? 0
  • +
  • -

#23 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 01:33 PM

That is the code that was provided. I am a newb at sql and was just looking for some guidance. To answer you question on line 132 there were no ()in the code provided.
email has not been declared globally in the code provided and I notice it is declared inside the else part of the add user. on line 104. and that is the only reference to the email string.
I am just confused.

This post has been edited by Curtis Rutland: 21 March 2014 - 01:40 PM
Reason for edit:: please do not quote the previous post in full. use quotes to call out sections of a comment.

Was This Post Helpful? 0
  • +
  • -

#24 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Posted 21 March 2014 - 01:39 PM

Quote

That is the code that was provided. I am a newb at sql and was just looking for some guidance. To answer you question on line 132 there were no ()in the code provided.

That is flat out wrong.

From the OP:
09	    client.Send(smtpUserNameTextBox.Text, ToAddress, "Thank You!", "Thank you for registering with us today! \n Your username/passwords are: \n \nUsername: "
10	                                                                + ToName.ToString() + "\nPassword: " + password.ToString());


There are the right amount of ( and ).



Quote

email has not been declared globally in the code provided and I notice it is declared inside the else part of the add user. on line 104. and that is the only reference to the email string.

So why would you believe the scope of one method would extend to another?
Was This Post Helpful? 0
  • +
  • -

#25 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Posted 21 March 2014 - 02:18 PM

Quote

I am a newb at sql and was just looking for some guidance.


Yes, however the issues you've listed have nothing to do with SQL; they're all C# syntax and usage errors.

Quote

To answer you question on line 132 there were no ()in the code provided.


I'm not sure where the misunderstanding is here. In the code you provided, this is line 132:

client.Send(smtpUserNameTextBox.Text, ToAddress, "Thank You!", "Thank you for registering with us today! \n Your username/passwords are: \n \nUsername: "ToName.ToString() + "\nPassword: " + password.ToString();


There are clearly several parenthesis in that statement. Try this: count the number of "(" and ")" in that expression. You should notice that there is one opening parenthesis that doesn't have a matching close parenthesis.

I suspect fixing the first might clear up the second, because syntax errors have a way of causing more than one error. If not, please let us know the line number (or just quote the line) that it says the error is on. Please remember to give us all the information we need to help you. We always want to know the exact error text, the line that threw the error, and what you expect should be happening. Actually, every forum is going to want that. It will help you get better help if people don't have to ask you for more information.

If you're unfamiliar with scope, I suggest reading this doc:

http://msdn.microsof...2(v=vs.71).aspx

But the basic point is, everything has a scope that it belongs to. Class-level variable's scope is the class it's defined in. Local variables' scopes are the methods they're defined in. You cannot reference these variables "out of scope". Which means that if in one method, you declare a variable, another method cannot access that variable. So as modi123_1 suggests, you can't refer to email in your HashPass method, since you never defined it as a parameter or local variable. Likely you will need to add it as a parameter and pass it in whenever you call that method.
Was This Post Helpful? 0
  • +
  • -

#26 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 04:33 PM

This is gold... very good explanations, Thank You! very much guys.
I meant C# when I said i am a newb. I am a newb at this whole IT deal but that is besides the point. It has been a really long day, I am sorry.

To answer to your previous post when I add the closing bracket at the end of line 123 the error remains unchanged. i suspect this has something to do with the fact that the email variable is not declared. I understand that I have to declare it but I am unsure on how to declare a string variable in a piece of code that uses smtp throughout it. This is literally the first program ever for me in C#. I am trying to get it to work just so I can understand it better. I read the article you provided and that is very good for me.
So far yall's forum is by far the best written c# code I have dealt with. reading through the msdn's from Microsoft is brutal and watching it on youtube is like trying to keep up with a freight train at full speed.

Anyway this is what I have right now:

using System;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace LoginSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void loginsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.loginsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.loginsDataSet);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'loginsDataSet.Logins' table. You can move, or remove it, as needed.
            this.loginsTableAdapter.Fill(this.loginsDataSet.Logins);

        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            AddUser(registerUserName.Text, registerPassword.Text, registerConfirmPassword.Text, registerEmail.Text);
        }

        private void AddUser(string username, string password, string confirmPass, string email)
        {

            //Local variables to hold values
            string smtpEmail = smtpUserNameTextBox.Text;
            string smtpPassword = smtpPasswordTextBox.Text;
            int smtpPort = (int)smtpPortNumericUD.Value;
            string smtpAddress = smtpAddressTextBox.Text;

            //Regex for making sure Email is valid
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);

            //Loop through Logins table
            foreach (DataRow row in loginsDataSet.Logins)
            {

                //And look for matching usernames
                if (row.ItemArray[0].Equals(username))
                {
                    //If one is found, show message:
                    MessageBox.Show("Username already exists");
                    return;
                }
            }

            //Confirm pass must equal password.
            if (password != confirmPass)
            {
                MessageBox.Show("Passwords do not match");
            }
            //Password must be at least 6 characters long
            else if (password.Length < 6)
            {
                MessageBox.Show("Password must be at least 6 characters long");
            }
            //If email is NOT valid
            else if (!match.Success)
            {
                MessageBox.Show("Invalid Email");
            }
            //If there is no usrename
            else if (username == null)
            {
                MessageBox.Show("Must have Username");
            }
            //If all is well, create the new user!
            else
            {
                loginsDataSet.LoginsRow newUserRow = loginsDataSet.Logins.NewLoginsRow();

                string EncryptedPass = HashPass(password);

                newUserRow.Username = username;
                newUserRow.Password = EncryptedPass;
                newUserRow.Email = email;

                loginsDataSet.Logins.Rows.Add(newUserRow);
                registerUserName.Text = String.Empty;
                registerPassword.Text = String.Empty;
                registerConfirmPassword.Text = String.Empty;
                registerEmail.Text = String.Empty;
                MessageBox.Show("Thank you for registering!");
                if (String.IsNullOrWhiteSpace(smtpEmail) || String.IsNullOrWhiteSpace(smtpPassword) || String.IsNullOrWhiteSpace(smtpAddress) || smtpPort <= 0)
                {
                    MessageBox.Show("Email Configuration is not set up Correctly! \nCannot send email!");
                }
                else
                {
                    SendMessage(email.ToString(), username.ToString(), password.ToString());
                }
            }

        }
        public void SendMessage(string ToAddress, string ToName, string password)
        {
            var client = new SmtpClient(smtpAddressTextBox.Text, (int)smtpPortNumericUD.Value)
            {
                Credentials = new NetworkCredential(smtpUserNameTextBox.Text, smtpPasswordTextBox.Text),
                EnableSsl = true
            };
            client.Send(smtpUserNameTextBox.Text, ToAddress, "Thank You!", "Thank you for registering with us today! \n Your username/passwords are: \n \nUsername: "ToName.ToString() + "\nPassword: " + password.ToString()); 
        }
        public string HashPass(string password)
        {
            SHA256 sha = new SHA256CryptoServiceProvider();

            //compute hash from the bytes of text
            sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password + email));

            //gethash result after compute it
            byte[] result = sha.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }
    }
}



The errors are the same
Was This Post Helpful? 0
  • +
  • -

#27 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 04:40 PM

if I rewrite line 133 like this:

[code]
public string HashPass(string password, string email)
[\code]

I get these errors:

1 )expected
2 Invalid expression term ')'
3 ;expected
4 No overload for method 'HashPass takes 1 arguments
Was This Post Helpful? 0
  • +
  • -

#28 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Posted 21 March 2014 - 05:16 PM

Just copying and pasting your code I see that you have a missing plus sign around here:

    are: \n \nUsername: "ToName.ToString() + 

Was This Post Helpful? 0
  • +
  • -

#29 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 05:23 PM

View Postandrewsw, on 21 March 2014 - 05:16 PM, said:

Just copying and pasting your code I see that you have a missing plus sign around here:

    are: \n \nUsername: "ToName.ToString() + 


Probably about 20 seconds before you posted I figured out how to use the variable email. That error is fixed now

I declared on line 49 like this

string email;
        private void AddUser(string username, string password, string confirmPass, string email)



Now I have to figure out what is wrong in the line you mentioned.
Was This Post Helpful? 0
  • +
  • -

#30 addanny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 14

Posted 21 March 2014 - 05:53 PM

I got it. It works awesome. Thank You all. I was missing a + sign you were right.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3