I'm stuck on something simple that I can't figure out why doesn't work now.
I tested the same code before(last Friday) and it works
I just need to get an String with four hexadecimal digits from an integer variable no mather what number between &H0000 and &HFFFF
So I write this simple code
Dim NumHex As Integer = 352
Dim NumHexStr As String = String.Format("{X4:0}", NumHex)
But now I'm getting
"Input string was not in a correct format."
Can you see something wrong on that code?
Thanks.
Your format condition must be String.Format("{0:X4}", value) or Value.ToString("X4") or may be Hex(value).PadLeft(4, "0"c)
Related
I have a small problem in my vb.net code. The error is
conversion to string 'System.data.dataRowView' to type integer is invalid
How can I fix it? I have 2 dropdownlist, first one; when I choose an option, all the data related to that option, should appear in the second dropdownlist. Part of my code:
Dim sb As New StringBuilder
sb.Append("select date_collected, spectral_id from Spectral_information")
sb.Append("inner join Crop on crop_id = si_crop_id")
sb.Append("where si_crop_id =")
sb.Append(CInt(crop.SelectedValue.ToString()))
Dim str As String = sb.ToString()
etc..
The line
sb.Append(CInt(crop.SelectedValue.ToString()))
seems silly doesn't it? Why convert the selected value to string, then int, then back to string in StringBuilder.Append(Integer)? If you expect an Integer there, then you have an issue when there isn't. You should address that first. Changing the line to this
sb.Append(crop.SelectedValue.ToString())
should make it work. Then you need to address the logic.
I have a VB.net project in that I need to convert a character into an hex value. According to https://msdn.microsoft.com/de-de/library/963zt96e(v=vs.90).aspx, I tried this (example):
Dim sChar as String = "€"
Dim sNum as Integer = AscW(sChar)
Dim sHex as String = hex(sNum).ToString
When I set a breakpoint after that, I get these values in direct console:
?sNum
8364
?hex(sNum)
"20AC"
Which is correct and works as expected.
But the value calculated in running program is garbage:
?sHex
"20254"
Why do I get different results in running code and direct input console?
And how do I get the expected string value ("20AC")?
okay, found a solution (but no explaination)
sHex = convert.ToString(sNum, 16)
I cannot explain these wrong values, but the code above works for me, just in case that someone else has the same problem.
Is it possible to convert, say, a textbox input to working code?
For example, user types 'if x<10 then y=2 else y=5' into textbox1, it gets used directly as code something like ...
dim x as integer = 5
dim y as integer = 0
include processed textbox1.text
resultbox.text = (y*20).tostring
It's not important why this would be needed - just whether there is any straight-forward method that parses a string to code.
Many thanks.
Maybe this is what you are looking for:
http://www.codeproject.com/Articles/12852/Compile-and-Run-VB-NET-Code-using-the-CodeDom
yes you can do this using the VBCodeProvider class. Although the amount of code required is quite significant:
http://www.codeproject.com/Articles/5472/Compiling-NET-code-on-the-fly
data table contain column named as Fld_primary. this column contain value like 0.00 it is double datatype in mysql table.i want store that datatable value in double variable. am always getting error when i convert to double datatype.
my code
-------
Dim ds5 As dataset1.DTSUMDataTable = TA5.GetData(users)
Dim dttot As Double
dttot = CType(ds5("fld_primary").ToString, Double)
Error
Conversion from string "fld_primary" to type 'Integer' is not valid.
Edited # 3:01 AM with most recent screen caps.
Sometimes I find myself second guessing certain code based on people's answers, but I went ahead and took the time to check if the code that they are all using is even valid:
As you can see that code is a no go, so I used the correct code you see at the bottom there to reference a column.
However, if you wish to get a single cell use the chunk of code below that uses the foreach loop (the rest is my basic setup to show you how it works):
"Y" will equal the value of the datatable cell and you may convert it using the Double.Parse() method:
Dim y = Double.Parse(zDataRow("cat").ToString())
Be careful, if you have multiple rows you will notice that the value of y will change as it makes its way through all the rows.
you can convert it using the Convert class.
Dim dttot As Double = Convert.ToDouble(ds5("fld_primary"))
Your error is actually: ds5 expects an integer as a parameter, so using ds5("fld_primary") is not valid in your code. Perhaps you can try ds5(0)("fld_primary").
After you fixed it, use
dttot = Double.Parse(whatever_string_you_should_put_here)
If you cannot ensure your string must be a valid double, then use Double.TryParse.
You are better off using the 'Double.TryParse' way of converting as this handles any errors better and simply returns a boolean if succesful or not, using 'parse' will throw an exception which isnt anywhere near as elegant.
Dim dub as Double = 0
Double.TryParse("Your String Here", dub)
Try using Double.TryParse(text,value)
Try using this:
For a=0 to yourgridview.rows.count-1
Yourgridview.rows(a).cells(targetcolumnnumber).value=cdbl(Yourgridview.rows(a).cells(targetcolumnnumber).value)
Next
How do I convert from a string to an integer? Here's what I tried:
Price = CInt(Int(txtPrice.Text))
I took out the Int and I still got an exception.
Use
Convert.toInt32(txtPrice.Text)
This is assuming VB.NET.
Judging by the name "txtPrice", you really don't want an Integer but a Decimal. So instead use:
Convert.toDecimal(txtPrice.Text)
If this is the case, be sure whatever you assign this to is Decimal not an Integer.
You can try it:
Dim Price As Integer
Int32.TryParse(txtPrice.Text, Price)
You can use the following to convert string to int:
CInt(String) for ints
CDec(String) for decimals
For details refer to Type Conversion Functions (Visual Basic).
Please try this, VB.NET 2010:
Integer.TryParse(txtPrice.Text, decPrice)
decPrice = Convert.ToInt32(txtPrice.Text)
From Mola Tshepo Kingsley (WWW.TUT.AC.ZA)
Convert.ToIntXX doesn't like being passed strings of decimals.
To be safe use
Convert.ToInt32(Convert.ToDecimal(txtPrice.Text))
You can try these:
Dim valueStr as String = "10"
Dim valueIntConverted as Integer = CInt(valueStr)
Another example:
Dim newValueConverted as Integer = Val("100")
Use Val(txtPrice.text)
I would also allow only number and the dot char by inserting some validation code in the key press event of the price text box.
If there might be invalid characters in the textbox it will throw an exception. The Val command pulls numbers and strips invalid characters. It returns a double. So you want to convert the result of Val to whatever type you need.
Price = Convert.toInt32(Val(txtPrice.Text))
This will return 0 instead of throwing an error on invalid input. If that isn't desired you should be checking that the input is valid before you convert.