Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

I am trying to load datagridview dynamically from values from database.
For instance i have 2 buttons Button1 and Button2. i click on Button1 and load the first table in datagridview dynamically. But when i click on Button2 I get an error "NullreferenceException was unhandled - Object variable or With block variable not set."
I have cleared the datasource that fills the datagridview. I works fine the first time.
Please help

Thankyou

My Code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt As New DataTable
dt.Clear()
datagridview1.DataSource = dt
datagridview1.DataSource = Nothing
datagridview1.Refresh()
Dim cmd As New SqlCommand("select srno, name from table1", sql)
Dim dt As New DataSet()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
datagridview1.DataSource = dt.Tables(0)
datagridview1.AutoGenerateColumns = False
datagridview1.Columns("srno").headerText = "Id"
datagridview1.Columns("name").headerText = "Full Name"
End sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dt As New DataTable
dt.Clear()
datagridview1.DataSource = dt
datagridview1.DataSource = Nothing
datagridview1.Refresh()
Dim cmd As New SqlCommand("select serial, Items from table2", sql)
Dim dt As New DataSet()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
datagridview1.DataSource = dt.Tables(0)
datagridview1.AutoGenerateColumns = False
datagridview1.Columns("serial").headerText = "Id"
datagridview1.Columns("Items").headerText = "Name"
End sub
Posted
Comments
j snooze 17-Jun-14 16:05pm    
Did you debug it to see what line is throwing the error? Seems to me that maybe your data has a null value in it, might not be the code, but bad data.
JPais 18-Jun-14 0:31am    
On clicking button1 the datagridview loads correctly and on clicking button2 I am getting the error at this line
datagridview1.Columns("serial").headerText = "Id"
On debugging I found out that the column contains nothing and not "serial" for button2.

Do as below
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ResetDataGridView()
    Dim con as New SqlConnection(conString)
    Dim cmd As New SqlCommand("select srno as Id , name as 'Full Name' from table1", con)
    Dim dt As New DataSet()
    Dim sda As New SqlDataAdapter(cmd)
    sda.Fill(dt)
    datagridview1.DataSource = dt.Tables(0)
    con.Close()
End sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    ResetDataGridView()
    Dim con as New SqlConnection(conString)
    Dim cmd As New SqlCommand("select serial as Id, Items as Name from table2", con)
    Dim dt As New DataSet()
    Dim sda As New SqlDataAdapter(cmd)
    sda.Fill(dt)
    datagridview1.DataSource = dt.Tables(0)
    con.Close()
End sub

VB
Private Sub ResetDataGridView()
    dataGridView1.CancelEdit()
    dataGridView1.Columns.Clear()
    dataGridView1.DataSource = Nothing
End Sub
 
Share this answer
 
v2
Comments
JPais 18-Jun-14 0:32am    
Thank you. I'll try and let you know.
JPais 18-Jun-14 12:17pm    
It din't work. I am still getting the same error. The datagridview.rows.count on the second click is 0.
Solved it by setting
VB
datagridview1.AutoGenerateColumns = True

Thank you for your replies.
 
Share this answer
 

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