error message when pressing save button

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
Hi,

Can anyone please explain to me, what I'm I doing wrong here and how can I fix it?

1.jpg

Thank you
 
From the screen shot you provided, the error should be exactly as shown "InvalidCastException".

The item that you have in ApacheCheckedListBox.CheckedItems(0) contains a string value of "mod_alias". You are trying to cast it to a Boolean type implicitly since in the If block you are comparing that string value with a Boolean value "True".

To fix this, ensure that you are comparing the object with the same type. For example something like this should remove the exception:

If ApacheCheckedListBox.CheckedItems(0) = "mod_alias" Then
    'do something
Else
    'do something else
End If


but note, it may not give the desired results, you will have to compare it with whatever value that fits your purpose...
 
Hey JohnH.

Basically what I want to do is:

if ApacheCheckedListBox item mod_alias.checked = true, then
replace "#LoadModule alias" with "LoadModule alias" in httpd.conf. Basically remove the #.

or even better, if I could tell it that:

if ApacheCheckedListBox item mod_alias.checked = true then
go to line 59 in httpd.conf and if that line StartsWith (#) then remove it.

I just could not figure out the correct logic behind doing this. Could you provide an example using CheckedListBox.GetItemChecked Method?

Thank you.
 
Maybe you can do a For-Each item in CheckedItems and use a Select Case on the item, ie Case "mod_alias" handle that.
 
For some reason this replaces the whole file with "LoadModule actions_module modules/mod_actions.so"

VB.NET:
Dim apacheconf = "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd.conf"
Dim Line() As String = System.IO.File.ReadAllLines(apacheconf)

Private Sub SaveApache_Click(sender As System.Object, e As System.EventArgs) Handles SaveApache.Click

        If ApacheCheckedListBox.CheckedItems(0) = "mod_actions" = False Then
            IO.File.WriteAllText((apacheconf), Line(59).Replace("LoadModule", "#LoadModule"))
        End If
        
End Sub

Any ideas why?
 
Because that's what you're telling it to do. WriteAllText, as the documentation says, overwrites the file with the text you specify. Your Replace call returns just the contents of that one line. You should be replacing that line in the array first, then using WriteAllLines to write out the entire array to the file.
 
Back
Top