Hey!
I'm trying to connect my backend (Ms Access) to the front end (VB.NET 2010). I'm trying to retrieve data and I get these errors.
I have two problems:
1. For i = 0 To dt.Rows.Count - 1
when typed the above code in the load form I get the letter i underlined.
this is the error - Warning 1 The type for variable 'i' will not be inferred because it is bound to a field in an enclosing scope. Either change the name of 'i', or use the fully qualified name (for example, 'Me.i' or 'MyBase.i').
- When I debugged/run the code, the code doesn't work and I get this error
This code is higlighted --------
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
TextBox1.Text = dt.Rows(i)(0)
End Sub
Error name - IndexOutOfRangeException was unhandled
There is no row at position 0.
Can someone please help me!
Thank You!
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Byte
Imports System.String
Public Class frmBirthdayCake
Private sql = "SELECT CakeID,Cake_Name,Cake_Description,Weight,Price,Image,ShelfLife,Size,NoOfServings FROM Birthday_Cake"
Private dt As New DataTable
Dim i As Integer
Private adapter As New OleDbDataAdapter(sql, con)
Dim ms As New IO.MemoryStream
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\CakeAlbum.accdb")
Private Sub frmBirthdayCake_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
adapter.Fill(dt)
DataGridView1.DataSource = dt
DirectCast(DataGridView1.Columns("Image"), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
For i = 0 To dt.Rows.Count - 1
Dim row As DataGridViewRow = DataGridView1.Rows(i)
row.Height = 60
Next
DataGridView1.Columns("Image").Width = 150
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'If i < dt.Rows.Count - 1 Then
If TextBox1.Text = "" Then
i = 0
TextBox1.Text = dt.Rows(i)(0)
TextBox2.Text = dt.Rows(i)(1)
Dim imagebytes As Byte() = CType(dt.Rows(i)(2), Byte())
Using ms As New IO.MemoryStream(imagebytes)
PictureBox1.Image = Image.FromStream(ms)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
Else
Try
i += 1
TextBox1.Text = dt.Rows(i)(0)
TextBox2.Text = dt.Rows(i)(1)
Dim imagebytes As Byte() = CType(dt.Rows(i)(2), Byte())
Using ms As New IO.MemoryStream(imagebytes)
PictureBox1.Image = Image.FromStream(ms)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
Catch
i -= 1
MsgBox("End Of Records")
End Try
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If TextBox1.Text = "" Then
Else
If i = dt.Rows.Count - 1 OrElse i <> 0 Then
i -= 1
TextBox1.Text = dt.Rows(i)(0)
TextBox2.Text = dt.Rows(i)(1)
Dim imagebytes As Byte() = CType(dt.Rows(i)(2), Byte())
Using ms As New IO.MemoryStream(imagebytes)
PictureBox1.Image = Image.FromStream(ms)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
Else
MsgBox("This is the first record")
End If
End If
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
con.Open()
Dim sql = "SELECT Image FROM Birthday_Cake where CakeID='" & DataGridView1.CurrentRow.Cells(0).Value & "'"
adapter = New OleDbDataAdapter(sql, con)
Dim d As New DataTable
adapter.Fill(d)
TextBox1.Text = d.Rows(i)(0)
con.Close()
End Sub
End Class