Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Using the SQLDataReader with empty parameters

Posted on 2007-07-31
2
Medium Priority
?
246 Views
Last Modified: 2010-04-23
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
            Return
        End If

        Dim part1 As String = STR_parts(0)
        Dim part2 As String = STR_parts(1)
        Dim reader As SqlDataReader

        SqlConnection1.Open()
        SqlCommand1.Parameters("@Cont#").Value = "" & STR_parts(0) & ""
        SqlCommand1.Parameters("@Str#").Value = "" & STR_parts(1) & ""
        SqlCommand1.ExecuteNonQuery()

        reader = SqlCommand1.ExecuteReader
        reader.Read()
        TXT_name.Text = reader("NAME")

        SqlConnection1.Close()

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)

Cheers.
0
Comment
Question by:FMabey
2 Comments
 
LVL 3

Author Comment

by:FMabey
ID: 19598883
Just to say. I've solved this issue buy inserting the TXT_name.Text = reader("NAME") line into an if statement as shown below. Points to me I guess ;o)

        If reader.HasRows Then

            TXT_name.Text = reader("NAME")

        End If

Cheers
0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 19599228
Hello FMabey,

An alternative is to do something like this....

        reader = SqlCommand1.ExecuteReader
        If reader.Read Then
             TXT_name.Text = reader("NAME")
         End If

This is because Read returns a boolean if there is a "next" record to read.

Regards,

Wayne
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question