Fejl i autogenerering?
Jeg skal have lavet en random kode.. Min Codebehind ser sådan her ud:Partial Class Default2
Inherits System.Web.UI.Page
Protected WithEvents TextBox14 As System.Web.UI.WebControls.TextBox
Function GeneratePassword(ByVal length As Integer) As String
'Make sure length and numberOfNonAlphanumericCharacters are valid....
If ((length < 1) OrElse (length > 128)) Then
Throw New ArgumentException("Membership_password_length_incorrect")
End If
Do While True
Dim i As Integer
Dim nonANcount As Integer = 0
Dim buffer1 As Byte() = New Byte(length - 1) {}
'chPassword contains the password's characters as it's built up
Dim chPassword As Char() = New Char(length - 1) {}
'chPunctionations contains the list of legal non-alphanumeric characters
Dim chPunctuations As Char() = "!@@$%^^*()_-+=[{]};:>|./?".ToCharArray()
'Get a cryptographically strong series of bytes
Dim rng As New System.Security.Cryptography.RNGCryptoServiceProvider
rng.GetBytes(buffer1)
For i = 0 To length - 1
'Convert each byte into its representative character
Dim rndChr As Integer = (buffer1(i) Mod 87)
If (rndChr < 10) Then
chPassword(i) = Convert.ToChar(Convert.ToUInt16(48 + rndChr))
Else
If (rndChr < 36) Then
chPassword(i) = Convert.ToChar(Convert.ToUInt16((65 + rndChr) - 10))
Else
If (rndChr < 62) Then
chPassword(i) = Convert.ToChar(Convert.ToUInt16((97 + rndChr) - 36))
Else
chPassword(i) = chPunctuations(rndChr - 62)
nonANcount += 1
End If
End If
End If
Next
If nonANcount < 10 Then
Dim rndNumber As New Random
For i = 0 To (10 - nonANcount) - 1
Dim passwordPos As Integer
Do
passwordPos = rndNumber.Next(0, length)
Loop While Not Char.IsLetterOrDigit(chPassword(passwordPos))
chPassword(passwordPos) = chPunctuations(rndNumber.Next(0, chPunctuations.Length))
Next
End If
Return New String(chPassword)
Loop
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox14.Text = GeneratePassword("10").ToString()
End Sub
End Class
Men Den kommer med en fejl..
"Objektreferencen er ikke indstillet til en forekomst af et objekt"
I denne linie som bliver markeret med gult:
TextBox14.Text = GeneratePassword("10").ToString()
Hvad er det der er galt??