Programming for Everybody: c# visual studio
Showing posts with label c# visual studio. Show all posts
Showing posts with label c# visual studio. Show all posts

c# tutorial for beginners: How to connect Microsoft access database to C#

c# tutorial for beginners: Connect access database (connection-insert-update-delete - search in database) with create access database and add images .
Subscribe to @programmingforeverybody
https://www.youtube.com/@programmingcode2025/?sub_confirmation=1
Contents:
Add controls to your form 00:00
Delete last blank row in datagridview 09:26
Browse button 09:42
Create table database 14:21
Add Dataset and relate it with controls 15:37
change columns header text datagridview 20:52
New button 23:16
Format DateTime column in a DataGridView 25:04
Save or update button 26:17
Search button 29:34
Reset button 32:26
Remove button 33:24
Previous button 34:16
Next button 34:48
First button 35:06
Last button 35:25
Close button 35:41

 



C# Login Form with Multiple Users & Permissions with SQL Server | C# Tutorial With Source code

 Source Code:

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 login_form

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=database6;Integrated Security=True");

        public static string user_v;

        private void checkBox1_CheckedChanged(object sender, EventArgs e)

        {

            if(checkBox1.Checked)

            {

                textBox1.UseSystemPasswordChar = false;

            }

            else

            {

                textBox1.UseSystemPasswordChar = true;

            }

        }


        private void button2_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            conn.Open();

            SqlCommand cmd = new SqlCommand("Select username,password From users Where username='Admin' And password=@pass", conn);

            cmd.Parameters.AddWithValue("pass", textBox1.Text);

            SqlDataReader myreader;

            myreader = cmd.ExecuteReader();

            if(myreader.Read())

            {

                int resultcomp = String.Compare(textBox1.Text, myreader.GetValue(1).ToString());

                if(resultcomp==0)

                {

                    user_v = myreader["username"].ToString();

                    Form2 gg = new Form2();

                    gg.Show();

                    this.Hide();

                    conn.Close();

                }

                else

                {

                    conn.Close();

                    MessageBox.Show("Error username or password");

                }

            }

            else

            {

                conn.Close();

                MessageBox.Show("Error username or password");

            }

        }


        private void checkBox2_CheckedChanged(object sender, EventArgs e)

        {

            if (checkBox2.Checked)

            {

                textBox2.UseSystemPasswordChar = false;

            }

            else

            {

                textBox2.UseSystemPasswordChar = true;

            }

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            fill_combo();

        }

        private void fill_combo()

        {

            SqlCommand cmd2 = new SqlCommand("Select id,username From users Where username<>'Admin'", conn);

            SqlDataAdapter da = new SqlDataAdapter(cmd2);

            DataTable dt = new DataTable();

            da.Fill(dt);

            comboBox1.DataSource = dt;

            comboBox1.DisplayMember = "username";

            comboBox1.ValueMember = "id";

            comboBox1.SelectedIndex = -1;

            comboBox1.Text = "Select user";

        }


        private void button3_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }


        private void button4_Click(object sender, EventArgs e)

        {

            if (comboBox1.SelectedValue==null)

            {

                MessageBox.Show("Please Select user");

            }

            else

            {

            conn.Open();

            SqlCommand cmd = new SqlCommand("Select * From users Where id=@id And password=@pass", conn);

                cmd.Parameters.AddWithValue("id", comboBox1.SelectedValue);

                cmd.Parameters.AddWithValue("pass", textBox2.Text);

            SqlDataReader myreader;

            myreader = cmd.ExecuteReader();

            if (myreader.Read())

            {

                int resultcomp = String.Compare(textBox2.Text, myreader.GetValue(2).ToString());

                if (resultcomp == 0)

                {

                    user_v = myreader["username"].ToString();

                        string add_v, update_v, delete_v;

                        add_v = Convert.ToString(myreader["add_data"].ToString());

                        update_v = Convert.ToString(myreader["update_data"].ToString());

                        delete_v = Convert.ToString(myreader["delete_data"].ToString());

                        Form2 gg = new Form2();


                        if (add_v == "True")

                            gg.add_btn.Visible = true;

                        else

                            gg.add_btn.Visible = false;


                        if (update_v == "True")

                            gg.update_btn.Visible = true;

                        else

                            gg.update_btn.Visible = false;


                        if (delete_v == "True")

                            gg.delete_btn.Visible = true;

                        else

                            gg.delete_btn.Visible = false;




                        gg.Show();

                    this.Hide();

                    conn.Close();

                }

                else

                {

                    conn.Close();

                    MessageBox.Show("Error username or password");

                }

            }

            else

            {

                conn.Close();

                MessageBox.Show("Error username or password");

            }

        }

    }

    }

}