2023 Database Connection

 Login Page


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace Simple_Database_Connection

{

    public partial class Login : Form

    {

        public Login()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

          

            try

            {

                if (textBox1.Text == "admin" && textBox2.Text == "admin")

                {

                    Main fm = new Main();

                    fm.Show();

                    fm.label1.Text = textBox1.Text;

 

                    this.Hide();

                }

 

                if(!(textBox1.Text==string.Empty))

                {

                    if(!(textBox2.Text==string.Empty))

                    {

                        SqlConnection con = new SqlConnection("Data Source=ICTV34;Initial Catalog=ictpt23;Integrated Security=True");

 

                        con.Open();

                        SqlCommand cmd = new SqlCommand("select username, password from login where username='"

                            +textBox1.Text+"' and password='"+textBox2.Text+"'",con);

                        SqlDataReader dr = cmd.ExecuteReader();

                        int count = 0;

                        while(dr.Read())

                        {

                            count = count + 1;

                        }

                        if(count==1)

                        {

                            Main fm = new Main();

                            fm.Show();

                            fm.label1.Text = textBox1.Text;

                            fm.administratorToolStripMenuItem.Visible = false;

                            this.Hide();

                        }

                        else if(count>1)

                        {

                            MessageBox.Show("duplicate User name and password");

                        }

                        else

                        {

                            MessageBox.Show("User name password are incorrect");

                        }

                    }

 

                }

            }

           

            catch(Exception es)

            {

                MessageBox.Show(es.Message);

            }

           

           

           

           

            //if (textBox1.Text == "abc" && textBox2.Text == "123")

            //{

            //    Form2 fm = new Form2();

            //    fm.Show();

            //    this.Hide();

            //}

            //else

            //{

            //    MessageBox.Show("check your user name password");

            //}

        }

 

        private void label3_Click(object sender, EventArgs e)

        {

           

        }

        bool showpassword = false;

        private void label3_MouseDown(object sender, MouseEventArgs e)

        {

            if(e.Button==MouseButtons.Left)

            {

                showpassword = true;

                textBox2.PasswordChar = '\0';

            }

        }

 

        private void label3_MouseUp(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                showpassword = false;

                textBox2.PasswordChar = '*';

            }

        }

    }

}


MDI Form:





using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace Simple_Database_Connection

{

    public partial class Main : Form

    {

        public Main()

        {

            InitializeComponent();

        }

 

        private void studentRegistrationToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Form2 fm = new Form2();

            fm.MdiParent = this;

            fm.Show();

        }

 

        private void stuRegToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Form2 fm = new Form2();

            //fm.MdiParent = this;

            fm.Show();

        }

 

        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)

        {

            this.Close();

            Login ln = new Login();

            ln.Show();

        }

 

        private void administratorToolStripMenuItem_Click(object sender, EventArgs e)

        {

 

        }

    }

}

 Student Registration:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace Simple_Database_Connection

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=ICTV34;Initial Catalog=ictpt23;Integrated Security=True");

            con.Open();

            SqlCommand cmd = new SqlCommand("insert into student1 values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',@aaa,'"+comboBox1.Text+"','"+dateTimePicker1.Text+"')", con);

            if(radioButton1.Checked)

            {

                cmd.Parameters.AddWithValue("@aaa", "Male");

            }

            else

            {

                cmd.Parameters.AddWithValue("@aaa", "Female");

            }

            cmd.ExecuteNonQuery();

            MessageBox.Show("Insert Done");

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=ICTV34;Initial Catalog=ictpt23;Integrated Security=True");

            con.Open();

            SqlCommand cmd = new SqlCommand("update student1 set name ='" + textBox2.Text + "',email ='" + textBox3.Text + "', mobile ='" + textBox4.Text + "' where stuid = '" + textBox1.Text + "'", con);

            cmd.ExecuteNonQuery();

            MessageBox.Show("Update Done");

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=ICTV34;Initial Catalog=ictpt23;Integrated Security=True");

            con.Open();

            SqlCommand cmd = new SqlCommand("select * from student1 where stuid = '" + textBox1.Text + "'", con);

            SqlDataReader abc= cmd.ExecuteReader();

            while(abc.Read())

            {

            textBox2.Text=abc[1].ToString();

            textBox3.Text = abc[2].ToString();

            textBox4.Text = abc[3].ToString();

 

            }

        

        }

    }

}

 














 



Post a Comment

0 Comments