Can't addnew in child grids

G

Guest

Can't addnew in child grids.

Haven't been progressing on this lately because of the Holy Days in Israel
and the call of other work.

Kevin Yu has been instrumental in helping me so far.

Have a keywordset table bound to textboxes and a checkbox, with the usual
buttons for moving position, adding, deleting, updating the dataset and
updating the database through the data adapter.

All work fine.

There are two child tables, keywords and searchphrase, related in the
dataset's xsd file and in the database itself. Each of the grids in which
the data is displayed have edit, add and delete buttons. The grids are read
only. When the add or edit button is clicked a small editkeyword or
editsearchprhase form is opened up, tied to the main form by a currency
manager.

Delete works fine. Edit works fine. But add doesn't work. So I decided to
catch errors, the edit form doesn't open up at all. Without errors it does,
but the add
doesn't work. Here's some code.

Private Sub frmFullWord_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
cnWKS.ConnectionString = gstrconn
cm = CType(BindingContext(dsWKS1, "KeyWordSets"), CurrencyManager)

AddHandler cm.ItemChanged, AddressOf cm_ItemChanged
AddHandler cm.PositionChanged, AddressOf cm_PositionChanged
AddHandler daWKS.RowUpdated, AddressOf OnRowUpDated
cmK = CType(BindingContext(dsWKS1,
"KeywordSets.KeywordSetsKeywords"), CurrencyManager)
cmSP = CType(BindingContext(dsWKS1,
"KeywordSets.KeywordSetsSearchPhrase"), CurrencyManager)
daWKS.Fill(dsWKS1, "KeywordSets")
Me.dsWKS1.KeywordSets.ActiveColumn.DefaultValue = True
daWK.Fill(dsWKS1, "Keywords")
daWSP.Fill(dsWKS1, "SearchPhrase")
Catch ex As OleDbException
If ex.Errors(0).SQLState = "3022" Then
MsgBox(ex.Errors(0).Message & s & "Please try again")
dsWKS1.Clear()
daWKS.Fill(dsWKS1, "KeywordSets")
daWK.Fill(dsWKS1, "Keywords")
daWSP.Fill(dsWKS1, "SearchPhrase")
Exit Try
Else
Dim errorMessages As String
Dim i As Integer = 0
errorMessages += "Index #" & i.ToString() & ControlChars.Cr _
& "Message: " & ex.Errors(i).Message &
ControlChars.Cr _
& "NativeError: " & ex.Errors(i).NativeError
& ControlChars.Cr _
& "Source: " & ex.Errors(i).Source &
ControlChars.Cr _
& "SQLState: " & ex.Errors(i).SQLState &
ControlChars.Cr _
& "StackTrace: " & ex.stacktrace &
ControlChars.Cr _
& ""
MsgBox(errorMessages)
End If

Catch ex As NullReferenceException
Exit Try

Catch ex As InvalidCastException
Exit Try

Catch ex As DBConcurrencyException
MsgBox(ex.Message & s & "The dataset will be refreshed." & s &
"Then you can navigate to the row and update it again.")
dsWKS1.Clear()
daWKS.Fill(dsWKS1, "KeywordSets")
daWK.Fill(dsWKS1, "Keywords")
daWSP.Fill(dsWKS1, "SearchPhrase")
Exit Try

Catch ex As Exception
MsgBox(ex.GetType.ToString & s & ex.Message & s & ex.HelpLink &
s & ex.StackTrace & s & ex.Source & s & "The form will be closed") '& s &
ex.TargetSite)
'Me.Close()


End Try

SetEditMode(False)

End Sub

and the add buttons for keywords ( searchphrase is similar)

