D
dennist
I thought I had this mastered, but apparently I don't.
Here's the problem. On one tab of a form I have a listbox
of Authors. In the previous tab a topic is created,
usually an article. On the next tab the user can assign
the author(s) who wrote the article to the
TopicAuthorAssignments table. On the authors tab page I
created a connection - cnTAA, dataadapter, daTAA, and
dataset dsTAA.
I try to do the update in a class called clsDataChor,
public function NewTopicAuthorAssignment. Something goes
wrong. I set up the call to this function in the code
below:
Private Sub btnSaveTabAuthors_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSaveTabAuthors.Click
Dim clsDChor As New clsDataChor
If lstAuthors.SelectedIndex = -1 Then
MsgBox("No Author selected.")
cboTopicIssuers.Focus()
Exit Sub
End If
Dim i, idx, iValue, iValue1 As Integer
giCount = lstAuthors.SelectedIndices.Count - 1
giValue = CInt(txtID.Text)
clsDChor.NewTopicAuthorAssignment(Me)
For i = 0 To lstAuthors.SelectedIndices.Count - 1
idx = lstAuthors.SelectedIndices(i)
Dim drv As DataRowView
drv = lstAuthors.Items(idx)
'MessageBox.Show(drv.Row("ID")).ToString
() '["selectedvalue"]
'MessageBox.Show(drv.Row
[listBox1.ValueMember].ToString())
iValue = CInt(txtID.Text)
iValue1 = drv.Row("ID")
'MsgBox(iValue.ToString & " " &
iValue1.ToString)
Next
End Sub
giCount and giValue are public 'global' variables,
later used in the function.
Now the code of the function, with some comments on what
happens when I try alternatives, follows:
Public Function NewTopicAuthorAssignment(ByVal frm As
frmTopicFromStart) As Boolean
NewTopicAuthorAssignment = False
Try
frm.cnTAA.Open()
Catch er As Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
End Try
'frm.daTAA.Fill(dsTAA, "TopicAuthorAssignments")
' dstaa is a type and cannot be used as an
expression
frm.daTAA.Fill
(frm.DsTAA1, "TopicAuthorAssignments")
'Dim tblTAA As
frm..TopicAuthorAssignmentsDataTable
'if frm, nothing appears after .
'if frmtopicfromstart, 4 items appear after . but
none relevant to datasets or datatables
'if dstaa1, nothing appears after dot
'if I start typing d, dstaa appears
Dim tblTAA As
dsTAA.TopicAuthorAssignmentsDataTable
Dim rowTAA As dsTAA.TopicAuthorAssignmentsRow
Dim i, idx As Integer
For i = 0 To giCount
MsgBox(giCount.ToString)
rowTAA = tblTAA.NewTopicAuthorAssignmentsRow
'object reference not set to an instance of
an object.
rowTAA.TopicID = giValue
idx = frm.lstAuthors.SelectedIndices(i)
Dim drv As DataRowView
drv = frm.lstAuthors.Items(idx)
rowTAA.AuthorID = drv.Row("ID")
tblTAA.AddTopicAuthorAssignmentsRow
(rowTAA) '. as tfsnet2.dstaa.topicauthorassignmentsrow)
Next
If frm.DsTAA1.HasChanges Then
Try
Dim intTAAModified As Integer
intTAAModified = frm.daTAA.Update
(frm.DsTAA1.TopicAuthorAssignments)
Dim strOutput As String
strOutput = "Modified " & intTAAModified
& " TopicAuthorAssignment(s)"
MessageBox.Show(strOutput, "Update
succeeded!", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, "Update
failed!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else
MessageBox.Show("No changes to
submit!", "SubmitChanges", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Function
The error I get is as follows
rowTAA = tblTAA.NewTopicAuthorAssignmentsRow
'object reference not set to an instance of
an object.
in more detail,
An unhandled exception of
type 'System.NullReferenceException' occurred in
TFSNet2.exe
Additional information: Object reference not set to an
instance of an object
dennist
Here's the problem. On one tab of a form I have a listbox
of Authors. In the previous tab a topic is created,
usually an article. On the next tab the user can assign
the author(s) who wrote the article to the
TopicAuthorAssignments table. On the authors tab page I
created a connection - cnTAA, dataadapter, daTAA, and
dataset dsTAA.
I try to do the update in a class called clsDataChor,
public function NewTopicAuthorAssignment. Something goes
wrong. I set up the call to this function in the code
below:
Private Sub btnSaveTabAuthors_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnSaveTabAuthors.Click
Dim clsDChor As New clsDataChor
If lstAuthors.SelectedIndex = -1 Then
MsgBox("No Author selected.")
cboTopicIssuers.Focus()
Exit Sub
End If
Dim i, idx, iValue, iValue1 As Integer
giCount = lstAuthors.SelectedIndices.Count - 1
giValue = CInt(txtID.Text)
clsDChor.NewTopicAuthorAssignment(Me)
For i = 0 To lstAuthors.SelectedIndices.Count - 1
idx = lstAuthors.SelectedIndices(i)
Dim drv As DataRowView
drv = lstAuthors.Items(idx)
'MessageBox.Show(drv.Row("ID")).ToString
() '["selectedvalue"]
'MessageBox.Show(drv.Row
[listBox1.ValueMember].ToString())
iValue = CInt(txtID.Text)
iValue1 = drv.Row("ID")
'MsgBox(iValue.ToString & " " &
iValue1.ToString)
Next
End Sub
giCount and giValue are public 'global' variables,
later used in the function.
Now the code of the function, with some comments on what
happens when I try alternatives, follows:
Public Function NewTopicAuthorAssignment(ByVal frm As
frmTopicFromStart) As Boolean
NewTopicAuthorAssignment = False
Try
frm.cnTAA.Open()
Catch er As Exception
MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
End Try
'frm.daTAA.Fill(dsTAA, "TopicAuthorAssignments")
' dstaa is a type and cannot be used as an
expression
frm.daTAA.Fill
(frm.DsTAA1, "TopicAuthorAssignments")
'Dim tblTAA As
frm..TopicAuthorAssignmentsDataTable
'if frm, nothing appears after .
'if frmtopicfromstart, 4 items appear after . but
none relevant to datasets or datatables
'if dstaa1, nothing appears after dot
'if I start typing d, dstaa appears
Dim tblTAA As
dsTAA.TopicAuthorAssignmentsDataTable
Dim rowTAA As dsTAA.TopicAuthorAssignmentsRow
Dim i, idx As Integer
For i = 0 To giCount
MsgBox(giCount.ToString)
rowTAA = tblTAA.NewTopicAuthorAssignmentsRow
'object reference not set to an instance of
an object.
rowTAA.TopicID = giValue
idx = frm.lstAuthors.SelectedIndices(i)
Dim drv As DataRowView
drv = frm.lstAuthors.Items(idx)
rowTAA.AuthorID = drv.Row("ID")
tblTAA.AddTopicAuthorAssignmentsRow
(rowTAA) '. as tfsnet2.dstaa.topicauthorassignmentsrow)
Next
If frm.DsTAA1.HasChanges Then
Try
Dim intTAAModified As Integer
intTAAModified = frm.daTAA.Update
(frm.DsTAA1.TopicAuthorAssignments)
Dim strOutput As String
strOutput = "Modified " & intTAAModified
& " TopicAuthorAssignment(s)"
MessageBox.Show(strOutput, "Update
succeeded!", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, "Update
failed!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else
MessageBox.Show("No changes to
submit!", "SubmitChanges", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Function
The error I get is as follows
rowTAA = tblTAA.NewTopicAuthorAssignmentsRow
'object reference not set to an instance of
an object.
in more detail,
An unhandled exception of
type 'System.NullReferenceException' occurred in
TFSNet2.exe
Additional information: Object reference not set to an
instance of an object
dennist