Programming for Everybody: 2020

C#: Insert all data of a dataGridView to database at once with database with source code




private void button8_Click(object sender, EventArgs e)

        {

            foreach(DataGridViewRow row in dataGridView1.Rows)

            {

                string constring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:/ddd.accdb";

                using (OleDbConnection con = new OleDbConnection(constring))

                {


                    using (OleDbCommand cmd = new OleDbCommand(" INSERT INTO table1 ([ID], [firstname],[lastname],[sum]) VALUES (@id,@firstname,@lastname,@sum)", con))

                    {



                        cmd.Parameters.AddWithValue("@id", row.Cells["ID"].Value);


                        cmd.Parameters.AddWithValue("@firstname", row.Cells["firstname"].Value);

                        cmd.Parameters.AddWithValue("@lastname", row.Cells["lastname"].Value);


                        cmd.Parameters.AddWithValue("@sum", row.Cells["sum"].Value);



                        con.Open();

                        cmd.ExecuteNonQuery();

                        con.Close();

                    }

                 

                    }

              


                }



            MessageBox.Show("All rows inserted");

         

           

        } 

Visual Basic.net: Populate Treeview in VB.Net with Access database(with Source Code)



 Imports System.Data

Imports System.Data.OleDb

Public Class Form3

    Private Function bind_data(ByVal strsql As String) As DataTable

        Dim dt As New DataTable

        Dim constr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Prog\Documents\sports.accdb"


        Using con As New OleDbConnection(constr)


            Using cmd As New OleDbCommand(strsql)


                Using sda As New OleDbDataAdapter()


                    cmd.CommandType = CommandType.Text

                    cmd.Connection = con

                    sda.SelectCommand = cmd


                    sda.Fill(dt)


                End Using


            End Using


            Return dt

        End Using

    End Function

    Private Sub ptreeview(ByVal dtparent As DataTable, ByVal parentid As Integer, ByVal tree1 As TreeNode)

        For Each row As DataRow In dtparent.Rows


            Dim child As New TreeNode() With { _

                .Text = row("name1").ToString(), _

                .Tag = row("id") _

            }

            If parentid = 0 Then


                TreeView1.Nodes.Add(child)


   Dim dtchild As DataTable = Me.bind_data("select id,name1 from names1 where id_sport=" & child.Tag)

                ptreeview(dtchild, Convert.ToInt32(child.Tag), child)

            Else

                tree1.Nodes.Add(child)

            End If

        Next

    End Sub


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

        Dim dt As DataTable = Me.bind_data("select id,name1 from sports1")

        Me.ptreeview(dt, 0, Nothing)

    End Sub

End Class

Visual Basic net: Datagridview multiple columns search from 4 tables using inner join

 Imports System.Data.SqlClient

Public Class Form7

    Private Function search1() As DataTable

        Dim query1 As String = "Select table1.id,table1.name1,table1.tel," & _

            "sports.sport_name,hobbies.hobby_name,countries.country from" & _

            "(table1 inner join sports on table1.id_sport=sports.id)" & _

            " inner join hobbies on table1.id_hobby=hobbies.id" & _

            " inner join countries on table1.id_country=countries.id"

        query1 &= " where name1 like '%' +@parm1+ '%' "

        query1 &= " or name1 like '%' +@parm1+ '%' "

        query1 &= " or tel like '%' +@parm1+ '%' "

        query1 &= " or sport_name like '%' +@parm1+ '%' "

        query1 &= " or hobby_name like '%' +@parm1+ '%' "

        query1 &= " or country like '%' +@parm1+ '%' "

        query1 &= " or @parm1= '' order by table1.id "

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

        Dim cmd As New SqlCommand(query1, conn)

        cmd.Parameters.AddWithValue("@parm1", TextBox1.Text.Trim())

        Dim da As New SqlDataAdapter(cmd)

        Dim dt As New DataTable

        da.Fill(dt)

        DataGridView1.DataSource = dt

        Return (dt)

    End Function


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

        DataGridView1.DataSource = Me.search1


    End Sub


    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp

        DataGridView1.DataSource = Me.search1

    End Sub

End Class



