user1270384
user1270384

Reputation: 721

Specified cast is not valid when getting Datatable Column Value

The above error message appears when I am trying to get Column Value from Datatable.

This is what I find in the stacktrace:

System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 source)

 and this in the TargetSite when debugging:

{ Boolean b__0(System.Data.DataRow)}

Here is my code: DataTable hr = new DataTable();

            hr.Columns.Add("BookingDate");
            hr.Columns.Add("BookingId");
            hr.Columns.Add("BookingSource");
            hr.Columns.Add("CheckInDate");
            hr.Columns.Add("CheckOutDate");


            for (int i = 0; i < gmisc.GetModifiedBookings(gmoreq).Bookings.Length; i++)
            {
                hr.Rows.Add();
                hr.Rows[i]["BookingDate"] = Convert.ToDateTime(gmisc.GetModifiedBookings(gmoreq).Bookings[i].BookingDate.ToString());
                hr.Rows[i]["BookingId"] = Convert.ToInt64(gmisc.GetModifiedBookings(gmoreq).Bookings[i].BookingId.ToString());
                hr.Rows[i]["BookingSource"] = gmisc.GetModifiedBookings(gmoreq).Bookings[i].BookingSource.ToString();
                hr.Rows[i]["CheckInDate"] = Convert.ToDateTime(gmisc.GetModifiedBookings(gmoreq).Bookings[i].CheckInDate.ToString());
                hr.Rows[i]["CheckOutDate"] = Convert.ToDateTime(gmisc.GetModifiedBookings(gmoreq).Bookings[i].CheckOutDate.ToString());



            }
            Int64 BookingId = (from DataRow dr in hr.Rows
                          where (Int64)dr["BookingId"] == BookId
                          select (Int64)dr["BookingId"]).FirstOrDefault();
                TextBox1.Text = Convert.ToString(BookingId);

Where did I go wrong, if somebody can please tell me.

Upvotes: 1

Views: 9398

Answers (3)

Ali Adravi
Ali Adravi

Reputation: 22833

Check your code, the very first two lines:

hr.Rows[i]["BookingDate"] = Convert.ToDateTime(gmisc.GetModifiedBookings(gmoreq).Bookings[i].BookingDate.ToString());
hr.Rows[i]["BookingId"] = Convert.ToInt64(gmisc.GetModifiedBookings(gmoreq).Bookings[i].BookingId.ToString());

if gmisc.GetModifiedBookings(gmoreq).Bookings[i].BookingDate is null then ??? you are trying to convert it into string and then to datetime

if null then .ToString will give error "Specified cost......."

and same will happen at the time of converting to datetime.

Upvotes: 1

Tilak
Tilak

Reputation: 30728

If dr["BookingId"] is never null (otherwise add null check)

Use

                  Int64 BookingId = (from DataRow dr in hr.Rows
                        where Int64.Parse(dr["BookingId"].ToString()) ==BookId
                        select Int64.Parse(dr["BookingId"].ToString())).FirstOrDefault();

Instead of

                  Int64 BookingId = (from DataRow dr in hr.Rows
                      where (Int64)dr["BookingId"] == BookId
                      select (Int64)dr["BookingId"]).FirstOrDefault();

Upvotes: 1

phoog
phoog

Reputation: 43076

When you create a data column using the Add(string) overload, the type of the column is string (see http://msdn.microsoft.com/en-us/library/52xzw8tf.aspx). You cannot cast a string directly to Int64 or DateTime.

Use the Add(string, Type) overload or the Add(string, Type, string) overload to specify the type of the column's data.

Upvotes: 0

Related Questions