You are on page 1of 4

Imports System.

IO
Imports System.Data.SqlClient
Public Class Form4
Dim sqlcon As New SqlConnection("Data Source=(localdb)\MSSQLLocalDB;Initial
Catalog=houcem;Integrated Security=True")
Dim ofd As New OpenFileDialog
Dim adapter As SqlDataAdapter
Dim ds As New DataSet

Sub Insertupload(ByVal conn As SqlConnection, ByVal strinsert As String)


Dim cmd As New SqlCommand(strinsert, conn)
Try
conn.Open()
cmd.ExecuteNonQuery()

Catch ex As Exception
MsgBox("error")

Finally
conn.Close()
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
ofd.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg;
*.jpeg; *.jpe; *.jfif; *.png|PDF files (*.pdf)|*.pdf ;"
If ofd.ShowDialog = DialogResult.OK Then
Dim klo2 As String = ""
Dim klo1 As String
Dim pos2 As Integer
Dim pos As Integer
pos = InStr(ofd.FileName, Path.GetExtension(ofd.FileName))
klo1 = Microsoft.VisualBasic.Left(ofd.FileName, pos - 1)
pos2 = InStr(ofd.FileName, "\")
While pos2 <> 0
klo2 = Microsoft.VisualBasic.Mid(klo1, pos2 + 1, pos - 1)
klo1 = klo2
pos2 = InStr(klo2, "\")
End While
TextBox1.Text = klo2 & Path.GetExtension(ofd.FileName)
textbox2.text = klo2
End If
Dim fs As New FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim file() As Byte = br.ReadBytes(br.BaseStream.Length)

Try

sqlcon.Open()

Dim strinsert As String = "INSERT INTO upload (label ,files,extension)


values (@label,@files ,@extension)"

Dim cmd As New SqlCommand(strinsert, sqlcon)


cmd.Parameters.Add(New SqlParameter("@label", SqlDbType.NVarChar,
50)).Value = TextBox2.Text
cmd.Parameters.Add(New SqlParameter("@files",
SqlDbType.VarBinary)).Value = file
cmd.Parameters.Add(New SqlParameter("@extension", SqlDbType.NChar,
10)).Value = Path.GetExtension(ofd.FileName)

cmd.ExecuteNonQuery()
ds.Clear()
Me.UploadTableAdapter1.Fill(Me.HoucemDataSet1.upload)
'dgvupload.
'Me.UploadTableAdapter.Fill(Me.HoucemDataSet.upload)

MsgBox("l'opération a été effectuer avec sucées ",


MsgBoxStyle.Information, "add")
Catch ex As Exception
MsgBox(ex.Message)
Finally
dgvupload.Rows(dgvupload.RowCount - 1).Selected = True
sqlcon.Close()
End Try

End Sub

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


'TODO: cette ligne de code charge les données dans la table
'HoucemDataSet1.upload'. Vous pouvez la déplacer ou la supprimer selon les besoins.
Me.UploadTableAdapter1.Fill(Me.HoucemDataSet1.upload)
'TODO: cette ligne de code charge les données dans la table
'HoucemDataSet.upload'. Vous pouvez la déplacer ou la supprimer selon les besoins.
'Me.UploadTableAdapter.Fill(Me.HoucemDataSet.upload)
'TODO: cette ligne de code charge les données dans la table
'HoucemDataSet.upload'. Vous pouvez la déplacer ou la supprimer selon les besoins.
'Me.UploadTableAdapter.Fill(Me.HoucemDataSet.upload)

End Sub

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


Button2.Click
' Dim cmd As SqlCommand

End Sub

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


Button3.Click

Me.Dispose()
End Sub

Private Sub dgvupload_CellContentClick(sender As Object, e As


DataGridViewCellEventArgs)

End Sub
Private Sub dgvupload_KeyDown(sender As Object, e As KeyEventArgs) Handles
dgvupload.KeyDown
If e.KeyCode = Keys.Delete Then

Try
If TextBox2.Text = "" Then
Exit Sub
End If
Dim strinsert As String = "delete from upload where ID = " &
CInt(TextBox2.Text) & ""

Dim cmd As New SqlCommand(strinsert, sqlcon)


'ds.Clear()
sqlcon.Open()
cmd.ExecuteNonQuery()
Me.UploadTableAdapter1.Fill(Me.HoucemDataSet1.upload)

'If Then
If dgvupload.RowCount > 0 Then
If CInt(TextBox3.Text) <= dgvupload.RowCount - 1 Then
dgvupload.Rows(CInt(TextBox3.Text)).Selected = True

TextBox2.Text = dgvupload(0, CInt(TextBox3.Text)).Value

Else
dgvupload.Rows(CInt(TextBox3.Text) - 1).Selected = True
TextBox2.Text = dgvupload(0, CInt(TextBox3.Text) - 1).Value
TextBox3.Text = (CInt(TextBox3.Text) - 1).ToString()
End If
End If
' End If
Catch ex As Exception
MsgBox(ex.Message)
Finally

sqlcon.Close()
End Try

End If
End Sub

Private Sub dgvupload_CellClick(sender As Object, e As


DataGridViewCellEventArgs) Handles dgvupload.CellClick
Dim i As Integer = dgvupload.CurrentRow.Index
TextBox2.Text = dgvupload(0, i).Value
TextBox3.Text = e.RowIndex 'dgvupload.CurrentRow.Index

End Sub

Private Sub dgvupload_CellContentClick_1(sender As Object, e As


DataGridViewCellEventArgs) Handles dgvupload.CellContentClick

End Sub
Private Sub Form4_Activated(sender As Object, e As EventArgs) Handles
Me.Activated
Me.WindowState = FormWindowState.Maximized
Me.FormBorderStyle = FormBorderStyle.None
TopMost = True
End Sub
End Class

You might also like