[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
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
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:
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 Count(MachineID) as NoMach FROM MachineProfile where ClientID = '" & MDIParentMW.clid & "'", connection)
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
i = reader.Item("NoMach")
End While
End Using
End Using
connection.Close()
End Using
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()
For j = 1 To i
reader.Read()
If j = 1 Then
mach = reader("Category")
Else
mach = mach & "," & j
End If
Next j
MsgBox(mach)
End Using
End Using
connection.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
End Try
Re: How to get set of values in one msgbox
Is there a simpler way to get an array assigned to a string?
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")
Re: How to get set of values in one msgbox
Re: [RESOLVED] How to get set of values in one msgbox
Try this:
vb.net Code:
Dim categoryList As New List(Of String)
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()
While reader.Read
categoryList.Add(reader("Category"))
End While
MsgBox(Join(categoryList.ToArray, ","))
End Using
End Using
connection.Close()
End Using
Re: [RESOLVED] How to get set of values in one msgbox