Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to setup a datagrid but I keep getting this error:

 

An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll

 

Additional information: Object reference not set to an instance of an object.

 

Here is my code:

The error here : dGridOneDay.TableStyles.Add(dGridTableStyle)

 

Dim adaptOneDay As New SqlClient.SqlDataAdapter("Select logtime, username from timelog", conSql)

Dim dsOneDay As DataSet

Dim dGridTableStyle As DataGridTableStyle

Dim dGridColumnTime As DataGridTextBoxColumn

Dim dGridColumnUser As DataGridTextBoxColumn

 

dGridOneDay.DataMember = "TimeLog"

dGridOneDay.DataSource = dsOneDay

dGridOneDay.HeaderForeColor = System.Drawing.SystemColors.ControlText

dGridOneDay.Location = New System.Drawing.Point(8, 152)

dGridOneDay.Name = "dGrid"

dGridOneDay.Size = New System.Drawing.Size(272, 208)

dGridOneDay.TabIndex = 2

dGridOneDay.TableStyles.Add(dGridTableStyle)

'dGridOneDay.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {dGridTableStyle})

dGridOneDay.Visible = False

 

dGridTableStyle.DataGrid = dGridOneDay

dGridTableStyle.GridColumnStyles.Add(dGridColumnTime)

dGridTableStyle.GridColumnStyles.Add(dGridColumnUser)

 

' dGridTableStyle.GridColumnStyles.AddRange(New System.Windows.Forms.DataGridColumnStyle() {dGridColumnTime, dGridColumnUser})

dGridTableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText

dGridTableStyle.MappingName = "TimeLog"

 

 

dGridColumnTime.Format = "MM/dd/yyyy hh:mm tt"

dGridColumnTime.FormatInfo = Nothing

dGridColumnTime.HeaderText = "Log Time"

dGridColumnTime.MappingName = "logTime"

dGridColumnTime.Width = 150

 

dGridColumnUser.Format = ""

dGridColumnUser.FormatInfo = Nothing

dGridColumnUser.HeaderText = "Username"

dGridColumnUser.MappingName = "username"

dGridColumnUser.Width = 75

 

adaptOneDay.Fill(dsOneDay, "Timelog")

 

 

For the life of me I do not know why....

 

Thanks

 

Lp

  • *Experts*
Posted

Unless you're setting the data classes to something else later

in the code, when you declare the variable you need to include

the New keyword to initialize a new instance of the class.

Otherwise, they will be equal to Nothing (null), and you can't do

anything with them. That's the cause of the error... you're passing

Nothing to the Add method.

 

I'm not familiar with the constructors of these classes, so you may

need to pass some parameters to the constructors.

 

Dim dsOneDay As New DataSet()
Dim dGridTableStyle As New DataGridTableStyle()
Dim dGridColumnTime As New DataGridTextBoxColumn()
Dim dGridColumnUser As New DataGridTextBoxColumn()

 

[edit]Added a little more info[/edit]

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...