Programming for Everybody

insert update delete search and print in sql server databse using C# [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 Insert_update_delete_search_and_print_in_sql

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

        SqlConnection conn = new SqlConnection("Data source=.;initial catalog=database3;integrated security=true");

        private void Form2_Load(object sender, EventArgs e)

        {

            bind_data();

        }

        private void bind_data()

        {

            SqlCommand cmd1 = new SqlCommand("Select id,fname As firstname,lname As Lastname,sum from Table1", conn);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd1;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

            dataGridView1.DefaultCellStyle.Font = new Font("arial", 12);

        }


        private void button1_Click(object sender, EventArgs e)

        {

            SqlCommand cmd2 = new SqlCommand("Insert into Table1(id,fname,lname,sum)Values(@id,@firstname,@lastname,@sum)", conn);

            cmd2.Parameters.AddWithValue("id", textBox1.Text);

            cmd2.Parameters.AddWithValue("firstname", textBox2.Text);

            cmd2.Parameters.AddWithValue("lastname", textBox3.Text);

            cmd2.Parameters.AddWithValue("sum", textBox4.Text);

            conn.Open();

            cmd2.ExecuteNonQuery();

            conn.Close();

            bind_data();


        }


        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";

            textBox4.Text = "";


        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            int index;

            index = e.RowIndex;

            DataGridViewRow selectedrow = dataGridView1.Rows[index];

            textBox1.Text = selectedrow.Cells[0].Value.ToString();

            textBox2.Text = selectedrow.Cells[1].Value.ToString();

            textBox3.Text = selectedrow.Cells[2].Value.ToString();

            textBox4.Text = selectedrow.Cells[3].Value.ToString();



        }


        private void button3_Click(object sender, EventArgs e)

        {

            SqlCommand cmd3 = new SqlCommand("Update Table1 Set fname=@firstname,lname=@lastname,sum=@sum where id=@id", conn);


            

            cmd3.Parameters.AddWithValue("firstname", textBox2.Text);

            cmd3.Parameters.AddWithValue("lastname", textBox3.Text);

            cmd3.Parameters.AddWithValue("sum", textBox4.Text);

            cmd3.Parameters.AddWithValue("id", textBox1.Text);

            conn.Open();

            cmd3.ExecuteNonQuery();

            conn.Close();

            bind_data();

        }


        private void button4_Click(object sender, EventArgs e)

        {

            SqlCommand cmd4 = new SqlCommand("Delete from Table1 where id=@id", conn);

            cmd4.Parameters.AddWithValue("id", textBox1.Text);

            conn.Open();

            cmd4.ExecuteNonQuery();

            conn.Close();

            bind_data();

        }


        private void button5_Click(object sender, EventArgs e)

        {

            SqlCommand cmd1 = new SqlCommand("Select id,fname As firstname,lname As Lastname,sum from Table1 where fname Like @firstname+'%'", conn);

            cmd1.Parameters.AddWithValue("firstname", textBox5.Text);

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd1;

            DataTable dt = new DataTable();

            dt.Clear();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

            dataGridView1.DefaultCellStyle.Font = new Font("arial", 12);

        }


        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

        {

            Bitmap imagebmp = new Bitmap(dataGridView1.Width, dataGridView1.Height);

            dataGridView1.DrawToBitmap(imagebmp, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));

            e.Graphics.DrawImage(imagebmp, 120, 20);

        }


        private void button6_Click(object sender, EventArgs e)

        {

            printPreviewDialog1.Document = printDocument1;

            printPreviewDialog1.PrintPreviewControl.Zoom = 1;

            printPreviewDialog1.ShowDialog();

        }

    }

}

Step by step learn (HTML,PHP and mysql database) with login and logout page with session in PHP

Step by step learn (HTML,PHP and mysql database) with login and logout page with session in PHP Contents: Create a new folder website 00:00 Save as a webpage in folder website login.php 00:29 HTML !DOCTYPE Declaration is an "information" to the browser about what document type to expect. 01:04 The html tag represents the root of an HTML document,and close by 01:13 The header element represents a container for introductory content or a set of navigational links. 01:18 meta charset="UTF-8" 01:23 you are telling your browser to use the UTF-8 character encoding, which is a method of converting your typed characters into machine-readable code. The title tag defines the title of the document 01:39 body contains all the contents of an HTML document. 02:06 The div tag defines a division or a section in an HTML document. 02:56 The h1 tag is used to define HTML heading. 03:13 Add styles to Div "aa" 03:43 Make div in the Center 05:13 Make the text in the center 05:13 The form tag is used to create an HTML form for user input. 06:25 The br tag inserts a single line break.08:04 Add a short hint that describes the expected value of an input field 09:37 Make input fields must be filled out before submitting the form 10:28 OPen XAMPP to create mysql database 11:14 Create database in phpmyadmin 11:59 Create table 12:34 Insert User 13:50 Connect php with database 14:24 Check connection 15:44 Add code php in login page 16:54 Call connect_database file by require 17:14 Start Session 17:41 check if the input username is filled or not null. 17:54 Removes backslashes 18:21 Escapes a string for use in SQL Statements 31:37 check if username exists in mysql database or no 20:05 mysqli_num_rows Returns the number of rows in a recordest 22:03 Store Session Username 22:43 Redirect to index.php Page 23:07 Error message 23:36 Check if session username exists 26:27 Create index page 27:46 Add hyperlink 29:39 Create logout page with destroy or remove session username 29:53 #programming_for_everybody Tags: How to connect HTML register form to MySQL database with PHP Login PHP Admin and user login in PHP Administrator login php Registration form in PHP code with validation PHP admin login