تحميل برنامج عارض ملفات الاكسس Access

 برنامج عارض ملفات الاكسس Access: هو برنامج بسيط لعرض جميع ملفات قواعد بيانات اكسس بجميع انواعها وحتى لو به كلمة مرور بشرط معرفة كلمة المرور يتم ادخالها من قبل المستخدم ثم يعرض جميع الجداول بقاعدة البيانات  ثم يتم اختيار الجدول المراد عرض وتعديل بياناته  بجملة استعلام افتراضية وعند الرغبة في عمل استعلامات  أخرى لعرض بيانات اكثر  يمكن متابعة فيديو الشرح للبرنامج أو ارسال اي استفسار، وهذا البرنامج بالرغم من بساطته ولكنه يساعدك في عرض اي ملف قاعدة بيانات اكسيس وكذلك تعلم برمجة 

Visual Basic.net و C# و SQL Server وذلك من خلال كتابة جمل الاستعلام في البرنامج.

البرنامج متاح مجاني للجميع ولكن من يستفيد بالبرنامج الرجاء الاشتراك بالقناة وعمل اعجاب ومشاركة الفيديو وذلك للتشجيع على الاستمرار . ولكم جزيل الشكر 

 رابط التحميل

http://www.mediafire.com/file/22109oj0zajee2c/Small_access.rar/file



Visual Basic.net: Insert RadioButton values into database (with source code)





Imports System.Data.SqlClient


Public Class Form3


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

        Dim conn As New SqlConnection("Data Source=.;Initial Catalog=users;Integrated Security=True")

        Dim name1 As String = TextBox1.Text

        Dim sport1 As String = String.Empty

        Dim hobby1 As String = String.Empty

        If f1.Checked Then

            sport1 = f1.Text

        ElseIf h1.Checked Then

            sport1 = h1.Text

        ElseIf V1.Checked Then

            sport1 = V1.Text

        End If

        If r1.Checked Then

            hobby1 = r1.Text

        ElseIf fi1.Checked Then

            hobby1 = fi1.Text

        ElseIf p1.Checked Then

            hobby1 = p1.Text

        End If

        Dim strsql As String

        strsql = " insert into table3(name,sport,hobby)Values(@name,@sport,@hobby)"

        Dim cmd1 As New SqlCommand(strsql, conn)

        cmd1.Parameters.AddWithValue("@name", name1)

        cmd1.Parameters.AddWithValue("@sport", sport1)

        cmd1.Parameters.AddWithValue("@hobby", hobby1)


        conn.Open()

        cmd1.ExecuteNonQuery()

        conn.Close()

        MessageBox.Show("Data Inserted")

    End Sub



How to update,delete image into a sql server database using vb.net(with Source Code+database)Part2

 


Full Code (Part1+part2)

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form2


    Dim con1 As New SqlConnection("Server=.;Database=images;integrated security=true")

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

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


        Dim openfiledialog1 As New OpenFileDialog


        openfiledialog1.Filter = "images|*.jpg;*.png;*.gif;*.bmp"


        If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then



            PictureBox1.Image = Image.FromFile(openfiledialog1.FileName)


        End If

    End Sub

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


        Dim command1 As New SqlCommand("Insert into table1(name,age,img1)values(@name,@age,@img)", con1)

        command1.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text


        command1.Parameters.Add("@age", SqlDbType.VarChar).Value = TextBox2.Text



        Dim memstr As New MemoryStream


        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = memstr.ToArray


        If con1.State = ConnectionState.Closed Then

            con1.Open()


        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("image inserted")

        con1.Close()

        bind1()

    End Sub


    Private Sub bind1()

        Dim command1 As New SqlCommand("Select * from table1 order by id desc", con1)


        Dim adapter As New SqlDataAdapter(command1)


        Dim table As New DataTable()

        adapter.Fill(table)

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.RowTemplate.Height = 90


        Dim pic1 As New DataGridViewImageColumn

        DataGridView1.DataSource = table

        pic1 = DataGridView1.Columns(3)

        pic1.ImageLayout = DataGridViewImageCellLayout.Stretch


    End Sub



    Private Sub DataGridView1_Click(sender As Object, e As System.EventArgs) Handles DataGridView1.Click


        Label4.Text = DataGridView1.CurrentRow.Cells(0).Value

        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value


        TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value



        Dim img As Byte()

        img = DataGridView1.CurrentRow.Cells(3).Value

        Dim memstr As New MemoryStream(img)


        PictureBox1.Image = Image.FromStream(memstr)




    End Sub

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


        Dim memstr As New MemoryStream

        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)


        Dim img() As Byte

        img = memstr.ToArray()

        Dim update1 As String = "Update table1 set name='" & TextBox1.Text & "',age='" & TextBox2.Text & "',img1=@img where id=" & Label4.Text

        Dim command1 As New SqlCommand(update1, con1)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = img

        If con1.State = ConnectionState.Closed Then

            con1.Open()
        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image updated")

        con1.Close()

        bind1()

    End Sub

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


        Dim delete1 As String = "Delete from table1 where id=" & Label4.Text

        Dim command1 As New SqlCommand(delete1, con1)

        If con1.State = ConnectionState.Closed Then

            con1.Open()

        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image deleted")


        con1.Close()


        bind1()


    End Sub
