Hello,
Can someone assist me how to create here buttons
- Button7 - First
- Button5 - Next
- Button6 - Previous
- Button8 - Last
To search in database and display the results.
Here is my project until now:
- Also if there are mistakes or suggestions to make it better i'm open for ideas to learn too :)
<pre>
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using con As New OleDbConnection(ServerStatus)
Using cmd As New OleDbCommand("SELECT * FROM Table", con)
cmd.CommandType = CommandType.Text
Using sda As New OleDbDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
'Set AutoGenerateColumns False
DataDisplay.AutoGenerateColumns = False
'DataDisplay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataDisplay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
DataDisplay.AutoResizeColumns()
'Set Columns Count
DataDisplay.ColumnCount = 6
'Add Columns
DataDisplay.Columns(0).Name = "ID"
DataDisplay.Columns(0).HeaderText = "ID"
DataDisplay.Columns(0).DataPropertyName = "ID"
DataDisplay.Columns(1).Name = "cName"
DataDisplay.Columns(1).HeaderText = "Name"
DataDisplay.Columns(1).DataPropertyName = "cName"
DataDisplay.Columns(2).Name = "cNumber"
DataDisplay.Columns(2).HeaderText = "Number"
DataDisplay.Columns(2).DataPropertyName = "cNumber"
DataDisplay.Columns(3).Name = "cSupplier"
DataDisplay.Columns(3).HeaderText = "Supplier"
DataDisplay.Columns(3).DataPropertyName = "cSupplier"
DataDisplay.Columns(4).Name = "cStore"
DataDisplay.Columns(4).HeaderText = "Store"
DataDisplay.Columns(4).DataPropertyName = "cStore"
DataDisplay.Columns(5).Name = "cCount"
DataDisplay.Columns(5).HeaderText = "Count"
DataDisplay.Columns(5).DataPropertyName = "cCount"
TextBox1.Text = dt.Rows(0).Item(1)
TextBox2.Text = dt.Rows(0).Item(2)
TextBox3.Text = dt.Rows(0).Item(3)
TextBox4.Text = dt.Rows(0).Item(4)
TextBox5.Text = dt.Rows(0).Item(5)
TextBox6.Text = dt.Rows(0).Item(0)
DataDisplay.DataSource = dt
End Using
End Using
End Using
End Using
Try
With cmd
Dim stream As New IO.MemoryStream()
conn.Open()
.Connection = conn
.CommandText = "select cPicture from Table where ID=@uID"
.Parameters.Add("@uID", OleDbType.Integer, 50).Value = TextBox6.Text
Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
Dim bitmap As New Bitmap(stream)
PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
stream.Close()
.Parameters.Clear()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Adapter.Update(dt)
Try
With cmd
Dim stream As New IO.MemoryStream()
conn.Open()
.Connection = conn
.CommandText = "select cPicture from Table where ID=@uID"
.Parameters.Add("@uID", OleDbType.Integer, 50).Value = TextBox6.Text
Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
Dim bitmap As New Bitmap(stream)
PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
stream.Close()
.Parameters.Clear()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dialog As New OpenFileDialog()
dialog.Title = "Browse Picture"
dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(dialog.FileName)
'TextBox1.Text = dialog.FileName.ToString
End If
End Sub
Private Sub RefreshData()
Using con As New OleDbConnection(ServerStatus)
Using cmd As New OleDbCommand("SELECT * FROM Table", con)
cmd.CommandType = CommandType.Text
Using sda As New OleDbDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
TextBox1.Text = dt.Rows(0).Item(1)
TextBox2.Text = dt.Rows(0).Item(2)
TextBox3.Text = dt.Rows(0).Item(3)
TextBox4.Text = dt.Rows(0).Item(4)
TextBox5.Text = dt.Rows(0).Item(5)
TextBox6.Text = dt.Rows(0).Item(0)
DataDisplay.DataSource = dt
End Using
End Using
End Using
End Using
Try
With cmd
Dim stream As New IO.MemoryStream()
conn.Open()
.Connection = conn
.CommandText = "select cPicture from Table where ID=@uID"
.Parameters.Add("@uID", OleDbType.Integer, 50).Value = TextBox6.Text
Dim image As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)
Dim bitmap As New Bitmap(stream)
PictureBox1.Image = bitmap '--->I have used another picturebox to display image from database.
stream.Close()
.Parameters.Clear()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
With cmd
Dim ms As New IO.MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim arrimage() As Byte = ms.GetBuffer
conn.Open()
.Connection = conn
.Parameters.Add("@uName", OleDbType.VarChar, 50).Value = TextBox1.Text
.Parameters.Add("@uNumber", OleDbType.Integer, 50).Value = TextBox2.Text
.Parameters.Add("@uSupp", OleDbType.VarChar, 50).Value = TextBox3.Text
.Parameters.Add("@uStore", OleDbType.VarChar, 50).Value = TextBox4.Text
.Parameters.Add("@uCount", OleDbType.Integer, 50).Value = TextBox5.Text
.Parameters.Add("@Picture", OleDbType.Binary).Value = arrimage
.Parameters.AddWithValue("@uID", TextBox6.Text)
.CommandText = "UPDATE Table SET cName = @uName,cNumber = @uNumber,cSupplier = @uSupp,cStore = @uStore,cCount = @uCount,cPicture = @Picture WHERE ID = @uID"
.ExecuteNonQuery()
.Parameters.Clear()
ms.Close()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
If conn IsNot Nothing Then
conn.Close()
End If
RefreshData()
End Try
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Try
With cmd
Dim ms As New IO.MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim arrimage() As Byte = ms.GetBuffer
conn.Open()
.Connection = conn
.CommandText = "INSERT INTO Table (cName,cNumber,cSupplier,cStore,cCount,cPicture) VALUES (@uName,@uYazaki,@uSupp,@uStore,@uCount,@picture)"
.Parameters.Add("@uName", OleDbType.VarChar, 50).Value = TextBox1.Text
.Parameters.Add("@uNumber", OleDbType.Integer, 50).Value = TextBox2.Text
.Parameters.Add("@uSupp", OleDbType.VarChar, 50).Value = TextBox3.Text
.Parameters.Add("@uStore", OleDbType.VarChar, 50).Value = TextBox4.Text
.Parameters.Add("@uCount", OleDbType.Integer, 50).Value = TextBox5.Text
.Parameters.Add("@Picture", OleDbType.Binary).Value = arrimage
.ExecuteNonQuery()
.Parameters.Clear()
ms.Close()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
If conn IsNot Nothing Then
conn.Close()
End If
End Try
End Sub
End Class</pre>
Georgi_1 0 Newbie Poster
SoftBa 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.