Click here to Skip to main content
15,890,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

My input is the invoice id where user refers the invoice id to search their order details. Now if user have made multiple orders under a single invoice id, there will be rows of data for order which has the same invoice id. Problem with my code is that it only reads the first row of data which has the same invoice id and doesn't read the rest of the rows of data which has the same id.

What I have tried:

Imports System.Data.SqlClient
Public Class payment
    Dim con As New SqlConnection("server = DESKTOP-SNQAJCQ;Database=SDP;Integrated security=SSPI")
    Dim dr As SqlDataReader
    Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click

        Dim i As Integer
        con.Open()

        Dim searchQuery As String = "SELECT * From Order_ WHERE id=@id "

        Dim cmd As New SqlCommand(searchQuery, con)
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = txtinvoice.Text
        dr = cmd.ExecuteReader
        While dr.Read
            i += 1
            dgwPayment.Rows.Add(dr.Item("item_description").ToString,
                                dr.Item("unit_price").ToString,
                                dr.Item("weight_").ToString,
                                dr.Item("quantity").ToString,
                                dr.Item("total").ToString,
                                dr.Item("special_remarks").ToString)
        End While

    End Sub
End Class
Posted
Updated 23-Mar-19 23:40pm
Comments
Maciej Los 24-Mar-19 5:55am    
Are you sure that there are records with the same id?

1 solution

We can't tell: we don't have any access to your data.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But do yourself a favour or two:
1) Don't hardcode connection strings: that means you need to change it in a huge number of places when you release the software. Use a config file to store it. (How I do it is a little more complicated, but it saves time later: Instance Storage - A Simple Way to Share Configuration Data among Applications[^] - the code is in C#, but it's pretty obvious).

2) Don't pass unvalidated data to SQL: check your textbox content before assumign it contains valid numeric data! If you don't, SQL will throw an exception, and your app will crash. Use Integer.TryParse[^] to check and convert, then pass the integer value to SQL and complain to the user if it won't!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900