Is there an easier way to do this? - vb.net

I am looping through a listbox, and I need to get the same value you would get if you did .selectvalue, but without selecting the row. This is how I am doing it now, and it seems dumb.
For intCtr = 0 To lstUserRoleAll.Items.Count -1
lstUserRoleAll.SelectedIndex = intCtr
InsertUserRoles(lstUserRoleAll.SelectedValue)
Next
I have read that lstUserRoleAll.Items(intCtr).value should work, but value isn't a member of that. Also, lstUserRoleAll.Items(intCtr).tostring gets me the text "System.Data.DataRowView".
Surely there is a better way to do this.
Here is the code that I use to add the rows:
Dim rsSelectedGroups As DataTable
strSQL = "SELECT UID, Name "
[Rest of query]
rsSelectedGroups = DataAccess.GetDataTable(strSQL)
lstUserRoleCurrent.DataSource = rsSelectedGroups
lstUserRoleCurrent.ValueMember = "UID"
lstUserRoleCurrent.DisplayMember = "Name"

Because I populated the listbox with a datatable, the following code works. I did not realize that is what was being returned.
For intCtr = 0 To lstUserRoleAll.Items.Count
InsertUserRoles(lstUserRoleAll.Items(intCtr).Item("UID"))
Next

Related

How to add a new row into DataGridView and fill it with data

This is my code
Public Sub InvoicItmCall()
'If IteDataTb.Rows.Count = 0 Then Exit Sub
SellingDGView.Rows.Add()
SellingDGView.Rows.Item(0).Cells(0).Value = SellingDGView.Rows.Item(0).Cells(0).Value + 1
SellingDGView.Rows.Item(0).Cells(1).Value = IteDataTb.Rows(0).Item("IteFName")
SellingDGView.Rows.Item(0).Cells(2).Value = IteDataTb.Rows(0).Item("IteFSizeUnit")
SellingDGView.Rows.Item(0).Cells(4).Value = IteDataTb.Rows(0).Item("IteFSellpris")
SellingDGView.Rows.Item(0).Cells(6).Value = IteDataTb.Rows(0).Item("IteVat")
'Dim unused = SellingDGView.Rows.Add(rows)
End Sub
Right now, the code works fine and fills the first row on the grid, this is for a barcode scanner. When I scan one barcode, it fills the row with the appropriate data. However, when I scan another code, it overwrites the older row.
I want it to add a new row and add the other data to it.
So when I scan a different item after the first one, it should fill up a new row.
How do I do that?
Thanks in advance :)
EDIT:
I have found a workaround
If IteDataTb.Rows.Count > 0 Then
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(row.Item("IteFName"), row.Item("IteFSizeUnit"), row.Item("IteFSellpris"), row.Item("IteVat"))
Next row
End If
This creates a new row every time and fills it. But now I want to fill specific cells.
How do I do that?
Right now it fills everything inappropriately.
If you wanted to do it the way you originally were, I'd do it like this:
Dim index = SellingDGView.Rows.Add()
Dim newRow = SellingDGView.Rows(index)
'Dim newRow = SellingDGView.Rows(SellingDGView.Rows.Add())
Dim dataRow = IteDataTb.Rows(0)
newRow.Cells(0).Value = SellingDGView.Rows(0).Cells(0).Value + 1
newRow.Cells(1).Value = dataRow("IteFName")
newRow.Cells(2).Value = dataRow("IteFSizeUnit")
newRow.Cells(4).Value = dataRow("IteFSellpris")
newRow.Cells(6).Value = dataRow("IteVat")
If you wanted to use the more appropriate overload of Add that I mentioned and you used in your second example:
For Each row As DataRow In IteDataTb.Rows
SellingDGView.Rows.Add(SellingDGView.Rows(0).Cells(0).Value + 1,
row("IteFName"),
row("IteFSizeUnit"),
Nothing,
row("IteFSellpris"),
Nothing,
row("IteVat"))
Next
The If statement is pointless.
Also, I doubt that you're getting the next ID in the best way with that expression that adds 1 but that's a different question.
Every time that you need a new row, you can do as follows:
Dim i As Integer = SellingDGView.Rows.Add()
SellingDGView.Rows.Item(i).Cells(0).Value = i
SellingDGView.Rows.Item(i).Cells(1).Value = IteDataTb.Rows(j).Item("IteFName")
SellingDGView.Rows.Item(i).Cells(2).Value = IteDataTb.Rows(j).Item("IteFSizeUnit")
SellingDGView.Rows.Item(i).Cells(4).Value = IteDataTb.Rows(j).Item("IteFSellpris")
SellingDGView.Rows.Item(i).Cells(6).Value = IteDataTb.Rows(j).Item("IteVat")
Herein, I've assumed that the variable "j" indicates to your desired row in IteDataTb datagridview. I mean that you should control the "j" variable yourself. I can help you if know more about the IteDataTb.

Multiple string replace at one time

I have this string abcd, and I want to replace the a with [a|b] and the b with [c|d]
I try many ways to do it, like
Dim varString As String = "abcd"
varString = varString.Replace("a", "[a|b]")
varString = varString.Replace("b", "[c|d]")
The result I get is
[a|[c|d]][c|d]cd
Instead I want it like this
[a|b][c|d]cd
The problem is every time I use the replace function it backs to change the values I already replaced before so I replaced a with [a|b] but then when I do my second command to replace the b it changes the b in [a|b] that I just changed and I don't want this.
I tried to use StringBuilder but it gives the same result.
Please advise me,
I solved the problem by making an array in this way
Dim NewCommand As String = "abcd"
For i = 0 To LikeCommand.Length - 1
If LikeCommand(i) = "a" Then
NewCommand += "[a|b]"
ElseIf LikeCommand(i) = "b" Then
NewCommand += "[c|d]"
Else
NewCommand += LikeCommand(i)
End If
Next
LikeCommand = NewCommand
Or just switch the logic up. But obviously I'm thinking you're using a basic example for a more complex question.
dim varString as string = "abcd"
varString = varString.Replace("b" ,"[c|d]")
varString = varString.Replace("a" ,"[a|b]")
That would get you the desired results.

