I am trying to run a series of checks on TextBox1 and display error messages when conditions are not met. Ultimately when all conditions are met before I then do a table insert and open a file dialog.
The first statement is to check if there is a value
The second checks that no numbers have been used
The third checks an SQL dB to make sure the value is not already in use - It is here I am having the issue.
If the value exists in the dB it flags the correct error:
MessageBox.Show(TextBox1.Text & " exists already.")
My issue is that when that condition is not met, I get a system error:
"Object reference not set to an instance of an object."
For this line of code.
Dim tarrif As String = cmd.ExecuteScalar().ToString()
I am not sure why because the rest of the code is below the ELSE statement?
Below is a longer section of my code, to give context.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Checks something is in Textbox1
If TextBox1.Text.Trim.Length = 0 Then
MessageBox.Show("Please specify new tarrif name!", "Indigo Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
'Check textbox on only has characters
If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, "^[0-9]*$") Then
'Error is box contains numbers
MessageBox.Show("Please enter Characters only!", "Indigo Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
'Check name is not already in use
Using conn As New SqlClient.SqlConnection("server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes")
Using cmd As SqlClient.SqlCommand = conn.CreateCommand()
cmd.CommandText = "SELECT 1 FROM Tarrifs WHERE Tarrif = @Tarrif"
cmd.Parameters.AddWithValue("@Tarrif", TextBox1.Text)
conn.Open()
Dim tarrif As String = cmd.ExecuteScalar().ToString()
If tarrif = "1" Then
MessageBox.Show(TextBox1.Text & " exists already.")
Else
'Create new table based on specified Tarrif name
Using con = New SqlConnection("server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes")
Using cmda = New SqlCommand("CREATE TABLE " & TextBox1.Text & " (CallType VarChar(30),ChargeCode VarChar(30),Destination VarChar(30),TariffUsed VarChar(30),Peak Float,OffPeak Float,Weekend Float,Setup Float,MinimumCharge Float,ChargeCap INT,InitialUnits INT,InitialCharge INT,InitialPeak INT,InitialOffPeak INT,InitialWeekend INT,BillingUnit INT,MinimumUnits INT,RateType VarChar(30));", con)
con.Open()
cmda.ExecuteNonQuery()
con.Close()
End Using
'import name into Tarrif table
Using cmdb = New SqlCommand("INSERT INTO Tarrifs (Tarrif) VALUES (@tarrif2)", con)
con.Open()
cmdb.Parameters.AddWithValue("@tarrif2", TextBox1.Text)
cmdb.ExecuteNonQuery()
con.Close()
End Using
End Using
'--First create a datatable with the same cols as CSV file, the cols order in both should be same
Dim table As New DataTable()
table.Columns.Add("CallType", GetType(String))
table.Columns.Add("ChargeCode", GetType(String))
table.Columns.Add("Destination", GetType(String))
table.Columns.Add("TariffUsed", GetType(String))
table.Columns.Add("Peak", GetType(Decimal))
table.Columns.Add("OffPeak", GetType(Decimal))
table.Columns.Add("Weekend", GetType(Decimal))
table.Columns.Add("Setup", GetType(Decimal))
table.Columns.Add("MinimumCharge", GetType(Decimal))
table.Columns.Add("ChargeCap", GetType(Integer))
table.Columns.Add("InitialUnits", GetType(Integer))
table.Columns.Add("InitialCharge", GetType(Integer))
table.Columns.Add("InitialPeak", GetType(Integer))
table.Columns.Add("InitialOffPeak", GetType(Integer))
table.Columns.Add("InitialWeekend", GetType(Integer))
table.Columns.Add("BillingUnit", GetType(Integer))
table.Columns.Add("MinimumUnits", GetType(Integer))
table.Columns.Add("RateType", GetType(String))
'open file dialog and store filename'
Dim openFileDialog1 As New OpenFileDialog
Dim strFileName As String
If anyone can help with this issue, I would be very grateful
cmd.ExecuteScalar().ToString(). EithercmdorExecuteScalaris null, so its failing on the subsequent call.