Private Sub btnAddK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddK.Click
If cm.Count > 0 Then
Try
cmK.AddNew()
Me.dsWKS1.Keywords.ActiveColumn.DefaultValue = True
Dim frmEditKeyword As New frmEditKeyword
With frmEditKeyword
.EditKeyword(cmK)
.KeywordSetID = Me.KSID
.txtKeywordSetID.Text = frmEditKeyword.KeywordSetID
.ShowDialog()
End With
frmEditKeyword.Dispose()
Catch ex As Exception
MsgBox(ex.GetType.ToString & s & ex.Message & s &
ex.HelpLink & s & ex.StackTrace & s & ex.Source & s & "The form will be
closed") '& s & ex.TargetSite)

Finally

End Try
Else
MessageBox.Show("No Current KeywordSet!", "Add Keyword",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

Here are excerpts from the error message when I click the keyword add
button:

system.invalidcastexception
object cannot be cast from dbnull to other types
...
at system.windowsforms.unsafenativemethods.intcreatewindowex
...
at fullword.vb line 967

With frmEditKeyword
.EditKeyword(cmK)
.KeywordSetID = Me.KSID
.txtKeywordSetID.Text = frmEditKeyword.KeywordSetID
.ShowDialog()
End With

line 967 is .showidialog.

The error message on add searchphrase:
...
edit fullword.vb line 991

line 156 is

line 988 is .showdialog

if i comment out the try..end try lines and show dialog, then when I
click addK or addSP I get another row in the respective grid, but can't edit
the keyword or searchphrase, because the grid is read-only.

However, the edit boxes work fine, even with the .showdialog and without the
try..endtry, etc.

can somebody help me?

dennis
 
G

Guest

I've been told the usual mvp who helps me on this application is on vacation.
Perhaps a different mvp can throw me a line?

thanks,

dennis
 
K

Kevin Yu [MSFT]

Hi Dennis,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you're receiving an exception, which
says "object cannot be cast from dbnull to other types". If there is any
misunderstanding, please feel free to let me know.

Based on the code you have provided, it seems that this might happen when
someone is trying to cast a DBNull value to other types like int. This
usually happens in a new row, because the new row doesn't have value
assigned. Could you let me know, on which line did the debugger exactly
stopped? Also, sending me a copy of the project will be much helpful for me
to debug it. Thanks for your cooperation!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin!

Must have been a short vacation. Hope it was fun and you didn't bring a
laptop. On one hand I'm obviously glad you're back. On the other hand, you
deserve to get away from this stuff regularly.

Here's what I did. I enabled the try, catch end try and showdialog in addK
(keywords). I clicked the add button and the line was 967, which is
..ShowDialog()

Then, for no good reason I clicked the add button again. Got a different
dbnull error message that specified that the keyword field cannot be null.

Now I took this, as usual from chapter 13 of ado.net core reference and he
has no such problems.

In the meantime, I will send you the code.

Thanks muchly.

dennis
 
K

Kevin Yu [MSFT]

Hi Dennis,

Thank you! Yes, I've just been back from the vacation.

From the code, I noticed that this issue is the exactly the same as the
know issue I mentioned last time. :) Since Active is a boolean field, we
have to set a default value for it, or the newly added row has a value
DBNull, which cannot be converted to a value that the datagrid knows how to
display it.

After I have set a default value in the schema dsWKS for that column to
true, everything works fine. Of course, don't forget to add default value
for Active column in SearchPhrase table. It's also a boolean field and has
the same problem.

HTH. Good luck!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin, I typed True into the default property for active in the keywords
table and got the following errors when I tried to build

H:\HasbaraNET\ado.net tests\TFSNet2\dsWKS.xsd(1): Custom tool error: Unable
to convert input xml file content to a DataSet. The value 'True' is invalid
according to its data type. An error occurred at H:\HasbaraNET\ado.net
tests\TFSNet2\dsWKS.xsd, (21, 9).
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(53): Type 'TFSNet2.dsWKS' is
not defined.
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(104): Type 'TFSNet2.dsWKS'
is not defined.
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(688): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(690): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(691): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(696): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(697): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(698): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(722): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(723): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(724): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(826): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(827): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:
H:\HasbaraNET\ado.net tests\TFSNet2\FullWord.vb(828): Overload resolution
failed because no accessible 'Fill' can be called with these arguments:

Dennis
 
G

Guest

Kevin,

After I received the errors above I checked the KeywordSets table and
noticed that no default value was set in the xsd file.

Instead I added to the form load event

Me.dsWKS1.Keywords.ActiveColumn.DefaultValue = True
Me.dsWKS1.SearchPhrase.ActiveColumn.DefaultValue = True

to go along with your suggestion in the previous problem. You didn't have
me set the default value in the xsd file, you had me put in

Me.dsWKS1.KeywordSets.ActiveColumn.DefaultValue = True

in the form load event.

Now it works fine ado.net wise. There's a display problem, which I'll
submit to the windowsforms.datagrid or controls newsgroup.

thank you very much.

Dennis
 
K

Kevin Yu [MSFT]

Hi Dennis,

Setting the default value in schema or in code results in the same. I also
found that the projects may not be included when rebuilding. Try the
following:

1. Right click the solution in Solution Explorer in select Properties.
2. Select Configuration properties from the left pane.
3. Make sure that the Build checkbox is check for TFSNet project, so that
it will be included when rebuilding.

It was nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top