Visual Basic.net: how to calculate third column in a datagridview as product of two other columns

 

Public Class Form8

    Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim table As New DataTable("table")

        table.Columns.Add("ID", Type.GetType("System.Int32"))

        table.Columns.Add("Name", Type.GetType("System.String"))

        table.Columns.Add("Arabic", Type.GetType("System.Double"))

        table.Columns.Add("English", Type.GetType("System.Double"))

        table.Columns.Add("Sum", Type.GetType("System.Double"))

       DataGridView1.DataSource = table

    End Sub

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

        For i As Integer = 0 To DataGridView1.Rows.Count - 1

            Dim a1 As Double = DataGridView1.Rows(i).Cells(2).Value

            Dim b1 As Double = DataGridView1.Rows(i).Cells(3).Value

            Dim c1 As Double = a1 + b1

            DataGridView1.Rows(i).Cells(4).Value = c1

        Next

    End Sub

    Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded

        e.Row.Cells("Arabic").Value = "0"

        e.Row.Cells("English").Value = "0"

        e.Row.Cells("Sum").Value = "0"

    End Sub



Visual Basic.net: retrieve values from database in row datagridview based on cell in Same row

 Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown

        If e.KeyCode = Keys.Right Then

            For i  As Integer = 0 To DataGridView1.Rows.Count - 1

                Dim constring1 As String = "data source=.;initial catalog=users;integrated security=true"

               Dim query1 As String = " select id,name,country from table2 where id='" & DataGridView1.Rows(i).Cells(0).Value & "'"

               Dim cn1 As SqlConnection = New SqlConnection(constring1)

                Dim cmd1 As SqlCommand = New SqlCommand(query1, cn1)

                cn1.Open()

                If True Then

                    Using read1 As SqlDataReader = cmd1.ExecuteReader()

                        While read1.Read()

                            DataGridView1.Rows(i).Cells(1).Value = (read1("name"))

                            DataGridView1.Rows(i).Cells(2).Value = (read1("country"))

                        End While

                    End Using

                End If

                cn1.Close()

            Next

        End If

 End Sub




VB.net: filter dates from access database between two datetimepickers and display in datagridview with source code

 Imports System.Data.OleDb

Public Class Form4

    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\dates.accdb")

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        d1.Format = DateTimePickerFormat.Custom

        d1.CustomFormat = "MM/dd/yyyy"

        d2.Format = DateTimePickerFormat.Custom

        d2.CustomFormat = "MM/dd/yyyy"

        DataGridView1.BackgroundColor = System.Drawing.SystemColors.Control

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim cmd1 As New OleDbCommand(" select id,date1,username from table1", conn)

        Dim da As New OleDbDataAdapter

        Dim dt As New DataTable

        da.SelectCommand = cmd1

        dt.Clear()

        da.Fill(dt)

        DataGridView1.DataSource = dt

        DataGridView1.Columns(1).DefaultCellStyle.Format = "dd/MM/yyyy"

        DataGridView1.Columns(0).HeaderText = "ID"

        DataGridView1.Columns(1).HeaderText = "Start date"

        DataGridView1.Columns(2).HeaderText = "Username"

        DataGridView1.EnableHeadersVisualStyles = False

        With DataGridView1.ColumnHeadersDefaultCellStyle

            .Font = New Font("arial", 12, FontStyle.Italic)

            .BackColor = Color.Black

            .ForeColor = Color.White

        End With

        DataGridView1.Columns(0).DefaultCellStyle.Font = New Font("tahoma", 10, FontStyle.Bold)

        DataGridView1.Columns(1).DefaultCellStyle.Font = New Font("tahoma", 10, FontStyle.Italic)

        DataGridView1.Columns(2).DefaultCellStyle.Font = New Font("tahoma", 10, FontStyle.Underline)

        DataGridView1.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter

        DataGridView1.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter

        DataGridView1.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter

        DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

        DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

        DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

        DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Yellow

        DataGridView1.Columns(1).DefaultCellStyle.BackColor = Color.Brown

        DataGridView1.Columns(2).DefaultCellStyle.BackColor = Color.Green

        DataGridView1.Rows(0).Cells(0).Selected = False

        DataGridView1.Rows(2).Cells(1).Selected = False

    End Sub

     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

         If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim dtdate1 As DateTime = DateTime.Parse(d1.Text)

        Dim dtdate2 As DateTime = DateTime.Parse(d2.Text)

        Dim cmd1 As OleDbCommand = New OleDbCommand("select id,date1,username from table1 where date1 between #" &

        dtdate1.ToString("MM/dd/yyyy") & "# and #" &

dtdate2.ToString("MM/dd/yyyy") & "# order by date1 desc", conn)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd1

        Dim dt As New DataTable

        dt.Clear()

        da.Fill(dt)

        DataGridView1.DataSource = dt

        conn.Close()

    End Sub

