Hello folks, I'm a little stuck and any help would be appreciated. Basically what I'm trying to do is have the user enter a pin or password, connect to a database and check whether that password is in there, if it is the program will switch to a new form. The problem is i don't know how to compare the query result with what the user has entered into the textbox. Below is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace FullProject
{
    public partial class Form1 : Form
    {
        //Making form 1 static so i can get back to it from form 2 and making pin public so form 2 can use it
        public static Form1 staticf = null;
        public static string ent = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //Only 6 characters can be entered in textbox
            textBox1.MaxLength = 6;
        }

        private void enter_Click(object sender, EventArgs e)
        {
            //ent now equals the textbox
            ent = textBox1.Text;

            //Opening connection to database and execute query
            MySqlConnection condatabase = new MySqlConnection("Data Source=localhost;" + "Persist Security Info=yes;" +
                "UserID = root; PWD= ;");
            MySqlCommand cmddatabase = new MySqlCommand("Use winc; Select pin From info Where pin = [box]" + ent);

            condatabase.Open();

            MySqlDataReader reader = cmddatabase.ExecuteReader();
            string res = cmddatabase.ToString;

            //If pin exists in database execute statement inside the if, ignore if condition as it was being used for testing
            if (textBox1.Text == "1234")
            {
                staticf = this;
                this.Hide();
                textBox1.Text = null;

                Form2 form2 = new Form2();
                form2.Show();
            }

            else
            {
                //Display if pin isnt found in database
                MessageBox.Show("Incorrect pin entered");
            }
        }
    }
}

Hai,

Instead of selecting the password first and then compare,
you can compare the username and password using sql query...

IF EXISTS(SELECT username FROM table1
	  WHERE username=txtUsername.text
          AND password=txtPassword.text
	 )
	 SELECT 1
ELSE
	 SELECT 0

This query returns 1 if such a user exists or 0 if not exists.
Hope this will help you...

Also, I would recommend checking my tutorial on accessing form controls/members from other forms.
Your 'ent' variable is specific to the current instance of the form, static members should be common across all instances. My tutorial shows how to use a property to access a private member. Properties are a better OO practice as they provide Encapsulation.

Having checked your code, however, i think you can eliminate the need to access the data on Form1 from Form2 by passing the user input to Form2 in its constructor:

public partial class Form2 : Form
{
    private string ent; //move your ent INTO form2
     
    public Form2(string UserEnteredText)    {
        InitializeComponent();
        ent = UserEnteredText;
    }
 }

  //then call it like this:

Form2 frm = new Form2(this.TextBox1.Text);
frm.Show();

EDIT: also, check out my new snippet to show how to show/hide a form when calling a new form

I'm sorry but i just don't understand where that if statement should go rohini.

As he said, the code given is an alternative SQL query, place it where your current query is.
Did you check any of the snippets i linked you to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.