Community server library - issue deleting a user - vb.net

I have been asked to fix a Community Server forum where thousands of users were created via a script. All of their profile pages are SEOspam for prescription drugs, etc. The forum was not using email verification or admin approval for newly registered users. I turned on the latter for now, but captcha would be nice.
My problem is that it is very cumbersome to mass delete these accounts. I set up a .net grid (Telerik Radgrid actually) so that I could mass select users and click delete. However the following code does not seem to be working (mind the VB nubbery):
Protected Sub rgUsers_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rgUsers.NeedDataSource
rgUsers.DataSource = Users.GetUsers().Users()
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
For Each item As GridDataItem In rgUsers.SelectedItems
Dim selectedUserID As Integer = item.OwnerTableView.DataKeyValues(item.ItemIndex)("UserID")
Dim userToDelete As CommunityServer.Components.User = Users.GetUser(selectedUserID, False) ' User is definitely populated. '
Dim username As String = userToDelete.Username
Dim deleteStatus As DeleteUserStatus = Users.DeleteUser(User)
Trace.Write(String.Format("Delete result for user {0}: {1}", username, deleteStatus.ToString)) ' Returns enum value 3 (Success.) '
Next
rgUsers.Rebind()
End Sub
The UserDeleteStatus result returns 'Success', however the user is not actually deleted. Am i using the correct delete function? Any help is greatly appreciated, as this is sort of time sensitive (the client is not in the market for penis enlargment pills.)

The issue was that the UserDeleteStatus was actually returning 'AuthenticationRequired'

Related

Reading Applications and services log

I want to read a custom event log which is stored under Applications and services log section in Windows Eventlog.
Unfortunately when calling the Log according to its naming properties I receive an error message that the log cannot be found.
Ulitmately I try read event details from events with a specific ID but first I need to able to access the log.
This is the code that I have so far:
Imports System
Imports System.Diagnostics.Eventing.Reader
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim query As New EventLog("Logname as per Properties", System.Environment.MachineName)
Dim elEventEntry As System.Diagnostics.EventLogEntry
Dim nmbr As Integer = query.Entries.Count
MsgBox(nmbr)
End Sub
End Class
This is the structure in the eventlog (I want to read the blue highlighted part)
Anybody any idea how to determine the correct log name?
Thx & BR
Daniel
For many of the event logs, you need to use an EventLogQuery.
As an example, if you wanted to query the "Setup" event log to count the number of entries with an EventID of 1, you could do this:
Imports System.Diagnostics.Eventing.Reader
Module Module1
Sub Main()
Dim query As New EventLogQuery("Setup", PathType.LogName, "*[System/EventID=1]")
Dim nEvents = 0
Using logReader = New EventLogReader(query)
Dim eventInstance As EventRecord = logReader.ReadEvent()
While Not eventInstance Is Nothing
nEvents += 1
eventInstance = logReader.ReadEvent()
End While
End Using
Console.WriteLine(nEvents)
Console.ReadLine()
End Sub
End Module
You can see the names of the items to query by looking at the XML for an event in Windows Event Viewer.
The Using construct makes sure that the EventLogReader is properly disposed of after it's been used.
Further information: How to: Access and Read Event Information (from Microsoft).

Google Account Registering Bot

thanks for spending time on my post.
I'm currently experiencing a easier way to create a Google account, and apparently there is no Google account creating bot or such thing like that, so I decided to write one my self.(Well, It's not a actual bot, it asks you to manually enter the Captcha.) Then I had a problem when setting a value of a combo box and when grabbing the captcha image to a picture box.....
Here's a part of my code(where the error occurred):
'WebBrowser1 has been navigated to "https://accounts.google.com/SignUp?continue=https:%2F%2Faccounts.google.com%2FManageAccount"
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim r As System.Random = New System.Random
'Sets the value of other textboxes
WebBrowser1.Document.GetElementById(":0").InnerText = MonthSelect(r.Next(1, 12))
WebBrowser1.Document.GetElementById(":d").InnerText = GenderSelect(r.Next(0, 1))
Captcha.ImageLocation = WebBrowser1.Document.GetElementById("recaptcha_chanllenge_image").GetAttribute("src")
End Sub
Public Function MonthSelect(mon As Integer)
'Generates a month
End Function
Public Function GenderSelect(gen As Integer)
'Generates a gender
End Function
And the NullReferenceException occurred in these 3 lines...
WebBrowser1.Document.GetElementById(":0").InnerText = MonthSelect(r.Next(1, 12))
WebBrowser1.Document.GetElementById(":d").InnerText = GenderSelect(r.Next(0, 1))
Captcha.ImageLocation = WebBrowser1.Document.GetElementById("recaptcha_chanllenge_image").GetAttribute("src")
What's wrong?

VB.NET 2008 - Input to data to website controls and download results

