get first row data from column from parameter tableadapter - vb.net

I am trying to return the first row value on colunn rep-id. Here is what I have but is only working half way I am not getting my string value
Dim text as string = sdTableAdapter.Repds(endweek).Rows (0)
How do I get the first row string from my column

You can use the Field extension method, assuming it's the first column:
Dim table As DataTable = sdTableAdapter.Repds(endweek)
Dim text As String = table.Rows(0).Field(Of String)(0)
You can also use it via column-name:
Dim text As String = table.Rows(0).Field(Of String)("ColumnName")
You'll get an exception if the type is not String, then you need to convert it from the correct type(e.g. Int32):
Dim text As String = table.Rows(0).Field(Of Int32)("ColumnName").ToString()

Related

pass datagridview in specific columns from another datagridview with delete previous data vb.net

I am trying to pass the data from one datagridview to another when I select a row, but I am trying to make it in specified columns the data I need to pass.
these are the fields that I select to pass to another datagridview in the columns with the same names.
but it happens like this
this is the code
Dim referencia As String = ROW.Cells("Column1").Value
Dim talla As String = ROW.Cells("Column2").Value
Dim color As String = ROW.Cells("Column3").Value
Dim descripcion As String = ROW.Cells("Column4").Value
Dim precio As String = ROW.Cells("Column5").Value
FrmFacturaVenta.DataGridView1.Rows.Add(referencia, talla, color, descripcion, precio)
Next

Using Split in Vb.net in last dash

i have two example
"hcg.com.ph?C402-10A-2012-06132017-22"
"hcg.com.?C3032-1B-2012-06132017-1"
output should be
hcg.com.ph?C402-10A-2012-06132017
hcg.com.?C3032-1B-2012-06132017
but i got
hcg.com.ph?C402 and hcg.com.?C3032
Dim FinalSplt() As String
Dim ItemBaseCode As String
FinalSplt = value.ToString.Split("-")
ItemBaseCode = FinalSplt(0)
How to split in the last dash?
Here is some code that uses Substring and LastIndexOf.
'test input
Dim si() As String = {"hcg.com.ph?C402-10A-2012-06132017-22", "hcg.com.?C3032-1B-2012-06132017-1"}
'use to verify
Dim sv() As String = {"hcg.com.ph?C402-10A-2012-06132017", "hcg.com.?C3032-1B-2012-06132017"}
For x As Integer = 0 To si.Length - 1
Dim s As String = si(x).Substring(0, si(x).LastIndexOf("-"c))
'verify
If s = sv(x) Then
Stop 'verified
End If
Next
Ok, without actually writing code I can see you need to split the string more efficiently.
Firstly, strip the quotes.
Secondly, split the string based on the ? mark.
Take the second string in the array, and split that based on the - mark.
Now you have an array of all the portions, join this array with all except the last element.
Join the new string with the original first part.
Add the quotes back if needed.

VBA How to declare an array with elements with different datatype

I have an array that I have created to store error records and it has the following elements:
Serial No,File name, error type, error cell, error cell value
As of now I have declared my array like this and then I populate the values later on.
Dim errorArray() As String
But ideally, I want Serial Number to be a proper integer, but it is getting converted into string. I don't know how to declare this correctly so that I can have Long datatype for the first element and string for the next 3 and variant for the last one.
Create a private type in your code, like this:
Private Type ErrRecord
SerialNo As Long
FileName As String
ErrorType As String
ErrorCell As String
ErrorCellValue As Variant
End Type
And then in your routine, call it like this:
Dim errorArray(0) As ErrRecord
With errorArray(0)
.SerialNo = 12345
.FileName = "Test.xls"
.ErrorType = "Bad error"
.ErrorCell = "1234"
.ErrorCellValue = "Test"
End With
You'll want to create an array of type Variant. An array of type Variant can store any data type in any of its elements.
Dim astrItems(0 To 9) As String
Dim varItems As Variant
varItems = astrItems

splitting a string in VB

I have a ComboBox which I assign to a variable:
Dim var as String = ComboBox1.SelectedValue
Dim name As String = var.Split(",")
This gives me the error
Value of type '1-dimensional array of string' cannot be converted to String
Any ideas as to where I'm going wrong?
Split returns an array of strings. Your variable needs to be changed to an array, not just a single string.
My VB is a bit rusty, but I think you have to make name an array:
Dim name() As String = var.Split(",")
name needs to be declared as an array.
dim name() as string = var.split(",")
The split() method will break up the string based on the given character and put each newly created string into an array and return it.
This is what your error message is telling you:
Value of type '1-dimensional array of string' cannot be converted to String
The method returns an array of string, but your trying to put it into just a string!
EDIT: In response to your answer...
So far you've managed to split the string yourself with the split method. To output this to your message box, you need to concatenate the two elements in the proper order:
msgbox(name(1) & " " & name(0))
Notice I indexed the array twice! Element 1 is the first name, element 0 is the last name. Remember you got this name in lname,fname format. Passing the array itself doesn't make sense! Remember, a datatype is not equal to an array of that type, they are two different things. Therefore a string is not compatible with a string array. However, each individual element of the array is a string, and so each of those are compatible with the string type (because they're the same thing)!
Dim var As String = ComboBox1.SelectedValue
Dim temp() As String = Split(var, ",", -1, CompareMethod.Binary)
Dim name As String = temp(0)
Or maybe "name" isn't an array and the goal is to populate "name" with everything up until the first comma, in which case the fix would be:
Dim name as String = var.Split(",")(0)
Note: assumes that var is not Nothing.

compare a string and trim in vb.net

I have this string that shall come in from another file. The string has maximum length of 102 digits. I need to compare the string with numbers in a pair and delete those from that string.
e.g - 6125223659587412563265... till 102
numbers that compare with this string-
first set - 61
new string = 25223659587412563265
second set - 36
new string = 252259587412563265
and so on. the set of numbers shall go to maximum of 51 pairs = 102, which shall give an end result of string = ""
How can i achieve this in a loop?
this is not answer, this is editing the question. i dont know why but the edit button just vaniashed so i have to edit question here.
No duplicates will ever be in this string. and in the end when compares are done, i want to see what numbers are left in pairs.
Dim input As String = "6125223659587412563265"
Dim targets As String() = {"61", "36"}
For Each target As String In targets
input = input.Replace(target, "")
Next
Debug.Assert(input = "252259587412563265")
Here is a simple solution. You will need to add your pairs to the List(Of String) and also initialize input to the string you want to alter.
Dim pairs As New List(Of String)()
Dim input As String = String.Empty
For Each pair As String In pairs
input = input.Replace(pair, String.Empty)
Next