Using the SQLDataReader with empty parameters
Posted on 2007-07-31
I am using the following code to return a value from a SQL Server table depending upon what a user enters into a textbox. I want to put this code in the textboxes TextChanged sub. This will mean that as soon as a match is found the result will appear on screen in another textbox.
Dim STR_cont As String
STR_cont = TXT_Cont.Text
Dim STR_parts As String() = STR_cont.Split("-")
If STR_parts.Length = 1 Then
' - not found in the string decide what to do
Dim part1 As String = STR_parts(0)
Dim part2 As String = STR_parts(1)
Dim reader As SqlDataReader
SqlCommand1.Parameters("@Cont#").Value = "" & STR_parts(0) & ""
SqlCommand1.Parameters("@Str#").Value = "" & STR_parts(1) & ""
reader = SqlCommand1.ExecuteReader
TXT_name.Text = reader("NAME")
My problem is that the code fails on line 'TXT_name.Text = reader("NAME")' as soon as a '-' is entered by the user. This is because the value of STR_parts(1) is empty.
How can I get around this?
I am using VB.NET, (visual studio 2003)