Programming for Everybody

VB. net Tutorial import data from Excel to SQL server

 Imports System.Data.OleDb

Imports System.Data.SqlClient

Public Class Form15

    Private Sub Form15_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" &

         "Data Source=F:\names2.xlsx;Extended Properties= 'Excel 8.0;HDR=False'")

        conn.Open()

        Dim cmd As New OleDbCommand("Select * from [sheet4$]", conn)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd

        Dim dt As New DataTable

        dt.Clear()

        da.Fill(dt)

        DataGridView1.DataSource = dt

        conn.Close()

    End Sub


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

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

        For Each row As DataGridViewRow In DataGridView1.Rows

            Dim cmd2 As New SqlCommand("Insert into table2(id,name1,age,telephone)Values(@id,@name1,@age,@telephone)", conn2)

            cmd2.Parameters.AddWithValue("id", row.Cells("id").Value.ToString)

            cmd2.Parameters.AddWithValue("name1", row.Cells("name1").Value.ToString)

            cmd2.Parameters.AddWithValue("age", row.Cells("age").Value.ToString)

            cmd2.Parameters.AddWithValue("telephone", row.Cells("telephone").Value.ToString)

            conn2.Open()

            cmd2.ExecuteNonQuery()

            conn2.Close()

        Next

        MessageBox.Show("All Data inserted successfully")

    End Sub

End Class



How do you create register form and relate it with user form in VB.net [with source code]

Form1>>>Register form 

Imports System.IO

Imports System.Data.SqlClient

Public Class Form1

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

        If CheckBox1.Checked = True Then

            password.UseSystemPasswordChar = False

        Else

            password.UseSystemPasswordChar = True

        End If

    End Sub

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

        OpenFileDialog1.ShowDialog()

        image_path.Text = Path.GetDirectoryName(OpenFileDialog1.FileName) & "\" & Path.GetFileName(OpenFileDialog1.FileName)

        PictureBox1.Image = Image.FromFile(image_path.Text)

    End Sub


    Private Sub lastname_KeyDown(sender As Object, e As KeyEventArgs) Handles lastname.KeyDown

        If e.KeyCode = Keys.Enter Then

            username.Text = firstname.Text + lastname.Text

        End If

    End Sub


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

        If username.Text = "" Then

            MessageBox.Show("Please enter username")

            Return

        End If

        If m1.Checked = False And f1.Checked = False Then

            MessageBox.Show("Please select gender")

            Return

        End If

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

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

        Dim cmd As New SqlCommand("Select username from table1 where username=@username", conn)

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

        Dim myreader As SqlDataReader = cmd.ExecuteReader

        If (myreader.Read()) Then

            MessageBox.Show("Username inserted before")

            conn.Close()

            Return

        Else

            conn.Close()


            Dim cmd2 As New SqlCommand("Insert into table1(firstname,lastname,username,password,phone,date_birth,gender,image1)Values(@firstname,@lastname,@username,@password,@phone,@date_birth,@gender,@image1)", conn)

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

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

            cmd2.Parameters.AddWithValue("username", username.Text)

            cmd2.Parameters.AddWithValue("password", password.Text)

            cmd2.Parameters.AddWithValue("phone", phone.Text)

            cmd2.Parameters.AddWithValue("date_birth", date_birth.Text)



            Dim gender_v As Boolean

            If m1.Checked = True Then

                gender_v = 1

            End If

            If f1.Checked = True Then

                gender_v = 0

            End If

            cmd2.Parameters.AddWithValue("gender", gender_v)

            cmd2.Parameters.AddWithValue("image1", image_path.Text)

            If conn.State = ConnectionState.Closed Then

                conn.Open()

            End If

            cmd2.ExecuteNonQuery()

            conn.Close()

            username_v = username.Text

            Form2.Show()

        End If

    End Sub

end class

Form2>>>user form 


Imports System.Data.SqlClient

