0

Hey, As you can see i have a function to retrieve the product description based on product code. For some reason this is refusing to return a value even though the value im entering into test is there in the table, i can see the value of the input enters fine, but by the end the string is empty.

Plsssss help looked at the same code for 3 days now lol

Private Function GetProductFromProductTable(ByVal product As String) As String
        Dim productDescription As String = String.Empty

        Using Myconnection As New SqlConnection()

            Myconnection.ConnectionString = ConfigurationManager.ConnectionStrings("EDIconnectionString").ConnectionString

            Dim mycommand As New SqlCommand()
            mycommand.Connection = Myconnection
            mycommand.CommandText = "SELECT Long_description FROM Stock WHERE Product = @Product"
            mycommand.Parameters.AddWithValue("@Product", product)

            Myconnection.Open()

            Dim dr2 As SqlDataReader = Nothing
            dr2 = mycommand.ExecuteReader()
            dr2.Read()
            While dr2.Read()
                productDescription = Convert.ToString(dr2("Long_description"))
            End While

            dr2.Close()

            Myconnection.Close()


            Return productDescription
        End Using
    End Function

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Freon22
0

Apologies if this is well off base, but what datatype is Product in your table? If its a char field of some description, shouldn't it's value be wrapped in single quotes in your SQL?

1

It looks like you are calling Read() two times.

Dim dr2 As SqlDataReader = Nothing
dr2 = mycommand.ExecuteReader()
dr2.Read()
While dr2.Read()
productDescription = Convert.ToString(dr2("Long_description"))
End While

Edit: When I call data I always check to see if my reader has rows before I use it. Here is how I do it in C#

if (myReader.HasRows)
            {
                while (myReader.Read())
                {
                    ......
                }
            }

Edited by Freon22: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.