show database time span value to numeric updown control - vb.net

I am saving my numericupddown value like this into database
Dim value As Date = New Date(Now.Year, Now.Month, Now.Day, 0, NumericUpDown1.Value, 0)
Dim valueAsString As String = value.ToString("HH:mm:ss")
..it will save data like 00:05:00.
I want to fetch corresponding data to my numeric updown control.
I am fetching data using my data reader.then i try to give code like this:
NumericUpDown1.Value = Convert.ToDecimal(dr("Timeinterval")).ToString
But showing
error:Unable to cast object of type `System.TimeSpan` to type `System.IConvertible`.
How can I show corresponding data to my numericupdown control?

You are trying to convert 00:05:00 to a decimal, which obviously is wrong. Before saving to database, you changed the value in your updown control to HH:mm:ss format. Now you need to write custom logic to convert 00:05:00 to number like ((HH * 60) + mm + (ss / 60)) before you would be able to convert it back to Decimal.

Related

How to convert a "Date" type array to a "String" type array (VB)

My Code:
listDates = (From row In ds.Tables(0) Select row.Field(Of Date)("Check In")).ToList
arrDates = listDates.ToArray
For a = 0 To arrDates.Length - 1
arrDatesStr(a) = (arrDates(a)).ToString
Next
I am making an analytics form to find the most popular month for a hotel:
listDates is a list that stores all of the dates from the database
arrDates is the conversion of listDates to an array
arrDatesStr is the array I need to fill with the date values but as strings, so i can substring the month
I have checked the values in listDates and arrDates and they conversion is successful, but when I try to convert it to string it says
"System.NullReferenceException: 'Object reference not set to an instance of an object.'"
How do I convert arrDates to a string and input those values into arrDatesStr?
arrDatesStr(a) has not been initialized.
You can add something like dim arrDatesStr(arrDates.length-1) before you use arrDatesStr. Better yet, don't use arrDates at all:
For a = 0 To listDates.Length - 1
arrDatesStr(a) = listDates(a).ToString
Next
If you use date.month instead of substring to get the month, you won't have to worry about whether it's a 1 or 2 digit month, and you won't have to convert it to string.
m = listDates(a).Month
Unless you have a good reason not to, you should let the conversion create the array for you:
Dim dateStrings = Array.ConvertAll(dates, Function(dt) dt.ToString())
or:
Dim dateStrings = dates.Select(Function(dt) dt.ToString()).ToArray()
That will create a String array of the appropriate length, containing the appropriate data.
Note that the first option requires an array but the second option will work on any IEnumerable(Of Date), which includes the List(Of Date) or even the original query.

how to convert the string to date in vb.net

I AM FACING ONE PROBLEM.
I AM USING BELOW METHOD FOR MAKING STRING VALUE TO DATE.
string values is like this ="01/12/2002" like "dd/MM/YYYY"
My problem is that.
two string values
->1)01/01/2025
->2)1/1/2025
i am getting the Value like above 1 or 2
Dim d As Date = DateTime.ParseExact(dumm, "dd/MM/yyyy", Nothing)
if 1 comes nothing will happen but
if i get 2 i am facing error like String was not recognized as a valid DateTime.
As per my Analysts what i understood is date should be 2 digits Remaining all are two digits other wise giving the error.
but some times i am getting single digits from the excel to vb.net
how can i solve this issue...
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact(dumm, "d/M/yyyy", Nothing)
i put the break point on dumm ok dumm vlaues= "1/2/2026 12:00:00 Am"
Error...............
now
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact("01/02/2026", "dd/MM/yyyy", Nothing)
Working Fine.......
Use the date format string "d/M/yyyy h:m:s tt". This will handle both cases i.e. with and without leading zero for the day and month. Additionally, since your actual variable has a time component in addition to the date, you need to add the format string for parsing time as well.
However, I would advise you to use TryParseExact, which will return boolean values based on success or failure of the parse rather than throwing exceptions.
Demo for using TryParseExact along with appropriate format string.

how to set a datagridview column to date format

I have created a DataGridView and I add columns to it like this:
Dim col_1 = New DataGridViewTextBoxColumn
col_1.Name = "Date"
col_1.DefaultCellStyle.Format = "MM/dd/yyyy"
data_grid.Columns.Add(col_1)
then I add data to the column like this:
data_grid.Item(1,1).Value = temp_date
The grid is filled with the correct data and everything works, however...when I click on the column headers, the column that shows the dates does not sort correctly (it sorts alphabetically) and I know this is because I set it up as "DataGridViewTextBoxColumn", but there is no option for a date type column. So how to I set it up as a date column so it sorts based on the date when the header is clicked?
Thanks.
You should also set the ValueType of the column:
DataGridView1.Columns(0).ValueType = GetType(Date)
Then convert date_temp to a Date-value before assigning this to the cell's Value.
Using CDate could be your first attempt:
data_grid.Item(1,1).Value = CDate(temp_date)
otherwise, investigate Parse, TryParse or Convert, to obtain the date-value.

convert numeric up down value to time format

I have one NumericUpDown1 control
if i select 5 in numeric up down control.the i am calling that value like this:
NumericUpDown1.Value
that return value 5 like integer instead of getting 5.
i want to get that value in time format
I if i select 5.that should return 00:05:00..
how i can convert numeric up down value to times..
in my data base i want to store that value the data type time(7).I tryed something like this:
Dim value As TimeSpan = Convert.ToDecimal(NumericUpDown1.Value)
but that is getting error
You are using TimeSpan wrongly, better rely on Date:
Dim value As Date = New Date(Now.Year, Now.Month, Now.Day, 0, NumericUpDown1.Value, 0)
You can convert this variable into a string with the format you want by doing:
Dim valueAsString As String = value.ToString("HH:mm:ss")
This is the right way to use TimeSpan:
Dim value As TimeSpan = New TimeSpan(0, NumericUpDown1.Value, 0)
But I do recommend the Date alternative above. Use TimeSpan just for measuring intervals (between Date type variables) and better store date/time-related information as Date, which is easier to deal with.

vb.net currency display has four zeros instead of two

when i get the money field from sql server to vb.net code, i always get 1.0000 instead of 1.00. how do i convert this to 1.00 in vb.net?
TD = New HtmlTableCell
If Not SqlDR("Price") Is DBNull.Value Then
TD.InnerHtml = SqlDR("Price")
Else
TD.InnerHtml = "0.00"
End If
SQLDR is my sql data reader
That is because SQL Server stores the MONEY field with 4 decimal places. To see it with 2, use the String.Format method.
String.Format("{0:c}", 10) ''result: $10.00
String.Format("{0:N2}", 10) ''result: 10.00
See these pages for more ways to format your numbers
Standard Numeric Format Strings
Custom Numeric Format Strings
You need to format the data when you output it:
myMoney.ToString("0.00");
Are you sure you aren't confusing the display of the value with the actual value?
1.0000 and 1.00 are the same value.
If you want to display only a certain number of places when converting a value to a string, you should look at the Custom Numeric Format Strings section in the MSDN documentation to figure out the format string to pass to the ToString method on the Decimal, Double, Single structures, or the static Format method on the String class.