Solved

DateTime Datagrid update - Specificed Cast is not valid

Posted on 2004-04-01
15
637 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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