I am trying to populate a combo box with every other element of an array
the array contains data like this
(0) server
(0) sqlInstance
(1) server
(1) sqlInstance
(2) server
(2) sqlInstance
I want all sqlInstance values to be added to the combo box. Not sure how to do it other than maybe another array.
Thanks
Private Sub GetSQLServers()
Dim oSQLApp As New SQLDMO.Application
Dim oServerNames As SQLDMO.NameList
Dim strServer As String
Dim ServerArr() As String
oServerNames = oSQLApp.ListAvailableSQLServers()
For i = 0 To oServerNames.Count
strServer = (oServerNames(i))
ServerArr = strServer.Split("\")
txtSQLHost.Text = ServerArr(0)
For Each s As String In ServerArr
'value of server instance
cbxSQLServerNames.Items.Add(s)
Next
Next i
End Sub
This is working, I'm pretty sure it is not the best way
The combo box fills with sqlInstances only
The txtbox has the server
oServerNames is server\sqlInstance
Private Sub GetSQLServers()
Dim oSQLApp As New SQLDMO.Application
Dim oServerNames As SQLDMO.NameList
Dim strServer As String
Dim ServerArr() As String
Dim sn As Integer = 1
oServerNames = oSQLApp.ListAvailableSQLServers()
For i = 0 To oServerNames.Count
strServer = (oServerNames(i))
ServerArr = strServer.Split("\")
txtSQLHost.Text = ServerArr(0)
For Each s As String In ServerArr
'value of server instance
If ((sn Mod 2) = 0) Then
cbxSQLServerNames.Items.Add(s)
sn = sn + 1
Else
sn = sn + 1
End If
Next
Next i
End Sub
Using this now..
Thanks
Private Sub GetSQLServers()
Dim oSQLApp As New SQLDMO.Application
Dim oServerNames As SQLDMO.NameList
Dim strServer As String
Dim ServerArr() As String
oServerNames = oSQLApp.ListAvailableSQLServers()
For i = 0 To oServerNames.Count
strServer = (oServerNames(i))
ServerArr = strServer.Split("\")
txtSQLHost.Text = ServerArr(0)
For Each item In oServerNames
Dim parts = item.Split("\")
Dim instance = parts(1)
cbxSQLServerNames.Items.Add(instance)
Next
Next i
End Sub
Try this:
For i = 0 To oServerNames.Count - 1 Step 2
Dim serverInstance = oServerNames(i) & "\" & oServerNames(i + 1)
cbxSQLServerNames.Items.Add(serverInstance)
Next i
The Count - 1 because, if you are count 10 items, you are counting from 0 to 9.
After looking at the source of your question, you say the array looks likes this:
(0) server
(0) sqlInstance
(1) server
(1) sqlInstance
(2) server
(2) sqlInstance
This is not possible. There can not be multiple items with the same index. Something is missing here...
If the array look like this:
(0) server\sqlInstance
(1) server\sqlInstance
(2) server\sqlInstance
You can do that:
For Each item In oServerNames
Dim parts = item.Split("\")
dim instance = parts(1)
cbxSQLServerNames.Items.Add(instance)
Next
Related
I have an array like below
(0) = "apple"
(1) = "orange"
How can I add some string to all item in array? like apple become 'apple', orange become 'orange'
Edited
Private Sub test()
Dim txtReader As TextReader = New StreamReader("data.csv")
Dim parser = New CsvParser(txtReader)
Dim str As String = ""
'Ignore first line
parser.Read()
While True
Dim row = parser.Read()
If row Is Nothing Then
Exit While
End If
str &= $"({String.Join(",", row)}),"
End While
str_record = str.TrimEnd(",")
End Sub
Private Sub Model_Insert()
Dim data As String = ""
Dim query As String = "INSERT INTO main_item(item_code,item_name,item_desc,item_unitprice,item_barcode,dept_id,cat_id,gst_id,set_item,active)" &
"VALUES " & str_record & ""
Using cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
End Sub
Im trying to create a string and use it in INSERT INTO
Use a For-loop:
Dim array = {"apple", "orange"}
For i As Int32 = 0 To array.Length - 1
array(i) = $"'{array(i)}'"
Next
If you can't use string interpolation yet, use String.Format (or string concatenation):
For i As Int32 = 0 To array.Length - 1
array(i) = String.Format("'{0}'", array(i))
Next
If you don't mind recreating the array, you could use Linq Select:
Dim testarray As String() = New String() {"orange", "apple"}
testarray = testarray.Select(Function(x) String.Format("'{0}'", x)).ToArray()
I have the following code which loop through all the cells of the selected row. How to store all the values in an array?
Dim selectedCellCount As Integer = dgvData.GetCellCount(DataGridViewElementStates.Selected)
Dim RowVal As String
Dim i As Integer
For i = 0 To selectedCellCount - 1
RowVal = dgvData.SelectedCells(i).Value.ToString
Next i
End Sub
There is two method one with list another with array
1- List
Dim selectedCellCount As Integer = dgvData.GetCellCount(DataGridViewElementStates.Selected)
Dim RowVal As String
Dim i As Integer
Dim list As New List(Of string)
For i = 0 To selectedCellCount - 1
RowVal = dgvData.SelectedCells(i).Value.ToString
list.Add(RowVal)
Next i
End Sub
2- Array
Dim selectedCellCount As Integer = dgvData.GetCellCount(DataGridViewElementStates.Selected)
Dim RowVal As String
Dim i As Integer
Dim arrayOfData(selectedCellCount - 1) As String
For i = 0 To selectedCellCount - 1
RowVal = dgvData.SelectedCells(i).Value.ToString
arrayOfData(i) = RowVal
Next i
End Sub
Check out a List(of string). The list is easier to use than any other type of array. Here is a good explanation:
https://www.dotnetperls.com/list-vbnet
I want to run the fetching of a Database into a background worker but it seems to be crashing without Visual Studio returning me an error in my code.
Here's the code in the DoWork:
Private Sub ITSM_Fetch_BW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ITSM_Fetch_BW.DoWork
Dim sql As String = "xxxx"
Dim ConnString As String = "DRIVER={AR System ODBC Driver};ARServer=xxxx;ARServerPort=xxxx;ARPrivateRpcSocket=xxxx;UID=xxxx;PWD=xxxx;ARAuthentication=;ARUseUnderscores=1;SERVER=NotTheServer"
Dim connection As New Odbc.OdbcConnection(ConnString)
connection.Open()
Dim ODBC_Command As New Odbc.OdbcCommand(sql, connection)
Dim ODBC_reader As Odbc.OdbcDataReader
'Load the Data into the local Memory
ODBC_reader = ODBC_Command.ExecuteReader
e.Result = ODBC_reader
ODBC_reader.Close()
End Sub
Private Sub ITSM_Fetch_BW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles ITSM_Fetch_BW.RunWorkerCompleted
Data = New DataTable
Data.Load(e.Result)
Dim Count_ToDo(5) As String
Count_ToDo(0) = "Product_Name"
Count_ToDo(1) = "Status"
Count_ToDo(2) = "Language"
Count_ToDo(3) = "Assigned_Group"
Count_ToDo(4) = "Priority"
Count_ToDo(5) = "Company"
For Each Item As String In Count_ToDo
Dim i As Integer = 0
Dim ITEM_Count(0, 1) As String
For Each Ticket As DataRow In Data.Rows
'PART FOR THE CI
If IsDBNull(Ticket.Item(Item)) = False Then
Dim IsInIndex As Integer = -1
If i = 0 Then
ITEM_Count(0, 0) = Ticket.Item(Item)
ITEM_Count(0, 1) = 1
Else
For x As Integer = 0 To ITEM_Count.GetLength(0) - 1
If ITEM_Count(x, 0) = Ticket.Item(Item) Then
IsInIndex = x
End If
Next
If IsInIndex = -1 Then
Dim ITEM_Count_Temp(ITEM_Count.GetLength(0), ITEM_Count.GetLength(0)) As String
ITEM_Count_Temp = ITEM_Count
ReDim ITEM_Count(ITEM_Count.GetLength(0), 1)
For x As Integer = 0 To ITEM_Count_Temp.GetLength(0) - 1
For y As Integer = 0 To ITEM_Count_Temp.GetLength(1) - 1
ITEM_Count(x, y) = ITEM_Count_Temp(x, y)
Next
Next
ITEM_Count(ITEM_Count.GetLength(0) - 1, 0) = Ticket.Item(Item)
ITEM_Count(ITEM_Count.GetLength(0) - 1, 1) = 1
Else
ITEM_Count(IsInIndex, 1) = ITEM_Count(IsInIndex, 1) + 1
End If
End If
Else
'IF NULL
End If
i = i + 1
Next
'CI_COUNT FILLING
'ORDERING BY COUNT
Dim ITEM_obj = New List(Of obj)
Dim ITEM_ToObj As String = ""
Dim ITEMCount_ToObj As String = ""
For x As Integer = 0 To ITEM_Count.GetLength(0) - 1
ITEM_ToObj = ITEM_Count(x, 0)
ITEMCount_ToObj = ITEM_Count(x, 1)
ITEM_obj.Add(New obj(ITEM_ToObj, ITEMCount_ToObj))
Next
ITEM_obj = OrderItem(ITEM_obj)
Dim Item_Count_listview As ListViewItem
For Each Itemobj As obj In ITEM_obj
Dim Transfer_Array(2) As String
Transfer_Array(0) = Itemobj.Item
Transfer_Array(1) = Itemobj.Item_Count
Item_Count_listview = New ListViewItem(Transfer_Array)
Select Case Item
Case "Product_Name"
CI_Count_Table.Items.Add(Item_Count_listview)
Case "Status"
Status_Count_Table.Items.Add(Item_Count_listview)
Case "Language"
Language_Count_Table.Items.Add(Item_Count_listview)
Case "Assigned_Group"
AssignedGroup_Count_Table.Items.Add(Item_Count_listview)
Case "Priority"
Priority_Count_Table.Items.Add(Item_Count_listview)
Case "Company"
LOB_Count_Table.Items.Add(Item_Count_listview)
Case Else
MsgBox("No Category Of this type exist. Programming Issue. Item is: " & Item)
End Select
Next
Next
End Sub
What is not possible to run into a background worker like this?
regards,
So i'm working on a project and i've hit a bit of a roadblock. i'm trying to compare two arrays that've been populated with read-in and split text files, find the matches of individual elements (names), then populate a listbox with the resulting matches. here's the code i have so far:
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim guest As String()
Dim gift As String()
Private Sub frmTYC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strFile1 As String
Dim strFile2 As String
Dim intCount As Integer
strFile1 = "Guests.txt"
strFile2 = "Gifts.txt"
intCount = 0
sr = IO.File.OpenText(strFile1)
Do While sr.Peek <> -1
guest = sr.ReadLine.Split(",")
lstGuests.Items.Add(guest(0))
intCount += 1
Loop
sr.Close()
intCount = 0
sr = IO.File.OpenText(strFile2)
Do While sr.Peek <> -1
gift = sr.ReadLine.Split(",")
lstGifts.Items.Add(gift(1) & " " & "gifted" & " " & gift(0))
intCount += 1
Loop
sr.Close()
End Sub
Private Sub btnList_Click(sender As Object, e As EventArgs) Handles btnList.Click
End Sub
i'm trying to compare guest(0) and gift(1) as those are the names, with guest having all the names and gift having all the names of the people in guest who gave a gift. i need the listbox to be populated on the list button click
In winForms adding a CSV to a DataGrid was quite easy. I am now trying to add this to a Silverlight DataGrid. Here is my attempt - which yields 3 columns Capacity|Count|Items - mind you the values are correct 83|83|_ on each row. There are 83 rows, but the columns should be 23 with diff values in each. Thanks for looking and enjoy your bounty!
Code:
Try
Dim ofd As New OpenFileDialog
If ofd.ShowDialog Then
If IO.File.Exists(ofd.File.FullName) Then
Dim srsCol As New List(Of List(Of String))
Using fs As IO.FileStream = ofd.File.OpenRead
Using sr As New IO.StreamReader(fs)
While Not sr.Peek = -1
srsCol.Add(New List(Of String)(sr.ReadLine.Split(","c).ToList))
End While
End Using
End Using
dgStaff.ItemsSource = srsCol
End If
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
I decided to use the BindableDataGrid from CodePlex Since the binding is being set dynamically I had to come up with a Random string generator and assign that for the binding and all is well.
csvDs.Tables.Clear()
Try
Dim ofd As New OpenFileDialog
If ofd.ShowDialog Then
If IO.File.Exists(ofd.File.FullName) Then
csvDs.Tables.Add(csvDt)
Using fs As IO.FileStream = ofd.File.OpenRead
Using sr As New IO.StreamReader(fs)
Dim i As Integer
While Not sr.EndOfStream
If i = 0 Then
Dim cols = sr.ReadLine.Split(","c)
For ii As Integer = 0 To cols.Count - 1
Dim rndValue As String = RndColName()
Dim col As New BindableDataGrid.Data.DataColumn(rndValue)
rndValues.Add(rndValue)
col.DataType = GetType(System.String)
col.Caption = ii.ToString
col.ReadOnly = True
col.AllowReorder = False
col.AllowResize = False
col.AllowSort = False
csvDt.Columns.Add(col)
AddItemsToCb(ii)
Next
Dim row As New BindableDataGrid.Data.DataRow
For _i As Integer = 0 To cols.Count - 1
Dim s As String = cols(_i).Replace("""", String.Empty)
row(rndValues(_i)) = s
csvValues.Add(s)
Next
csvDt.Rows.Add(row)
Else
Dim cols = sr.ReadLine.Split(","c)
Dim row As New BindableDataGrid.Data.DataRow
For _i As Integer = 0 To cols.Count - 1
row(rndValues(_i)) = cols(_i).Replace("""", String.Empty)
Next
csvDt.Rows.Add(row)
End If
i += 1
End While
End Using
End Using
dgStaff.DataSource = csvDs
dgStaff.DataMember = "csvTable"
dgStaff.DataBind()