End Class



How to create login form with multi users and permissions in VB.net using Sql database[with Source code]




 Imports System.Data.SqlClient

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim conn As New SqlConnection("Data source=.;initial catalog=login3;integrated security=true")

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim cmd As New SqlCommand("Select * from Admin where username='admin' and password=@password", conn)

        cmd.Parameters.AddWithValue("password", TextBox1.Text)

        Dim myreader As SqlDataReader = cmd.ExecuteReader

        If (myreader.Read()) Then

            username_v = myreader("username")

            Form2.Show()

            Me.Hide()

        Else

            MsgBox("Error Password")

        End If

    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Me.Close()

    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim conn As New SqlConnection("Data source=.;initial catalog=login3;integrated security=true")

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim cmd As New SqlCommand("Select * from users where username=@username and password=@password", conn)

        cmd.Parameters.AddWithValue("username", TextBox3.Text)

        cmd.Parameters.AddWithValue("password", TextBox2.Text)

        Dim myreader As SqlDataReader = cmd.ExecuteReader

        If (myreader.Read()) Then

            username_v = myreader("username")

            If Not myreader.IsDBNull(myreader.GetOrdinal("insert")) Then

                CheckBox1.Checked = myreader("insert")

            End If

            If Not myreader.IsDBNull(myreader.GetOrdinal("update")) Then

                CheckBox2.Checked = myreader("update")

            End If

            If Not myreader.IsDBNull(myreader.GetOrdinal("delete")) Then

                CheckBox3.Checked = myreader("delete")

            End If

            If CheckBox1.Checked = False Then

                Form2.Button1.Visible = False

            End If

            If CheckBox2.Checked = False Then

                Form2.Button2.Visible = False

            End If

            If CheckBox3.Checked = False Then

                Form2.Button3.Visible = False

            End If


            Form2.Show()

                Me.Hide()

            Else

                MsgBox("Error Password")

        End If

    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Me.Close()

    End Sub

End Class

ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

Public Class Form2

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

        Label1.Text = "Welcome, " & username_v

    End Sub

End Class

ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ

Module Module1

    Public username_v As String

End Module

Backup delete and restore sql server database in VB. net

 

Imports System.Data.SqlClient



Public Class Form3

    Dim conn As New SqlConnection("Data source=.;Integrated security=true")

    Private Sub databases()

        conn.Open()

        ComboBox1.Items.Clear()

        Dim cmd1 As New SqlCommand("Select * from sysdatabases order by name", conn)

        Dim myreader As SqlDataReader = cmd1.ExecuteReader

        While myreader.Read

            ComboBox1.Items.Add(myreader(0))

            ComboBox1.Text = "Select database"

        End While

        conn.Close()

    End Sub

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

        databases()

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If ComboBox1.Text = "Select database" Then

            MessageBox.Show("Please Select database")

            Return

        Else

            SaveFileDialog1.FileName = ComboBox1.SelectedItem

            If SaveFileDialog1.ShowDialog = DialogResult.OK Then

                Dim lfolder As String

                lfolder = SaveFileDialog1.FileName

                Dim cmd2 As New SqlCommand("BACKUP Database " & ComboBox1.Text & " To disk='" & lfolder & "'", conn)

                conn.Open()

                cmd2.ExecuteNonQuery()

                conn.Close()

                MessageBox.Show("database backed up successfully")

            Else

                MessageBox.Show("Please Save database")

            End If

        End If

    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If ComboBox1.Text = "Select database" Then

            MessageBox.Show("Please Select database")

            Return

        Else

            Dim cmd3 As New SqlCommand("Drop database " & ComboBox1.Text & "", conn)

            conn.Open()

            cmd3.ExecuteNonQuery()

            conn.Close()

            MessageBox.Show("database deleted successfully")

            databases()

        End If


    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim dbname As String

        dbname = InputBox("Input database name you want to restore", "Database Name")

        Dim cmd4 As New SqlCommand("Select * from sysdatabases where name = '" & dbname & "'", conn)

        conn.Open()

        Dim myreader2 As SqlDataReader = cmd4.ExecuteReader

        If myreader2.Read Then

            MessageBox.Show("Database exists in sql server")

            conn.Close()

        Else

            conn.Close()

            If OpenFileDialog1.ShowDialog = DialogResult.OK Then

                Dim cmd3 As New SqlCommand("Restore database " & dbname & " from disk='" & OpenFileDialog1.FileName & "'", conn)

                conn.Open()

                cmd3.ExecuteReader()

                conn.Close()

                databases()

                MessageBox.Show("database restored successfully")

            Else

                MessageBox.Show("Please Select database you want to restore")

            End If

        End If

    End Sub

End Class