Solved

DateTime Datagrid update - Specificed Cast is not valid

Posted on 2004-04-01
15
647 Views
Last Modified: 2008-02-01
I am trying to update a SQL table via a stored procedure / datagrid and have the user entering a date in a textbox and everysingle time it is throwing a an eror at:

Here is the code that I am using -
-----------------------------------------------------------------
protected void SortableGrid1_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                  //Set parameter values based upon DataGrid edit values
                  //int pkid = (int)SortableGrid1.DataKeys[e.Item.ItemIndex];
                  //string SD;
                  //TextBox calScheduledDate = (TextBox)e.Item.Cells[3].FindControl("calScheduledDate");
                  //SD= calScheduledDate.Text;

                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ReImageID", System.Data.SqlDbType.Int, 4)).Value = (int)SortableGrid1.DataKeys[e.Item.ItemIndex];
                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SerialNumber", System.Data.SqlDbType.VarChar, 150)).Value = ((TextBox)e.Item.Cells[3].FindControl("txtSerialNumber")).Text;
                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OldSerialNumber", System.Data.SqlDbType.VarChar, 150)).Value = ((TextBox)e.Item.Cells[3].FindControl("txtOldSerialNumber")).Text;
                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ScheduledDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calScheduledDate")).Text;
                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CopySettingsDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calCopySettingsDate")).Text;
                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@GhostedUpDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calGhostedUpDate")).Text;
                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ReimageDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calReImageDate")).Text;
                  sqlConnection1.Open();
                  sqlSelectCommand2.ExecuteNonQuery();
                  sqlConnection1.Close();
                  sqlSelectCommand2.Dispose();
                  SortableGrid1.EditItemIndex = -1;
                  UpdateDataView();
            }
----------------------------------------------------------------------
And here is the error

-----------------------------------------------------
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 101:                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SerialNumber", System.Data.SqlDbType.VarChar, 150)).Value = ((TextBox)e.Item.Cells[3].FindControl("txtSerialNumber")).Text;
Line 102:                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OldSerialNumber", System.Data.SqlDbType.VarChar, 150)).Value = ((TextBox)e.Item.Cells[3].FindControl("txtOldSerialNumber")).Text;
Line 103:                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ScheduledDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calScheduledDate")).Text;
Line 104:                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CopySettingsDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calCopySettingsDate")).Text;
Line 105:                  this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@GhostedUpDate", System.Data.SqlDbType.DateTime, 8)).Value = ((TextBox)e.Item.Cells[3].FindControl("calGhostedUpDate")).Text;
 



-----------------------------------------------------


            
0
Comment
Question by:tbaseflug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
15 Comments
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10735188
Are you sure that the dates are the ones throughing the errors? I would wrap you DateTime items with the Convert.ToDateTime object

this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@GhostedUpDate", System.Data.SqlDbType.DateTime, 8)).Value = Convert.ToDateTime((TextBox)e.Item.Cells[3].FindControl("calGhostedUpDate")).Text);

see if that does it.

You could also check to see what the value of (TextBox)e.Item.Cells[3].FindControl("calGhostedUpDate")).Text is at runtime by having it write the value to the debug window with Debug.WriteLine(...);
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10735202
You need to type it.

Try this

.Value = DateTime.Parse(((TextBox)e.Item.Cells[3].FindControl("calGhostedUpDate")).Text);
0
 

Author Comment

by:tbaseflug
ID: 10735309
dfiala13 -
Still throwing the error - I am making certain that the date format is restricted to a short datetime format and am stumped
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10735384
what does your data format look like?
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10735427
tbaseflug
um, are all your controls in the same cell?

YOu're looking in Cell[3] for everything.

Might bet is the control does not exist in the column you are looking for it in and you are returning a null.  the TextBox Cast is what is returning the error.
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10735463
my above comment should be "what does your date format look like"

But I did mention above to check to see what the value fo the items are at runtime, because as dfiala13 points out you may not be returning an object at all. If the FindControl method doesn't find anything then it would be null.
0
 

Author Comment

by:tbaseflug
ID: 10735465
Actually -

Even when I switch it to the following I still get the error?

DateTime.Parse(((TextBox)e.Item.FindControl("calScheduledDate")).Text);
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10735483
You have to look in a specific cell!

DateTime.Parse(((TextBox)e.Item.Cells[iCell].FindControl("calScheduledDate")).Text);
0
 

Author Comment

by:tbaseflug
ID: 10735524
ok -

This is weird - even when I comment all of the update code out and try this:

lblerror.Text = ((TextBox)e.Item.Cells[4].FindControl("calScheduledDate")).Text;
I am still getting the specified cast error?
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10735533
put an if statement to see if this is == null --> e.Item.Cells[4].FindControl("calScheduledDate")

if( e.Item.Cells[4].FindControl("calScheduledDate") != null )
{
   then there is something
}
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10735548

Assuming!  that:

calScheduledDate is in Cell 4 (the 5th cell, because we start from 0)

and

it is a TextBox control

then this should work...

lblerror.Text = ((TextBox)e.Item.Cells[4].Controls[1].Text;

0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10735558

lblerror.Text = (TextBox)e.Item.Cells[4].Controls[1].Text;
 
0
 

Author Comment

by:tbaseflug
ID: 10735603
when i enter:
lblerror.Text = (TextBox)e.Item.Cells[4].Controls[1].Text;

I get this when I attempt to build

 'System.Web.UI.Control' does not contain a definition for 'Text'
0
 
LVL 9

Accepted Solution

by:
Joeisanerd earned 500 total points
ID: 10735620
that's because it needs ( ) around it like this ((TextBox)e.Item.Cells[4].Controls[1]).Text;
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10735639
lblerror.Text = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question