End Class

Visual Basic.net: Insert data into sql Database and Solve Violation of PRIMARY KEY

 


:Source code

Imports System.Data.SqlClient


Public Class Form4

    Dim con1 As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=users;Integrated Security=True")


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


        con1.Open()

        Dim cmd2 As New SqlCommand("Select name1,username1 from table1 where username1=@username1", con1)


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

        Dim myreader As SqlDataReader

        myreader = cmd2.ExecuteReader()

        If (myreader.Read()) Then

            con1.Close()

            MessageBox.Show("Duplicate Username")

            con1.Close()

        Else

           con1.Close()

            Dim strsql As String


            strsql = " Insert into table1(name1,username1) Values(@name,@username)"


            Dim cmd1 As New SqlCommand(strsql, con1)


            cmd1.Parameters.AddWithValue("@name", TextBox1.Text)

            cmd1.Parameters.AddWithValue("@username", TextBox2.Text)


            con1.Open()

            cmd1.ExecuteNonQuery()

            con1.Close()

            MessageBox.Show(" insert successful")


        End If


    End Sub

End Class

Retrieve data from a database in VB.net and Solve Invalid attempt to read when no data is present


 

Imports System.Data.SqlClient

Public Class Form3

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con1 As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=names;Integrated Security=True")
        con1.Open()
        Dim cmd1 As New SqlCommand(" Select name1,tel from table1 where id=@parm1", con1)

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

        Dim myreader As SqlDataReader

        myreader = cmd1.ExecuteReader()

        If (myreader.Read()) Then
            TextBox2.Text = myreader("name1")
            TextBox3.Text = myreader("tel")
        Else
            TextBox2.Text = ""
            TextBox3.Text = ""
            MessageBox.Show("No Data Found")
        End If
       

        con1.Close()


    End Sub

How to Copy Selected File to selected Folder Using VB.NET

 



Imports System.IO

