473,395 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Specified Cast is not valid

40
What I'm trying to do in a listview is calculate the time now from a time in the table and place that result into a column on the grid for each record.
I was able to do this great using a datagrid, I'm exploring into the listview world and I cant seem to figure out how to do this...here is my code

Expand|Select|Wrap|Line Numbers
  1. Function fncDaysOpen()
  2.  
  3.         Dim row As Integer
  4.               Try
  5.             For row = 0 To Mydataset3.Tables(0).Rows.Count - 1
  6.  
  7. 'Convert DateTime String              
  8.                 Dim order_date As String
  9.                 order_date = lstGrid.Items(row).SubItems(2).Text
  10.                 Dim conv As DateTime
  11.                 conv = System.Convert.ToDateTime(order_date)
  12.  
  13.                 Dim d1 As DateTime = conv
  14.                 Dim d2 As DateTime = System.DateTime.Today
  15.  
  16.                 Dim subtract, absday
  17.                 subtract = 0
  18.                 Select Case DatePart("w", d1)
  19.                     Case 1
  20.                         subtract = subtract + 1
  21.                     Case 7
  22.                         subtract = subtract + 2
  23.                 End Select
  24.  
  25.                 Select Case DatePart("w", d2)
  26.                     Case 1
  27.                         subtract = subtract + 2
  28.                     Case 7
  29.                         subtract = subtract + 1
  30.                 End Select
  31.                 absday = DateDiff("d", d1, d2) - 1
  32.  
  33.                 fncDaysOpen = absday - CInt(2 * (absday \ 7))
  34.  
  35.                'I get My Error Right Here - when placing results for each rec.
  36.  
  37.                 lstGrid.Items(row).SubItems(7).Text = fncDaysOpen     
  38.  
  39.                            Next row
  40.         Catch ex As Exception
  41.             MsgBox(ex.Message)
  42.         End Try
  43.     End Function<b></b>
Thanks for any help
Sep 28 '07 #1
6 1957
Plater
7,872 Expert 4TB
First off, why are you doing all that date part work?
DateTime objects can be subtracted from eachother

Expand|Select|Wrap|Line Numbers
  1. dim t as TimeSpan
  2. t= (TimeSpan)(DateTime.Now-saveddatetime)
  3. dim numdays as Double
  4. numdays=t.TotalDays
  5.  
  6. 'now you have a double with the total number of whole(and fractional!) days inbetween the two dates. Feel free to round off as you wish.
  7.  
Secondly,
Does your listgrid have as many rows as your dataset already? Do they have a 7th subitem?
Sep 28 '07 #2
KA NMC
40
First off, why are you doing all that date part work?
DateTime objects can be subtracted from eachother

Expand|Select|Wrap|Line Numbers
  1. dim t as TimeSpan
  2. t= (TimeSpan)(DateTime.Now-saveddatetime)
  3. dim numdays as Double
  4. numdays=t.TotalDays
  5.  
  6. 'now you have a double with the total number of whole(and fractional!) days inbetween the two dates. Feel free to round off as you wish.
  7.  
Secondly,
Does your listgrid have as many rows as your dataset already? Do they have a 7th subitem?
Thanks for that Code Snippet I'll give that a shot...
Yes grid has as many rows as my dataset. Rows 0 - 6 is coming from the database - row 7 is what I created to store the calculations of Date.Now - row 2.

Thanks for the help and the suggestion.
Oct 1 '07 #3
Plater
7,872 Expert 4TB
ahh, if your listview is bound to a datasource, you cannot add columns/rows to it. What you can do as an easy workaround, is in your SQL query, get another column that you can use as scratch.
So if you have "SELECT * FROM MYTABLE" you would say "SELECT *, myIDColumn as [Computed Column] FROM MYTABLE"
And then you would have an extra column called "Computed Column" that you can over-write the data in to be your computed value.
Oct 1 '07 #4
KA NMC
40
OHHHH.....I'll give that a shot...Question? how would I do this if I have to create two columns - syntax?? - "Select * myIDcolumn as [Computed Column_one], [Computed Column_two] Frm Mytable"

I'll definetly give this a shot. Thanks for your help.

ahh, if your listview is bound to a datasource, you cannot add columns/rows to it. What you can do as an easy workaround, is in your SQL query, get another column that you can use as scratch.
So if you have "SELECT * FROM MYTABLE" you would say "SELECT *, myIDColumn as [Computed Column] FROM MYTABLE"
And then you would have an extra column called "Computed Column" that you can over-write the data in to be your computed value.
Oct 1 '07 #5
Plater
7,872 Expert 4TB
Well let's say you have a table with the following columns:
ID | fname | lname | uname
-----------------------------------------

"SELECT *"
would return those 4 columns as:
("ID", "fname", "lname", "uname")

"SELECT *, ID as [Computed Column]"
would return those 4 columns plus an additional column as:
("ID", "fname", "lname", "uname", "Computed Column")

"SELECT *, ID as [Computed Column], ID as [Computed Column2]"
would return those 4 columns plus 2 additional columns as:
("ID", "fname", "lname", "uname", "Computed Column", "Computed Column2")
Oct 1 '07 #6
KA NMC
40
Got it thanks...For all your help...Now I just got to get it nested with another table like my datagrid. I dont think I'm a fan of Listviews yet...alot I need to learn. Thanks Again.

Well let's say you have a table with the following columns:
ID | fname | lname | uname
-----------------------------------------

"SELECT *"
would return those 4 columns as:
("ID", "fname", "lname", "uname")

"SELECT *, ID as [Computed Column]"
would return those 4 columns plus an additional column as:
("ID", "fname", "lname", "uname", "Computed Column")

"SELECT *, ID as [Computed Column], ID as [Computed Column2]"
would return those 4 columns plus 2 additional columns as:
("ID", "fname", "lname", "uname", "Computed Column", "Computed Column2")
Oct 1 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
4
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Stack Trace:
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.