Programming for Everybody

C# tutorial: search, update and delete buttons with(all data types) With SQL Server with 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 Retrieve_data_from_Sql_server_database

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }


        private void Form2_Load(object sender, EventArgs e)

        {

            dateTimePicker1.Format = DateTimePickerFormat.Custom;

            dateTimePicker1.CustomFormat = " ";

        }


        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection conn1 = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=true");

            conn1.Open();

            SqlCommand cmd1 = new SqlCommand("Select firstname,lastname,phone,gender,date_birth,image1 From Table1 Where id=@idpar", conn1);

            cmd1.Parameters.AddWithValue("idpar", idtxt.Text.Trim());

            SqlDataReader reader1;

            reader1 = cmd1.ExecuteReader();

            if (reader1.Read())

            {

                ftxt.Text = reader1["firstname"].ToString();

                ltxt.Text = reader1["lastname"].ToString();

                phtxt.Text = reader1["phone"].ToString();

                dateTimePicker1.Format = DateTimePickerFormat.Custom;

                dateTimePicker1.CustomFormat = "MM-dd-yyyy";

                dateTimePicker1.Text = reader1["date_birth"].ToString();

                pictureBox1.Image = Image.FromFile(reader1["image1"].ToString());

                Boolean gender_v = (bool)reader1["gender"];

                if (gender_v==true)

                {

                    m1.Checked = true;

                }

                else

                {

                    f1.Checked = true;

                }

            }

            else

            {

                ftxt.Text = "";

                ltxt.Text = "";

                phtxt.Text = "";

                dateTimePicker1.Format = DateTimePickerFormat.Custom;

                dateTimePicker1.CustomFormat = " ";

                pictureBox1.Image = null;

                m1.Checked = false;

                f1.Checked = false;

                MessageBox.Show("No data found");

            }

            conn1.Close();

        }


        private void browse_btn_Click(object sender, EventArgs e)

        {

            openFileDialog1.Filter = "Image|*.jpg;png";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

            else

                pictureBox1.Image = null;

        }


        private void update_btn_Click(object sender, EventArgs e)

        {

            SqlConnection conn2 = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=true");

            SqlCommand cmd2 = new SqlCommand("Update Table1 Set firstname=@fname,lastname=@lname,phone=@phone,gender=@gender,image1=@image,date_birth=@dateb Where id=@id", conn2);

            cmd2.Parameters.AddWithValue("fname", ftxt.Text.Trim());

            cmd2.Parameters.AddWithValue("lname", ltxt.Text.Trim());

            cmd2.Parameters.AddWithValue("phone", phtxt.Text.Trim());

            int gg;

            if (m1.Checked==true)

            {

                gg = 1;

            }

            else

            {

                gg = 0;

            }

            cmd2.Parameters.AddWithValue("gender", gg);

            cmd2.Parameters.AddWithValue("image", openFileDialog1.FileName);

            cmd2.Parameters.AddWithValue("dateb", dateTimePicker1.Value);

            cmd2.Parameters.AddWithValue("id", idtxt.Text.Trim());

            conn2.Open();

            cmd2.ExecuteNonQuery();

            conn2.Close();

            MessageBox.Show("Data updated successfully");

        }


        private void button2_Click(object sender, EventArgs e)

        {

            if (MessageBox.Show("Are you want to delete this row?", "Delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)

            {

                SqlConnection conn3 = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=True");

                SqlCommand cmd3 = new SqlCommand("Delete From Table1 Where id=@id", conn3);

                cmd3.Parameters.AddWithValue("id", idtxt.Text.Trim());

                conn3.Open();

                cmd3.ExecuteNonQuery();

                conn3.Close();

                idtxt.Text = "";

                ftxt.Text = "";

                ltxt.Text = "";

                phtxt.Text = "";

                dateTimePicker1.Format = DateTimePickerFormat.Custom;

                dateTimePicker1.CustomFormat = " ";

                pictureBox1.Image = null;

                m1.Checked = false;

                f1.Checked = false;

                MessageBox.Show("ID deleted Successfully");

            }

        }

    }

}


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");

            }

        }

    }

    }

}



Simple and Effective: Creating Invoices with Crystal Reports in C# With Source code

Source Code:
 SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=products;Integrated Security=True");
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select * From View_3 Where n_invoice=@n_invoice", conn);
            cmd.Parameters.AddWithValue("n_invoice", textBox1.Text.Trim());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            dt.Clear();
            da.Fill(dt);
            conn.Close();
            CrystalReport1 report = new CrystalReport1();
            report.SetDataSource(dt);
            crystalReportViewer1.ReportSource = report;
            crystalReportViewer1.Refresh();



Video MS Access VBA Tutorial: Building a Data Entry Form with ListBox



 MS Access VBA Tutorial: Building a Data Entry Form with ListBox

Subscribe to @programmingforeverybody

https://www.youtube.com/@programmingforeverybody/?sub_confirmation=1

Keywords:

