Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 660
  • Last Modified:

DateTime Datagrid update - Specificed Cast is not valid

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
tbaseflug
Asked:
tbaseflug
  • 6
  • 5
  • 4
1 Solution
 
JoeisanerdCommented:
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
 
dfiala13Commented:
You need to type it.

Try this

.Value = DateTime.Parse(((TextBox)e.Item.Cells[3].FindControl("calGhostedUpDate")).Text);
0
 
tbaseflugAuthor Commented:
dfiala13 -
Still throwing the error - I am making certain that the date format is restricted to a short datetime format and am stumped
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JoeisanerdCommented:
what does your data format look like?
0
 
dfiala13Commented:
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
 
JoeisanerdCommented:
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
 
tbaseflugAuthor Commented:
Actually -

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

DateTime.Parse(((TextBox)e.Item.FindControl("calScheduledDate")).Text);
0
 
dfiala13Commented:
You have to look in a specific cell!

DateTime.Parse(((TextBox)e.Item.Cells[iCell].FindControl("calScheduledDate")).Text);
0
 
tbaseflugAuthor Commented:
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
 
JoeisanerdCommented:
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
 
dfiala13Commented:

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
 
dfiala13Commented:

lblerror.Text = (TextBox)e.Item.Cells[4].Controls[1].Text;
 
0
 
tbaseflugAuthor Commented:
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
 
JoeisanerdCommented:
that's because it needs ( ) around it like this ((TextBox)e.Item.Cells[4].Controls[1]).Text;
0
 
dfiala13Commented:
lblerror.Text = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now