0

I've been trying to make a login form to my database for a mobile shop store (university project). I have an Employee table containing Emp_name and Emp_password as id and password to then access the database. This is the code but somehow I created the design and when I click on the log in button it doesn't write any message for me. Please tell me how to let this login form launch the database separately of mysql server

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 System.Data.Sql;
using System.Data.OleDb; 
using System.Data.SqlClient;

namespace login_form
{
public partial class Form1 : Form
{
    SqlConnection con = new SqlConnection();
    public Form1()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=RAYMOND-PC;Initial Catalog=[mobile shop   final];Integrated Security=True";

        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'sTUDENTDataSet.login' table. You can move, or remove it, as needed.  
        //this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);  
        SqlConnection con = new SqlConnection("Data Source=RAYMOND-PC;Initial Catalog= mobile shop final;Integrated Security=True");
        con.Open();

        {
        }
    }

    private void Button1_Click(object sender, EventArgs e)  
        {  
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=RAYMOND-PC;Initial Catalog=mobile shop final;Integrated Security=True";  
            con.Open();
            string Emp_name=textBox1.Text;
            string Emp_password = textBox2.Text ;
            SqlCommand cmd = new SqlCommand("select Emp_name , Emp_password from Employee where Emp_name='" + textBox1.Text + "'and Emp_password='" + textBox2.Text + "'", con);  
            SqlDataAdapter da = new SqlDataAdapter(cmd);  
            DataTable dt = new DataTable();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            da.Fill(dt);
            if (Emp_name == textBox1.Text && Emp_password==textBox2.Text)
            {  
                MessageBox.Show("Login sucess Welcome to Mobile shop");  
                System.Diagnostics.Process.Start("mobile shop final");  
            }  
            else  
            {  
                MessageBox.Show("Invalid Login please check username and password");  
            }  
            con.Close();  
        }

    private void Button2_Click(object sender, EventArgs e)
    {
        Application.Exit();

    }

    private void Label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click_1(object sender, EventArgs e)
    {

    }
}

}

Brendan
  • 1,237
  • 1
  • 16
  • 34
  • Welcome to Stack Overflow. Have you ever debug your code? Did you check your all values are correct? You should always use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/) by the way. This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. And don't store your passwords as a plain text. Read: http://stackoverflow.com/questions/1054022/best-way-to-store-password-in-database Don't forget to use `using` statement to dispose your database connections and objects. – Soner Gönül Dec 19 '14 at 07:29
  • @SonerGönül yes i debug it and when i enter the emp name and password nothing happened notting i added a connection by clicking on connect to data base in tools and added the tables in the data bindings of the maing login form – Raymond Hani Artin Dec 19 '14 at 07:32
  • @SonerGönül i'm still a beginner like i'm still learning c++ and data structures algorithms and oop but i'm trying to do this for my extra knowledge so can you please explain in details :D? – Raymond Hani Artin Dec 19 '14 at 07:34
  • remove `SqlConnection con = new SqlConnection();` from everywhere in the code except in the button1 click event and remove the connection string also; to restate, remove all your code except in button 1 and button 2 from your project form. You're opening up connections for no reason and not closing them. – prospector Dec 19 '14 at 07:39
  • but that won't help my code to connect any way so it's doesn't make a big problem i just want it to run ... any ideas ? :D – Raymond Hani Artin Dec 19 '14 at 16:51

1 Answers1

0

Use MySqlConnectionStringBuilder to create connection string and PropertyGrid control to enter values. This will guarantee that every option is correct.

i486
  • 6,491
  • 4
  • 24
  • 41
  • would you please explaining with code details ? cause i'm still beginner :D?? – Raymond Hani Artin Dec 19 '14 at 16:47
  • Instead of simple string assignment like "Data Source=...", define `MySqlConnectionStringBuilder` object and use it for connection string: `MySqlConnectionStringBuilder mcs = new MySqlConnectionStringBuilder()`. To get the real connection string use `mcs.ConnectionString` property. Put a `PropertyGrid` control in your form and assign its `SelectedObject` to `mcs`. This will be editor for all possible MySQL connection options. Note: `MySqlConnectionStringBuilder` requires reference to `MySql.Data`. – i486 Dec 19 '14 at 22:19