Public Class Form2

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

        username.Text = username_v

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

        If conn.State = ConnectionState.Closed Then

            conn.Open()

        End If

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

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

        Dim myreader As SqlDataReader = cmd.ExecuteReader

        If (myreader.Read()) Then

            firstname.Text = myreader("firstname")

            lastname.Text = myreader("lastname")

            phone.Text = myreader("phone")

            password.Text = myreader("password")

            Dim gender_v As Boolean = myreader("gender")

            If gender_v = True Then

                m1.Checked = True

            Else

                f1.Checked = True

            End If

            image_path.Text = myreader("image1")

            PictureBox1.Image = Image.FromFile(image_path.Text)

            date_birth.Text = myreader("date_birth")

            conn.Close()


        End If

    End Sub

End Class






Programming with C # tutorial: Login Form in C # with SQL 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.SqlClient;


namespace login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data source=.; initial catalog=users;integrated security=true");

            SqlCommand cmd = new SqlCommand("select * from login1 where user1=@user1 and pass1=@pass1", conn);

            cmd.Parameters.Add("@user1", SqlDbType.VarChar).Value = textBox1.Text;

            cmd.Parameters.Add("@pass1", SqlDbType.VarChar).Value = textBox2.Text;


            SqlDataAdapter adapter1 = new SqlDataAdapter(cmd);

            DataTable table = new DataTable();

            adapter1.Fill(table);

            if (table.Rows.Count == 1)
            {

                Form2 gg = new Form2();

                gg.Show();

                this.Hide();

            }
            else
            {
                MessageBox.Show("Error username or password");


            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBox1.Checked == true)
            {
                textBox2.UseSystemPasswordChar = false;
            }
            else
            {

                textBox2.UseSystemPasswordChar = true;
            }



        }

       
    }

    }

Visual Basic.NET: insert, update, delete and search using access database and print datagridview

 



Imports System.Data.OleDb

Public Class Form3

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

    Private Sub bind_data()

        Dim cmd1 As New OleDbCommand("Select * from table1", conn)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd1

        Dim table1 As New DataTable

        table1.Clear()

        da.Fill(table1)

        DataGridView1.DataSource = table1


    End Sub


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

        bind_data()

    End Sub


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

        Dim strsql As String

        strsql = "Insert into table1(id,firstname,lastname,sum1)Values(@id,@firstname,@lastname,@sum1)"

        Dim cmd2 As New OleDbCommand(strsql, conn)

        cmd2.Parameters.AddWithValue("@id", TextBox1.Text)

        cmd2.Parameters.AddWithValue("@firstname", TextBox2.Text)

        cmd2.Parameters.AddWithValue("@lastname", TextBox3.Text)

        cmd2.Parameters.AddWithValue("@sum1", TextBox4.Text)

        conn.Open()

        cmd2.ExecuteNonQuery()

        conn.Close()

        bind_data()



    End Sub


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

        TextBox1.Text = ""

        TextBox2.Text = ""

        TextBox3.Text = ""

        TextBox4.Text = ""


    End Sub


    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        Dim index As Integer

        index = e.RowIndex

        Dim selectedrow As DataGridViewRow = 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


    End Sub


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

        Dim cmd4 As New OleDbCommand("Update table1 set firstname='" & TextBox2.Text & "',lastname='" & TextBox3.Text & "',sum1=" & TextBox4.Text & " where id=" & TextBox1.Text & "", conn)

        conn.Open()

        cmd4.ExecuteNonQuery()

        conn.Close()

        bind_data()

    End Sub


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

        Dim cmd5 As New OleDbCommand("delete from table1 where id=@id", conn)

        cmd5.Parameters.AddWithValue("@id", TextBox1.Text)

        conn.Open()

        cmd5.ExecuteNonQuery()

        conn.Close()

        bind_data()

    End Sub


    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

        PrintPreviewDialog1.Document = PrintDocument1

        PrintPreviewDialog1.PrintPreviewControl.Zoom = 1

        PrintPreviewDialog1.ShowDialog()


    End Sub


    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim imagebmp As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)

        DataGridView1.DrawToBitmap(imagebmp, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))

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

    End Sub


    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        Dim cmd1 As New OleDbCommand("Select * from table1 where firstname like '%' +@parm1+ '%' ", conn)

        cmd1.Parameters.AddWithValue("@parm1", TextBox5.Text)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd1

        Dim table1 As New DataTable

        table1.Clear()

        da.Fill(table1)

        DataGridView1.DataSource = table1

    End Sub

End Class