Public Class Form3

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

        OpenFileDialog1.ShowDialog()
        TextBox1.Text = Path.GetFileName(OpenFileDialog1.FileName)
        TextBox2.Text = Path.GetDirectoryName(OpenFileDialog1.FileName) & "\" & Path.GetFileName(OpenFileDialog1.FileName)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FolderBrowserDialog1.ShowDialog()

        TextBox3.Text = FolderBrowserDialog1.SelectedPath.ToString


    End Sub

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

        If System.IO.File.Exists(TextBox3.Text & "\" & TextBox1.Text) Then

            'MsgBox("the file exists")

            File.AppendAllText(TextBox2.Text, TextBox3.Text & "\" & TextBox1.Text)

        Else

            File.Copy(TextBox2.Text, TextBox3.Text & "\" & TextBox1.Text)

            MsgBox("file copied successfully")


        End If





    End Sub

End Class

CSS text-transform Property(none-lowercase-uppercase-capitalize )

 div.a {

  text-transform: uppercase;

}


div.b {

  text-transform: lowercase;

}


div.c {

  text-transform: capitalize;

}

Default Value 
text-transform: none;

How to get value selected item from combobox in VB.NET-with source code

 


Imports System.Data.SqlClient


Public Class Form5


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

        Dim con As New SqlConnection(" data source=.;initial catalog=sports1;integrated security=true")


        Dim cmd As New SqlCommand("select id,name from sports", con)


        Dim da As New SqlDataAdapter


        da.SelectCommand = cmd


        Dim table1 As New DataTable


        da.Fill(table1)


        Dim row As DataRow = table1.NewRow


        row(0) = 0

        row(1) = "Select sport"


        table1.Rows.InsertAt(row, 0)


        ComboBox1.DataSource = table1

        ComboBox1.DisplayMember = "name"

        ComboBox1.ValueMember = "id"








    End Sub


    

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

        Label1.Text = ComboBox1.SelectedValue


    End Sub

End Class

How to insert image into a SQL database using Visual Basic.net(with Source Code+database)











Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form2


    Dim con1 As New SqlConnection("Server=.;Database=images;integrated security=true")

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

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


        Dim openfiledialog1 As New OpenFileDialog


        openfiledialog1.Filter = "images|*.jpg;*.png;*.gif;*.bmp"


        If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then



            PictureBox1.Image = Image.FromFile(openfiledialog1.FileName)


        End If

    End Sub

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


        Dim command1 As New SqlCommand("Insert into table1(name,age,img1)values(@name,@age,@img)", con1)

        command1.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text


        command1.Parameters.Add("@age", SqlDbType.VarChar).Value = TextBox2.Text



        Dim memstr As New MemoryStream


        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = memstr.ToArray


        If con1.State = ConnectionState.Closed Then

            con1.Open()


        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("image inserted")

        con1.Close()

        bind1()

    End Sub


    Private Sub bind1()

        Dim command1 As New SqlCommand("Select * from table1 order by id desc", con1)


        Dim adapter As New SqlDataAdapter(command1)


        Dim table As New DataTable()

        adapter.Fill(table)

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.RowTemplate.Height = 90


        Dim pic1 As New DataGridViewImageColumn

        DataGridView1.DataSource = table

        pic1 = DataGridView1.Columns(3)

        pic1.ImageLayout = DataGridViewImageCellLayout.Stretch


    End Sub



    Private Sub DataGridView1_Click(sender As Object, e As System.EventArgs) Handles DataGridView1.Click


        Label4.Text = DataGridView1.CurrentRow.Cells(0).Value

        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value


        TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value



        Dim img As Byte()

        img = DataGridView1.CurrentRow.Cells(3).Value

        Dim memstr As New MemoryStream(img)


        PictureBox1.Image = Image.FromStream(memstr)




    End Sub

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


        Dim memstr As New MemoryStream

        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)


        Dim img() As Byte

        img = memstr.ToArray()

        Dim update1 As String = "Update table1 set name='" & TextBox1.Text & "',age='" & TextBox2.Text & "',img1=@img where id=" & Label4.Text

        Dim command1 As New SqlCommand(update1, con1)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = img

        If con1.State = ConnectionState.Closed Then

            con1.Open()
        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image updated")

        con1.Close()

        bind1()

    End Sub

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


        Dim delete1 As String = "Delete from table1 where id=" & Label4.Text

        Dim command1 As New SqlCommand(delete1, con1)

        If con1.State = ConnectionState.Closed Then

            con1.Open()

        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image deleted")


        con1.Close()


        bind1()


    End Sub
End Class

How to populate combobox based on another combobox selection item in vb.net With Source Code



Imports System.Data.SqlClient

Public Class Form2

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

        Dim constr1 As String = "data source=.; initial catalog=names;integrated security=true"

        Using con1 As SqlConnection = New SqlConnection(constr1)


            Using sda1 As SqlDataAdapter = New SqlDataAdapter(" Select id,name from table1", con1)


                Dim dt As DataTable = New DataTable

                sda1.Fill(dt)



                Dim row As DataRow = dt.NewRow()

                row(0) = 0

                row(1) = "Select"

                dt.Rows.InsertAt(row, 0)

                ComboBox1.DataSource = dt

                ComboBox1.DisplayMember = "name"

                ComboBox1.ValueMember = "id"


            End Using


        End Using






    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged


        Dim con1 As SqlConnection = New SqlConnection("Data source=.\;initial catalog=names;integrated security=true")

        con1.Open()
        Dim strsql As String
        strsql = "Select tel from table1 where name='" + ComboBox1.Text + "'"

        Dim cmd As New SqlCommand(strsql, con1)

        Dim myreader As SqlClient.SqlDataReader

        myreader = cmd.ExecuteReader

        ComboBox2.Items.Clear()
        While myreader.Read


            ComboBox2.Items.Add(myreader("tel"))


        End While


        con1.Close()


    End Sub
End Class

How to attach database mdf file in sql server and fix an error occurred when attaching the database

 How to attach database mdf file in sql server and fix an error occurred when attaching the databases, Click the hyperlink in the Message column for detail.