Programming for Everybody: update
Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

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

Insert,Update and Delete Statements in SQL Server

INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);


UPDATE table_name
SET column1 = value1, column2 = value2, ... WHERE condition;



DELETE FROM table_name WHERE condition;












insert update delete data in datagridview without using database in visual basic.net




Imports System.Data.DataTable
Public Class Form2
    Dim table As New DataTable("table")
    Dim index As Integer





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

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

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

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

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


        DataGridView1.DataSource = table







    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        table.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)

        DataGridView1.DataSource = table




    End Sub

   

  

   

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

        index = e.RowIndex

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














    End Sub

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

        Dim newdata As DataGridViewRow


        newdata = DataGridView1.Rows(index)


        newdata.Cells(0).Value = TextBox1.Text

        newdata.Cells(1).Value = TextBox2.Text


        newdata.Cells(2).Value = TextBox3.Text



        newdata.Cells(3).Value = TextBox4.Text
















    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""


        TextBox2.Text = ""



        TextBox3.Text = ""



        TextBox4.Text = ""







    End Sub

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

        DataGridView1.Rows.RemoveAt(index)








    End Sub
End Class