Step-by-Step Guide: Creating a ListBox-Driven Data Entry Form in Access

Learn How to Use ListBoxes for Dynamic Data Input in MS Access

Master MS Access VBA: Design a Data Entry Form with ListBox Options

Creating a User-Friendly Data Entry Form in Access Using ListBoxes

Keyword-Rich Titles:

MS Access VBA ListBox Data Entry Form Tutorial

How to Make a ListBox-Based Data Entry Form in Access

Access VBA: Dynamic Data Entry with ListBoxes

Learn Access VBA: Create a ListBox-Driven Data Entry Form

Access VBA Tutorial: ListBox Data Validation and Input

Boost Your Access Skills: Create a Dynamic ListBox Data Entry Form

Simplify Data Entry in Access: Use ListBoxes Effectively

Master MS Access: Build a Powerful ListBox-Driven Data Entry Form

Take Your Access Game to the Next Level: Learn ListBox Data Entry

Want to Impress Your Boss? Create a Custom ListBox Data Entry Form


Very simple Code insert button VBA without duplicate values in MS Access VBA with source code





Source Code in video

Dim dbs As DAO.Database

Dim rst As DAO.Recordset

Dim strsql As String

Set dbs = OpenDatabase("H:\products.accdb")

Set rst = dbs.OpenRecordset("Select product From Table1 Where product = '" & Me.Text0.Value & "' ")

If rst.RecordCount > 0 Then

MsgBox ("This Product exists before")

Else

strsql = "Insert Into Table1(product)Values( '" & Me.Text0.Value & "' )"

dbs.Execute (strsql)

MsgBox ("The product inserted successfully")

End If

rst.Close

dbs.Close


c# tutorial for beginners: How to store images update and delete in sql database with 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;

using System.IO;


namespace Images

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data source=.;Initial catalog=images3;Integrated Security = true");

        SqlCommand cmd;

        private void button1_Click(object sender, EventArgs e)

        {

            openFileDialog1.Filter = "Select image(*.JpG; *.png; *.Gif)|*.JpG; *.png; *.Gif";

            if (openFileDialog1.ShowDialog()== DialogResult.OK)

            {

                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

            }

        }


        private void button2_Click(object sender, EventArgs e)

        {

            cmd = new SqlCommand("Insert Into table1(name_image,image1)Values(@name_image,@image1)", conn);

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

            MemoryStream memstr = new MemoryStream();

            pictureBox1.Image.Save(memstr, pictureBox1.Image.RawFormat);

            cmd.Parameters.AddWithValue("image1", memstr.ToArray());

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            MessageBox.Show("Data Inserted Successfully");

            load_data();

        }

        private void load_data()

        {

            cmd = new SqlCommand("Select * from table1 order by id desc", conn);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.RowTemplate.Height = 75;

            dataGridView1.DataSource = dt;

            DataGridViewImageColumn pic1 = new DataGridViewImageColumn();

            pic1 = (DataGridViewImageColumn)dataGridView1.Columns[2];

            pic1.ImageLayout = DataGridViewImageCellLayout.Stretch;

        }


        private void Form2_Load(object sender, EventArgs e)

        {

            load_data();

        }


        private void dataGridView1_Click(object sender, EventArgs e)

        {

            id1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();

            MemoryStream ms = new MemoryStream((byte[])dataGridView1.CurrentRow.Cells[2].Value);

            pictureBox1.Image = Image.FromStream(ms);

        }


        private void button3_Click(object sender, EventArgs e)

        {

            cmd = new SqlCommand("Update table1 Set name_image = @name_image,image1=@image1 Where id=@id", conn);

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

            MemoryStream memstr = new MemoryStream();

            pictureBox1.Image.Save(memstr, pictureBox1.Image.RawFormat);

            cmd.Parameters.AddWithValue("image1", memstr.ToArray());

            cmd.Parameters.AddWithValue("id", id1.Text);

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            load_data();

        }


        private void button4_Click(object sender, EventArgs e)

        {

            cmd = new SqlCommand("Delete from table1 where id=@id", conn);

            cmd.Parameters.AddWithValue("id", id1.Text);

            conn.Open();

            cmd.ExecuteNonQuery();

            conn.Close();

            load_data();

            pictureBox1.Image = null;

            textBox1.Text = "";

            id1.Text = "";

        }

    }

}


Code VB.Net SQL query combine two columns that contain different data type and display in ComboBox

 



Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim conn As New SqlConnection("Data Source=.;Initial Catalog=products;integrated Security=true")

        'conn.Open()

        Dim cmd As New SqlCommand("Select id, '(' +  Cast(id As nvarchar(50))+ ')' + item_product  As products from names_products", conn)

        Dim da As New SqlDataAdapter(cmd)

        Dim dt As New DataTable

        da.Fill(dt)

        With ComboBox1

            .DataSource = dt

            .DisplayMember = "products"

            .ValueMember = "id"

            .Text = "Select Product"

        End With

    End Sub

End Class