Managing Blank field from Linq Query

I am writing a method in VB.Net that runs a LINQ query on my database, and puts the information from the query into textboxes on a form.
Public Sub OpenPartWindow(ByVal partNumber As String)
Using dbContext As New DB_LINQDataContext
Dim query = (From p In dbContext.Parts
Join ppl In dbContext.Part_Price_Logs On p.Part_ID Equals ppl.Part_ID
Where p.Part_Number = partNumber
Select p.Part_Number, p.Part_Description, p.Part_Information, p.Supplier.Supplier_Name, _
p.Part_Manufacturer.Manufacturer_Name, p.Part_Subcategory.Part_Category.Category_Description, _
p.Part_Subcategory.Subcategory_Description, ppl.Cost_Per_Unit, ppl.Discount_Percentage).First()
MessageBox.Show(query.ToString())
txtPartNum.Text = query.Part_Number.ToString()
txtSupplier.Text = query.Supplier_Name.ToString()
txtManufacturer.Text = query.Manufacturer_Name.ToString()
txtPrice.Text = query.Cost_Per_Unit.ToString()
txtDiscount.Text = query.Discount_Percentage.ToString()
txtDescription.Text = query.Part_Description.ToString()
txtInfo.Text = query.Part_Information.ToString()
Me.Show()
End Using
End Sub
The issue I am having right now is with the last field, the txtInfo TextBox. You see the Part_Information field on the database allows NULL values. So when I go to fill the field when the field is null I am getting a NullReferenceException, which is understandable. However I cannot find a way to get around this exception.
I've tried:
If Not IsNothing(query._Part_Information.ToString()) Then
As well as
If query.Part_Information.Length > 0 Then
As If statements to run through first. But I keep getting the error each time. So I am confused on how I am suppose to deal with this error.
You can use either of the following options:
txtInfo.Text = String.Format("{0}", query.Part_Information)
txtInfo.Text = $"{query.Part_Information}" → $ - String Interpolation
txtInfo.Text = query.Part_Information?.ToString() → ?. - Null Conditional Operator
If query.Part_Information is null, the first two expressions result in Sting.Empty and the last one result in Nothing.

VB.Net Must be non-negative and less than the size of the collection

Index was out of range. I was trying to put the selected rows in frm_Guest to frm_Main:
Private Sub dgview_GPIGuest_CellContentClick_1(sender As Object, e As
DataGridViewCellEventArgs) Handles dgview_GPIGuest.CellContentClick
Dim ID As String = dgview_GPIGuest.SelectedRows(0).Cells(0).Value.ToString()
Dim FName As String = dgview_GPIGuest.SelectedRows(0).Cells(1).Value.ToString()
Dim LName As String = dgview_GPIGuest.SelectedRows(0).Cells(2).Value.ToString()
Dim Gender As String = dgview_GPIGuest.SelectedRows(0).Cells(3).Value.ToString()
Dim Address As String = dgview_GPIGuest.SelectedRows(0).Cells(4).Value.ToString()
Dim IDType As String = dgview_GPIGuest.SelectedRows(0).Cells(5).Value.ToString()
Dim IDNumber As String = dgview_GPIGuest.SelectedRows(0).Cells(6).Value.ToString()
frm_Main.txt_GPIId.Text = ID
frm_Main.txt_GPIFirstName.Text = FName
frm_Main.txt_GPILastName.Text = LName
frm_Main.txt_GPIAddress.Text = Address
frm_Main.txt_GPIIDNumber.Text = IDNumber
frm_Main.txt_GPIIdType.Text = IDType
If Gender = "Male" Then
frm_Main.rb_GPIMale.Checked = True
Else
frm_Main.rb_GPIFemale.Checked = True
End If
End Sub
I think the confusion is between selected row and cell click. A row is selected by clicking in the far left column of the grid. The entire row will highlight. When a single cell is clicked there are no selected rows; therefore index 0 is out of range. If you want to use this event then use the DataGridViewCellEventArgs as #jmcilhinney suggested.
Debug.Print($"Row {e.RowIndex.ToString}, Column {e.ColumnIndex}")
Firstly, not sure if you already did it but you must set the SelectionMode property to FullRowSelect either in design time or from code behind :
Dgvw1.SelectionMode = DataGridviewSelectionMode.FullRowSelect
The reason for this is to select the entire row when you click on a cell.
Moving on, the usage of SelectedRows is quite unnecessary here(assuming MultiSelect is not set). What i understand so far is that you want to get the column value of the selected row, right ? Here's a quick code :
Dim row = dataGridView1.CurrentCell.RowIndex; ''This gets the row index(or count) of the selected row
Dim col = dataGridView1.CurrentCell.ColumnIndex; ''This gets the column index(or count) of the selected row
Dim ID As String = dataGridView1.Rows(row).Cells(col).Value.ToString()) ''Now note that here i am getting the selected column/cell's value, you can change it with any required index :)
Hope this helps.
The reason you received the error
It's pretty simple to understand. It is somewhat like you are trying to subtract 1 from 0 which will result in -1 but in programming(in cases of collections) , it is an invalid value. I assume when you select a cell, your entire row isn't selected(default behavior)(read the beginning of the answer to fix). That's why SelectedRows(), which is supposed to return all selected rows, returns null. And finally you get the error

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next