Marcus Eagle
Marcus Eagle

Reputation: 95

VB - Value cannot be null error

The problem I am having is as follows:

I am using an SQL query to take data out of a table like so:

Dim query As String = "SELECT Pronunciation, Character FROM [Hiragana List] WHERE Pronunciation='" & pronunciation & "';"
    Dim instruction = New SqlCommand(query, sqlCon)
    Dim da As New SqlDataAdapter
    da.SelectCommand = instruction
    da.Fill(HiraganaList)

This should then take the data from the table and fill-in a datatable called "Hiragana List", however when the code reaches the line

da.Fill(Hiragana List)

The operation of the program stops and it instead returns this error:

An unhandled exception of type 'System.ArgumentNullException' occurred in System.Data.dll

Additional information: Value cannot be null.

I took a look at the "Hiragana List" table itself and changed it so that it would accept Nulls in each of the field in order to see if it would fix it, unfortunately it still returns the same error.

(edit) Here is the declaration for the datatable 'Hiragana List':

Dim connectionString As String = "Server=my_server;Database=name_of_db;User Id=user_name;Password=my_password"
Dim sqlCon = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='J:\Computing Coursework\real project\KES\KES\Kana List.mdf';Integrated Security=True;Connect Timeout=30")
Dim HiraganaList As DataTable
Dim KanjiList As DataTable
Dim Katakana As DataTable
Dim YoonList As DataTable
Dim YoonKataList As DataTable

Upvotes: 1

Views: 1135

Answers (1)

Al-3sli
Al-3sli

Reputation: 2181

Ok on your code befor da.Fill(HiraganaList) add this Code :

HiraganaList = New DataTable

you Need this to avoid null Exception.

Upvotes: 1

Related Questions