Results 1 to 8 of 8

Thread: [RESOLVED] How to get set of values in one msgbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [RESOLVED] How to get set of values in one msgbox

    This piece of code is returning the multiple values of Category in MsgBox one by one. How can i make it to show all the values of category in one MsgBox separated by commas and store it in a String?

    Code:
    Try
                Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
                    connection.Open()
                    Using command As New SqlCommand("SELECT * FROM MachineProfile where ClientID = '" & MDIParentMW.clid & "'", connection)
                        Using reader As SqlDataReader = command.ExecuteReader()
                            If reader.HasRows Then
                                While reader.Read()
                                    MsgBox(reader("Category"))
                                End While
                            Else
    
                            End If
                        End Using
                    End Using
                    connection.Close()
                End Using
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
            End Try

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to get set of values in one msgbox

    If I have understood your query correctly then Declare a new String Variable and keep on appending the values of Category in the loop separated by appropriate delimiter to it and then when you exit the loop simply display the string variable in the message box.

    Edit:

    Here is a very basic example...

    Code:
    Public Class Form1
        Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
            Dim StrSample As String, i As Long
    
            StrSample = ""
    
            For i = 1 To 5
                If i = 1 Then
                    StrSample = i
                Else
                    StrSample = StrSample & " , " & i
                End If
            Next i
            '~~> Display String
            MsgBox(StrSample)
        End Sub
    End Class
    Last edited by Siddharth Rout; Nov 18th, 2009 at 12:21 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: How to get set of values in one msgbox

    I have modified you suggestion and tried using it as below. But i am not getting the desired result. What could possibly be wrong in here?

    vb Code:
    1. Try
    2.             Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
    3.                 connection.Open()
    4.                 Using command As New SqlCommand("SELECT Count(MachineID) as NoMach FROM MachineProfile where ClientID = '" & MDIParentMW.clid & "'", connection)
    5.                     Using reader As SqlDataReader = command.ExecuteReader()
    6.                         While reader.Read()
    7.                             i = reader.Item("NoMach")
    8.                         End While
    9.                     End Using
    10.                 End Using
    11.                 connection.Close()
    12.             End Using
    13.             Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
    14.                 connection.Open()
    15.                 Using command As New SqlCommand("SELECT * FROM MachineProfile where ClientID = '" & MDIParentMW.clid & "'", connection)
    16.                     Using reader As SqlDataReader = command.ExecuteReader()
    17.                         For j = 1 To i
    18.                             reader.Read()
    19.                             If j = 1 Then
    20.                                 mach = reader("Category")
    21.                             Else
    22.                                 mach = mach & "," & j
    23.                             End If
    24.                         Next j
    25.                         MsgBox(mach)
    26.                     End Using
    27.                 End Using
    28.                 connection.Close()
    29.             End Using
    30.         Catch ex As Exception
    31.             MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
    32.         End Try

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: How to get set of values in one msgbox

    Is there a simpler way to get an array assigned to a string?

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to get set of values in one msgbox

    No you are not looping. and adding the correct value.

    you are adding j which is similar to what I have done above but it was specific for that example...

    mach = mach & "," & reader("Category")
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: How to get set of values in one msgbox

    Thanks

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [RESOLVED] How to get set of values in one msgbox

    Try this:

    vb.net Code:
    1. Dim categoryList As New List(Of String)
    2.  
    3.  
    4. Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
    5.     connection.Open()
    6.     Using command As New SqlCommand("SELECT * FROM MachineProfile where ClientID = '" & MDIParentMW.clid & "'", connection)
    7.         Using reader As SqlDataReader = command.ExecuteReader()
    8.             While reader.Read
    9.                 categoryList.Add(reader("Category"))
    10.             End While
    11.             MsgBox(Join(categoryList.ToArray, ","))
    12.         End Using
    13.     End Using
    14.     connection.Close()
    15. End Using
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [RESOLVED] How to get set of values in one msgbox

    Thank you pradeep.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width