Programming with c#: how to search data from access database and display it in textboxes 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.Windows.Forms;

using System.Data.OleDb;


namespace retrieve_data_c_message_box

{

    public partial class Form5 : Form

    {

        OleDbConnection conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\ddd.accdb");

        public Form5()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            conn1.Open();

            OleDbCommand cmd1 = new OleDbCommand("Select id,firstname,lastname,sum1 from table2 where id=@parm1", conn1);

            cmd1.Parameters.AddWithValue("@parm1", search1.Text);

            OleDbDataReader reader1;

            reader1 = cmd1.ExecuteReader();

         if(   reader1.Read())

            {

                textBox1.Text = reader1["id"].ToString();

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

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

                textBox4.Text = reader1["sum1"].ToString();

            }

         else

            {

                MessageBox.Show("No Data found");

            }


            conn1.Close();

        }


        private void button2_Click(object sender, EventArgs e)

        {

            conn1.Open();

            OleDbCommand cmd1 = new OleDbCommand("Select id,firstname,lastname,sum1 from table2 where firstname=@parm1", conn1);

            cmd1.Parameters.AddWithValue("@parm1", search1.Text);

            OleDbDataReader reader1;

            reader1 = cmd1.ExecuteReader();

            if (reader1.Read())

            {

                textBox1.Text = reader1["id"].ToString();

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

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

                textBox4.Text = reader1["sum1"].ToString();


            }

            else

            {

                MessageBox.Show("No data found");

            }

            conn1.Close();

        }

    }

}


Visual Basic.Net: Insert Checked rows from DataGridView to Access Database with source code

 




Imports System.Data.OleDb

Public Class Form14

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


    Private Sub load_data()

        conn.Open()

        Dim cmd As New OleDbCommand("select id,name1,telephone from names1", conn)

        Dim da As New OleDbDataAdapter

        da.SelectCommand = cmd

        Dim table1 As New DataTable

        table1.Clear()

        da.Fill(table1)

        DataGridView1.DataSource = table1

        conn.Close()

        Dim checkboxcol As New DataGridViewCheckBoxColumn

        checkboxcol.Width = 40

        checkboxcol.Name = "checkboxcol"

        checkboxcol.HeaderText = ""

        DataGridView1.Columns.Insert(0, checkboxcol)

    End Sub

     

    Private Sub Form14_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        load_data()

    End Sub


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

        For Each row As DataGridViewRow In DataGridView1.Rows

            Dim select1 As Boolean = Convert.ToBoolean(row.Cells("checkboxcol").Value)

            If select1 Then

                conn.Open()

                Dim sql As String = " insert into telephones(Id,name1,telephone) values(@id,@name1,@telephone)"

                Dim cmd1 As New OleDbCommand(sql, conn)

                cmd1.Parameters.AddWithValue("@id", row.Cells("ID").Value)

                cmd1.Parameters.AddWithValue("@name1", row.Cells("name1").Value)

                cmd1.Parameters.AddWithValue("@telephone", row.Cells("telephone").Value)

                cmd1.ExecuteNonQuery()

                conn.Close()

            End If

        Next

        MessageBox.Show("Records Inserted")

    End Sub

Open word document in RichtextBox using VB.net

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim op1 As OpenFileDialog = New OpenFileDialog() With {
            .ValidateNames = True,
            .Multiselect = False,
            .Filter = "Word|*.doc;*.docx"


                }
        If op1.ShowDialog() = DialogResult.OK Then

            Dim readonly1 As Object = True
            Dim visible As Object = True
            Dim save As Object = True
            Dim filename As Object = op1.FileName
            Dim missing As Object = Type.Missing
            Dim template As Object = False
            Dim typedoc As Object = 0

            Dim doc1 As Microsoft.Office.Interop.Word._Document = Nothing
            Dim word1 As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application() With {
              .Visible = False
            }

            doc1 = word1.Documents.Open(filename, readonly1, visible)
            doc1.ActiveWindow.Selection.WholeStory()
            doc1.ActiveWindow.Selection.Copy()
            Dim data1 As IDataObject = Clipboard.GetDataObject
            RichTextBox1.Rtf = data1.GetData(DataFormats.Rtf).ToString
            word1.Quit(missing)


        End If
End Sub