Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I saw this code in the internet:
VB
Dim bs As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim conn As New SqlConnection("Server = .\SQLExpress;Database = NorthWind; Integrated Security = SSPI;") 
    Dim dt As New DataTable

    Dim da As New SqlDataAdapter("Select * from Categories", conn) 
    da.Fill(dt) 
    bs.DataSource = dt

    DataRepeater1.DataSource = bs 
End Sub

The DataRepeater has a DrawItem event which allows us to put the data in the controls for each item. You can also use this event for formatting the data for display
VB
Private Function GetBitmap(ByVal Pic() As Byte) As Bitmap 
    Dim ms As New System.IO.MemoryStream 
    Dim bm As Bitmap 
    ms.Write(Pic, 78, Pic.Length - 78) 
    bm = New Bitmap(ms) 
    Return bm 
End Function

Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles DataRepeater1.DrawItem 
    Dim currItem As DataRowView = bs.Item(e.DataRepeaterItem.ItemIndex)

    DirectCast(e.DataRepeaterItem.Controls("pbCategory"), PictureBox).Image = GetBitmap(DirectCast(currItem.Item("Picture"), Byte())) 
    DirectCast(e.DataRepeaterItem.Controls("lblName"), Label).Text = currItem.Item("CategoryName").ToString 
End Sub

My problem is I encountered an error "Object reference not set to an instance of an object." on the line "
VB
DirectCast(e.DataRepeaterItem.Controls("pbCategory"), PictureBox).Image = GetBitmap(DirectCast(currItem.Item("Picture"), Byte()))"

Can you guys tell me whats causing me this error? Thanks in advance :)
Posted
Updated 13-Jul-12 7:59am
v2

1 solution

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

For now, it seems like:
currItem.Item("Picture") or e.DataRepeaterItem.Controls("pbCategory") is Nothing.
 
Share this answer
 
Comments
Sander Rossel 13-Jul-12 14:39pm    
Of course, 5'd :)
Sandeep Mewara 13-Jul-12 15:22pm    
Thanks Naerling. :)
gaga blues 13-Jul-12 23:16pm    
Thanks Sandeep for the help :)
I though the code

da.Fill(dt)
bs.DataSource = dt
DataRepeater1.DataSource = bs
fills
Dim currItem As DataRowView = bs.Item(e.DataRepeaterItem.ItemIndex)?
So how am i gonna pass values to currItem?

And this code i dont quite understand
e.DataRepeaterItem.ItemIndex
Thanks again and I hope you can help me grasp these codes. Thanks!
Sandeep Mewara 14-Jul-12 6:41am    
You wrote the code and have no clue on what they do and where from they came? I am confused!
gaga blues 14-Jul-12 6:53am    
Actually what i said was "I saw this code in the internet" so actually it was not my code. I am asking question regarding this one because I tried to apply this and I'm receiving errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900