0

I am working on a small VB.net application that will send SMS messages about overdue accounts. This application has a Data Grid View that pulls data from a local SQL server.

What I am trying to enable is the ability to select multiple rows on the DGV, and when I hit a button on the form, have it perform the same action on each row - in this case it should get the Request ID, the Outstanding amount and the Phone Number, and send the message based on that information.

If I select 2 rows for example, and hit the button, it sends 2 messages, but they both have the same Request ID and outstanding amount, and for the life of me I cannot understand why.

I have tried to use a checkbox on the rows but I get the same result. If you refer to my screen shot, the data for both messages comes from the row with the right facing arrow on the far left column.

Any help would be appreciated!!

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    For Each dr As DataGridViewRow In dgResults.SelectedRows
        Dim ref As String = dgResults.SelectedRows(0).Cells(0).Value.ToString
        Dim amt As String = dgResults.SelectedRows(0).Cells(5).Value.ToString
        Dim amtaud As Decimal = amt
        Dim charge As String = amtaud.ToString("C")
        Dim number As String = dgResults.SelectedRows(0).Cells(3).Value.ToString

        Dim baseurl As String
        Dim webclient1 As New WebClient()

        baseurl = "http://api.smsbroadcast.com.au/api-adv.php"

        webclient1.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
        webclient1.QueryString.Add("username", "user")
        webclient1.QueryString.Add("password", "pass")
        webclient1.QueryString.Add("to", number)
        webclient1.QueryString.Add("from", "sender")
        If rbDeclined.Checked = True Then webclient1.QueryString.Add("message", "Your account is now due. NutriPATH has received your sample however your credit card details provided has resulted in a declined or invalid card. Please call NutriPATH on 1300688522 to pay for your test to avoid delays in results or alternatively you may pay via our website https://www.bpoint.com.au/payments/nutripath Reference ID " + ref + " Amount Due " + charge)
        If rbOverdue.Checked = True Then webclient1.QueryString.Add("message", "This is a courtesy reminder that your account is now overdue. Please call NutriPATH on 1300688522 to pay for your test to avoid delays in results or alternatively you may pay via our website https://www.bpoint.com.au/payments/nutripathReference ID " + ref + " Amount Due " + charge)
        If rbReceived.Checked = True Then webclient1.QueryString.Add("message", "Your sample has been received and is ready for testing. Please call NutriPATH on 1300688522 to pay for your test to avoid delays or alternatively you may pay via our online payment facility: https://www.bpoint.com.au/payments/nutripathYour Reference ID is " + ref + " Amount Outstanding is " + charge)
        webclient1.QueryString.Add("maxsplit", "3")

        Dim myStream As Stream = webclient1.OpenRead(baseurl)
        Dim sr As New StreamReader(myStream)
        MsgBox(sr.ReadToEnd(), MsgBoxStyle.OkOnly)
             myStream.Close()

    Next
End Sub

Screenshot

1

1 Answer 1

1

The issue is here:

For Each dr As DataGridViewRow In dgResults.SelectedRows
    Dim ref As String = dgResults.SelectedRows(0).Cells(0).Value.ToString
    Dim amt As String = dgResults.SelectedRows(0).Cells(5).Value.ToString
    Dim amtaud As Decimal = amt
    Dim charge As String = amtaud.ToString("C")
    Dim number As String = dgResults.SelectedRows(0).Cells(3).Value.ToString

The loop control variable is dr and that contains the current row but, instead of using that, you use dgResults.SelectedRows(0) so of course you get the same data every time: you're using the same row every time. Change that to this:

For Each dr As DataGridViewRow In dgResults.SelectedRows
    Dim ref As String = dr.Cells(0).Value.ToString
    Dim amt As String = dr.Cells(5).Value.ToString
    Dim amtaud As Decimal = amt
    Dim charge As String = amtaud.ToString("C")
    Dim number As String = dr.Cells(3).Value.ToString

That's For Each loops 101. The point of a For Each is to use the loop control variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks heaps! I knew the answer was right under my nose but just couldn't work it out (still learning the ropes).
@BrettFK if this answer helped you please mark it as an answer (check sign). This is important not only to thank the person who provided the answer but also to show that the question is not still open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.