title: Insert data into database by linking class to btnRegister
body: <p>I am new for programming and trying to learn VB.net through youtube tutorials. </p>
  
  <p>I created a windows form app for users to create new account.</p>
  
  <p>I am also trying to apply the hash and salt technique to store the password for security reason. </p>
  
  <p>The main issue I have is maintaining successful connection between a two class I called "DatabaseManager" and "DataHandling", codes in "btnRegister" and the database called "Test". </p>
  
  <p>The program runs fine. When I click btnRegister after filling the "txtUsername.Text" and "txtPassword.Text", it gives me error that says <code>"Column</code>User_ID<code>does not allow nulls."</code>.</p>
  
  <p>So this is where I keep getting issues. I tried to make it work a lot and I have no idea why it is not recording the new data. Here are my codes. I use V Studio 2012. Please help.
      Just to be clear I copied some of the codes from internet and tried to make them work with the class</p>
  
  <pre><code>Imports System
  Imports System.IO
  Imports System.Text
  Imports System.Data.OleDb
  Imports System.Data
  Imports System.Windows.Forms
  Imports System.Data.SqlClient
  Imports System.Security.Cryptography
  
  Public Class DatabaseManager
  
      Private Const CONNECTION_STRING As String = "Data Source=(localdb)\Projects;Initial Catalog=Test;Integrated Security=True"
  
      Private connection As SqlConnection = Nothing
      Private usersdataadapter As SqlDataAdapter = Nothing
  
      Sub New()
          connection = New SqlConnection(CONNECTION_STRING)
          usersdataadapter = New SqlDataAdapter("select * from Test", connection)
      End Sub
  
      Public Sub Register(ByVal Username As String, ByVal Password As String)
  
          connection.Open()
  
          Dim usersDataset As New DataSet()
          usersdataadapter.FillSchema(usersDataset, SchemaType.Source, "Test")
          Dim table As DataTable = usersDataset.Tables("Test")
  
          Dim newRecord As DataRow = table.NewRow()
          newRecord("Username") = Username
          newRecord("Password") = Password
          table.Rows.Add(newRecord)
  
          Dim command As New SqlCommandBuilder(usersdataadapter)
          usersdataadapter.Update(usersDataset, "Test")
          usersDataset.Dispose()
  
          connection.Close()
      End Sub
          Public Function UsernameAvailable(ByVal username As String) As Boolean
  
          Dim usersDataset As New DataSet()
          usersdataadapter.FillSchema(usersDataset, SchemaType.Source, "Test")
          usersdataadapter.Fill(usersDataset, "Test")
  
          Dim table As DataTable = usersDataset.Tables("Test")
          For i As Integer = 0 To table.Rows.Count - 1
              Dim currnetUser As String = table.Rows(i)("Username").ToString().Trim()
              If (currnetUser = username) Then
                  usersDataset.Dispose()
                  connection.Close()
                  Return False
              End If
          Next
          Return True
          usersDataset.Dispose()
          connection.Close()
      End Function
      Public Function Login(ByVal username As String, ByVal password As String) As Boolean
  
          Dim usersDataset As New DataSet()
          usersdataadapter.FillSchema(usersDataset, SchemaType.Source, "Test")
          usersdataadapter.Fill(usersDataset, "Test")
  
          Dim table As DataTable = usersDataset.Tables("Test")
          For i As Integer = 0 To table.Rows.Count - 1
              Dim currnetUser As String = table.Rows(i)("Username").ToString().Trim()
              Dim currnetPassword As String = table.Rows(i)("Password").ToString().Trim()
  
              If (currnetUser = username AndAlso currnetPassword = password) Then
                  usersDataset.Dispose()
                  connection.Close()
                  Return True
              End If
          Next
          usersDataset.Dispose()
          connection.Close()
          Return False
      End Function
  End Class
  
  Imports System
  Imports System.Text
  Imports System.Data.OleDb
  Imports System.Data
  Imports System.Data.SqlClient
  Imports System.Windows.Forms
  Imports System.Security.Cryptography
  
  Public Class DataHandling
  
      Inherits Form1
      Public Shared Function GenerateRandomString() As String
          Dim i_key As Integer
          Dim Random1 As Single
          Dim arrIndex As Int16
          Dim sb As New StringBuilder
          Dim RandomLetter As String
  
          Dim KeyLetters As String = "abcdefghijklmnopqrstuvwxyz"
          Dim KeyNumbers As String = "0123456789"
          Dim KeyLength As Integer = 12
  
          Dim LettersArray = KeyLetters.ToCharArray
          Dim NumbersArray = KeyNumbers.ToCharArray
  
          For i_key = 1 To KeyLength
              Randomize()
              Random1 = Rnd()
              arrIndex = -1
  
              If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then
  
                  Do While arrIndex &lt; 0
                      arrIndex = _
                       Convert.ToInt16(LettersArray.GetUpperBound(0) _
                       * Random1)
                  Loop
                  RandomLetter = LettersArray(arrIndex)
  
                  If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 &lt;&gt; 0 Then
                      RandomLetter = LettersArray(arrIndex).ToString
                      RandomLetter = RandomLetter.ToUpper
                  End If
                  sb.Append(RandomLetter)
              Else
                  Do While arrIndex &lt; 0
                      arrIndex = _
                        Convert.ToInt16(NumbersArray.GetUpperBound(0) _
                        * Random1)
                  Loop
                  sb.Append(NumbersArray(arrIndex))
              End If
          Next
  
          Return sb.ToString
      End Function
  
      Public Shared Function CheckPassword(ByVal plainText As String, ByVal passwordHash As Byte(), ByVal salt As Byte()) As Boolean
          Dim encoding As New System.Text.UTF8Encoding
  
          'Retrieve Salt String from byte array
          Dim saltStr As String = encoding.GetString(salt)
          Dim hashable As String = Trim(plainText) &amp; Trim(saltStr)
  
          ' Convert into hash strings
          Dim testhash As String = EncryptStringAsHash(hashable)
          Dim realHash As String = encoding.GetString(passwordHash)
  
          ' Compare and return result
          Return testhash = realHash
      End Function
  
      Public Shared Function EncryptStringAsHash(ByVal value As String) As String
          Dim encoding As New System.Text.UTF8Encoding
          Dim stringBytes As Byte() = encoding.GetBytes(value)
  
          Dim SHhash As SHA512Managed = New SHA512Managed
          Dim hash As String = Convert.ToBase64String(SHhash.ComputeHash(stringBytes))
  
          Return hash
      End Function
  
      Public Shared Function ConvertStringToByteArray(ByVal value As String) As Byte()
          Dim encoding As New System.Text.UTF8Encoding
          Return encoding.GetBytes(value)
      End Function
  End Class
  
  Imports System.IO
  Imports System.Text
  Imports System.Security.Cryptography
  Imports System.Data.OleDb
  Imports System.Windows.Forms
  Imports System.Data.OleDb.OleDbConnection
  Imports System.Data.SqlClient
  
  Public Class Form1
  
      Dim maxrows As Integer
      Dim incdec As Integer
      Dim con As New OleDb.OleDbConnection
      Dim dbprovider As String
      Dim dbsource As String
      Dim ds As New DataSet
      Dim da As OleDb.OleDbDataAdapter
      Dim sql As String
  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          Dim con As New OleDbConnection
          Dim dbprovider As String
          Dim dbsource As String
          dbprovider = "PROVIDER = Microsoft.ACE.OLEDB.12.0;"
          dbsource = "Data Source=(localdb)\Projects;Initial Catalog=Test;Integrated Security=True"
          con.ConnectionString = dbprovider &amp; dbsource
  
      End Sub
  
      Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
  
          Dim dbmanager As New DatabaseManager
          Dim Data As New DataHandling
  
          'Generate Hash and Salt for password
          Dim salt As String = DataHandling.GenerateRandomString
          Dim hashable As String = Trim(txtPassword.Text) &amp; Trim(salt) 'txtPassword.Text used to be password
          Dim hash As String = DataHandling.EncryptStringAsHash(hashable)
  
      Dim confirmationId As String = System.Guid.NewGuid.ToString
          Dim reg As user
  
          reg.Username = txtUsername.Text     'txtUsername.Text used to be username
          reg.Password = DataHandling.ConvertStringToByteArray(hash)
          reg.Salt = DataHandling.ConvertStringToByteArray(salt)
  
          If dbmanager.UsernameAvailable(txtUsername.Text) Then
  
              dbmanager.Register(txtUsername.Text, txtPassword.Text)
  
      Dim password As String
              If txtPassword.Text = String.Empty Then
                  password = "217tABCDEF42#$tolq"
              Else
                  password = txtPassword.Text
              End If
  
              Dim salt As String = GenerateRandomString(12)
              Dim hashable As String = Trim(password) &amp; Trim(salt)
              MsgBox("Hashable = " &amp; hashable)
              Dim hash As String = EncryptStringAsHash(hashable)
              CheckPassword(password, ConvertStringToByteArray(hash), ConvertStringToByteArray(salt))
  
              frmAccessGranted.Show()
              Me.Hide()
          Else
              MessageBox.Show("Cannot Register, Username Already Taken!")
          End If
      End Sub
  
      Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
          txtUsername.Clear()
          txtPassword.Clear()
      End Sub
  
  End Class
  </code></pre>