this is my first Q on this website so let me know if I have missed any important details, and thanks in advance.
I have been asked to access a website and download the results from a user-inputted form. The website asks for a username/password and once accepted, several questions which are used to generate several answers.
Since I am unfamiliar with this area I have set up a simple windows form to tinker around with websites and try to pick things up. I have used a webbrowser control and a button to use it to view the website in question.
When I try to view the website through the control, I just get script errors and nothing loads up. I am guessing I am missing certain plug-ins on my form that IE can handle without errors. Is there anyway I can identify what these are and figure out what to do next? I am stumped.
The script errors are:
"Expected identifier, string or number" and
"The value of the property 'setsection' is null or undefined"
Both ask if I want to continue running scripts on the page. But it works in IE and I cannot see why my control is so different. It actually request a username and password which works fine, it is the next step that errors.
I can provide screenies or an extract from the website source html if needed.
Thanks,
Fwiw my code is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("http://website.com")
'WebBrowser1.Navigate("http://www.google.com")
End Sub
Thanks for Noseratio I have managed to get somewhere with this.
Even though the errors I was getting seemed to be related to some XML/Java/Whatever functionality going askew it was actually because my webbrowser control was using ie 7.0
I forced it into using ie 9 and all is now well. So, using my above example I basically did something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'WebBrowser1.ScriptErrorsSuppressed = True
BrowserUpdate()
WebBrowser1.Navigate("http://website.com")
'WebBrowser1.Navigate("http://www.google.com")
End Sub
Sub BrowserUpdate()
Try
Dim IEVAlue As String = 9000 ' can be: 9999 , 9000, 8888, 8000, 7000
Dim targetApplication As String = Process.GetCurrentProcess.ToString & ".exe"
Dim localMachine As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim parentKeyLocation As String = "SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl"
Dim keyName As String = "FEATURE_BROWSER_EMULATION"
Dim subKey As Microsoft.Win32.RegistryKey = localMachine.CreateSubKey(parentKeyLocation & "\" & keyName)
subKey.SetValue(targetApplication, IEVAlue, Microsoft.Win32.RegistryValueKind.DWord)
Catch ex As Exception
'Blah blah here
End Try
End Sub

Do Variables save when the form closes?

Take this example:
I have a LoginForm and when I enter my credentials into the text box and click Go it the directs me to my main HomeForm. On this event it stores the current user text from the User Textbox in the Login form in a Public Variable in the HomeForm called CurrentUser So my code is like this:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Home.CurrentUser = UsernameTextBox.text
End Sub
When I then try to access the information stored in the variable I am having no problems it's just that I want to know if the user closes the HomeForm will the variable still equal to the previous value before the user closed the Form. And if not how would you recommend on saving. I Don't want to be using stream readers/writers due to all the unnecessary text files.
Any help is greatly appreciated!
If you declare CurrentUser as Shared then it will keep the value even when the form is closed and then re-opened (assuming the whole application hasn't been closed):
Public Class HomeForm
Public Shared CurrentUser As String
End Class
Access it using the Forms name:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
HomeForm.CurrentUser = UsernameTextBox.text
End Sub
*If you are looking at saving this value across application runs, then add a "CurrentUser" value in Project --> Properties --> Settings, then use code like:
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
My.Settings.CurrentUser = UsernameTextBox.text
My.Settings.Save()
End Sub
You can retrieve the value from anywhere using: My.Settings.CurrentUser
As long as you access the same instance of your HomeForm class, the variable values will still be there for you to access after the user has closed the form.
This applies to any fields or properties that do not access any of the inherited properties or methods of the Form class. Such methods by Form may depend on internal resources that are indeed freed upon closing or disposing of the form, while simple CLR properties will remain untouched for as long as the instance is within scope.
Dim onearray() As String
Dim i As Integer
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Home.CurrentUser = UsernameTextBox.text
Home.CurrentUser = onearray(i)
i = i + 1
End Sub
It depends on how you are calling your form from the login form you described. Calling it with the show method will dispose the form after the user closes the form. Please see comments below for explanations and behavior this may cause.
According to MSDN
The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed.
Based on further examination it is possible to read a simple variable even if the form is disposed. However, in my opinion I would approach saving the needed data by passing in an object or variable as outlined below.
There are several ways you can persist or save the Current User information in order to safely access it later. Here is one.
Create a class called CurrentUser and give it basic public properties you can get and set.
Add a New Constructor to HomeForm and pass a byref argument reference of the new CurrentUser object.
Set or get any information needed from the CurrentUser object
When the form closes you will have the information needed from the CurrentUser object passed into the form. Ensure the scope of this object is avaliable to other parts of your as needed. (i.e make it a public property in the form that calls the Home form.)
dim ouser as new CurrentUser
dim frmHomeForm as new HomeForm(ouser)
frmHomeForm.show
msgbox(ouser.Name)
If you want to save the value after the user closes the application then you can add a setting in the Project Properties, and just refer to My.Settings.YourSettingName instead of using a variable to get and set the users login name.
You can use Settings (Click Prject -> Project Settings -> settings -> Add setting) if you want to access it another time when you open your program variables might not save all the time only in case how you will use them.

How to use two forms to search in one and display the records to the other using Visual Basic 2010, SQL Server 2008

I have 2 forms in my visual basic 2010 project. Both are connected with an SQL 2008 Server where i keep a db with customers records(name,surname,address, etc).
What i want to do is to give certain criteria in the first form, lets say name,and hit a search button. The program should search the db if the specified name exists.If it exists i want to show the results in the second form. By results i mean the name , surname, address,etc. If it does not exist i want to prompt the user to insert data into the db.
The problem is that i know how to do this work in a single form but not how to make the two forms "communicate" with each other (meaning that one does the search and the other the projection-adding part).
Any help is appreciated
Edit
I am sorry guys but i cannot figured it out. I tried setting the filters but all i get is errors. I will post whatever i have written so far in case someone can help me out. So here it is.
As i stated earlier i know how to search for the data in a single form(lets call it DisplayForm). As Olivier mentioned i have textboxes for the records, a combo box with the search criteria(name , surname,id) and a Search button that perfoms the search and populates the text boxes. The code for the search button is this:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim intPosition As Integer
'Combo box code
Select Case cboField.SelectedIndex
Case 0
ObjDataView.Sort = "surname"
Case 1
ObjDataView.Sort = "id"
End Select
If cboField.SelectedIndex = 0 Then
intPosition = ObjDataView.Find(txtSearch.Text)
ElseIf cboField.SelectedIndex = 1 Then
intPosition = ObjDataView.Find(CType(txtSearch.Text, Integer))
End If
If intPosition = -1 Then
MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Else
objCurrencyManager.Position = intPosition
End If
End Sub
I want this procedure be done in a second form (lets call it FormSearch) and populate the textboxes in form DisplayForm. I know that it is easy enough for someone that knows VB but i am tottally a newbie and desperatly need help.
Thanks a lot for any advice
In your display form add this code
Public Sub SetFilter(name as String)
' Set the filter here
End Sub
In your filter form do this
DirectCast(Application.OpenForms(0), DisplayForm).SetFilter(NameTextBox.Text)
Here I assume that DisplayForm is your main form and that it's name is DisplayForm. You would call this in a button click event hanlder, for instance.
EDIT in response to comment:
You said that you know how to do it on a single form. I do not know how you do it, but probably you have textboxes for the name, the address, etc. that you are looking for. Then probably you press a button called btnSearch. By pressing this button you trigger
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch_Click.Click
' The search logic goes here
End Sub
What I am telling you is to replace btnSearch_Click by Public Sub SetFilter(name as String) that is public. You would move those search fields and the button to the second form and from there call SetFilter of the first form in order to communicate with it and tell it to search a customer. In my example, I pass only a name, but I left it up to you to pass more parameters as required.
Assuming that your customer form is called "DisplayForm", you could access it like this
Dim cust As DisplayForm
cust = DirectCast(Application.OpenForms("DisplayForm"), DisplayForm)
cust.SetFilter(NameTextBox.Text)
My answer was only about the communication between the two forms, assuming that you already managed the other part.
Searching for "VB.NET Tutorial: Working With Data" on YouTube results in six videos on this subject. Video #3 in particular shows how to use filters.
EDIT #2. Here is a little more help:
I do not know how you are opening the two forms. Since you need a reference to the display form in the search form, it is easier to open the search form as the main form at application startup. At Form_Load in the search form, you then open the display form by code
Code in the search form:
' Reference to the display form
Private _displayForm As DisplayForm
' Create the display form and open it
Private Sub SearchForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
_displayForm = New DisplayForm()
_displayForm.Show()
End Sub
' Get the search parameters and tell the display form to set the filter
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
Dim field As String, search As Object
Select Case cboField.SelectedIndex
Case 0
field = "surname"
search = txtSearch.Text
Case 1
Dim n As Integer
field = "id"
If Integer.TryParse(txtSearch.Text, n) Then
search = n
Else
MessageBox.Show("Please enter a number for id search", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Return
End IF
End Select
_displayForm.SetFilter(field, search)
End Sub
Code in the display form that actually performs sorting and searching:
Public Sub SetFilter(ByVal field As String, ByVal search As Object)
Dim intPosition As Integer
ObjDataView.Sort = field
intPosition = ObjDataView.Find(search)
If intPosition = -1 Then
MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Else
objCurrencyManager.Position = intPosition
End If
End Sub