Solved

DateTime Datagrid update - Specificed Cast is not valid

Posted on 2004-04-01
15
607 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now