Solved

DateTime Datagrid update - Specificed Cast is not valid

Posted on 2004-04-01
15
645 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
Industry Leaders: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
PrintingFoundIt(Almost!(Damn!)) 1 37
How to make a Gridview column hidden (visibility false) in the HTML code? 2 26
Amazon S3 .Net error 5 37
Problem to Office 1 39
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

713 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