Casting DataGridItem

Discussion in 'ASP .Net Datagrid Control' started by Alan Z. Scharf, Jul 8, 2005.

  1. agrids on six different pages which are editable. They each
    draw from a different SQLServer table.

    2. When data is changed in any rows, the Row ID gets added to an array, and
    then a Stored Procedure updates the database table by looping through the
    array of Row ID's.

    I'm using the RowChanged event to add to the array via the function below.

    3. The grids populate OK when the paages are opened, so there is no problem
    with connection..

    4. However, when I edit data and press Save to run the update procedure, I
    get 'Specified cast is not valid error 'on the line that is casting the
    datagruid item.

    5. All editable grids worked for months and then all stopped working at the
    same time.


    Any idea what would cause this problem?

    Thanks.

    Alan


    Function
    ---------------------------------------
    protected void RowChanged(object sender, System.EventArgs e)
    {
    // Add changed record ID's to ChangedRecordList for SaveRecord loop
    --->> Error line: DataGridItem dgi = (DataGridItem) (((Control)
    sender).NamingContainer);
    int recordID = int.Parse(dgi.Cells[0].Text);

    // Add to list if not already there
    if(!ChangedRecordIDList.Contains(recordID))
    {
    ChangedRecordIDList.Add(recordID);
    }
    }

    Stack trace
    ----------------------------------------
    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 167: {
    Line 168: // Add changed record ID's to ChangedRecordList for SaveRecord
    loop
    Line 169: DataGridItem dgi = (DataGridItem) (((Control)
    sender).NamingContainer);
    Line 170: int recordID = int.Parse(dgi.Cells[0].Text);
    Line 171:

    Source File: d:\inetpub\wwwroot\winfundssql\default.aspx.cs Line: 169

    Stack Trace:

    [InvalidCastException: Specified cast is not valid.]
    NetFunds.Values.RowChanged(Object sender, EventArgs e) in
    d:\inetpub\wwwroot\winfundssql\default.aspx.cs:169
    System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +108

    System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePo
    stDataChangedEvent() +26
    System.Web.UI.Page.RaiseChangedEvents() +115
    System.Web.UI.Page.ProcessRequestMain() +1081
     
    Alan Z. Scharf, Jul 8, 2005
    #1
    1. Advertising

  2. Casting DataGridItem - Corrected Posting

    agrids on six different pages which are editable. They each
    draw from a different SQLServer table.

    2. When data is changed in any rows, the Row ID gets added to an array, and
    then a Stored Procedure updates the database table by looping through the
    array of Row ID's.

    I'm using the RowChanged event to add to the array via the function below.

    3. The grids populate OK when the paages are opened, so there is no problem
    with connection..

    4. However, when I edit data and press Save to run the update procedure, I
    get 'Specified cast is not valid error 'on the line that is casting the
    datagruid item.

    5. All editable grids worked for months and then all stopped working at the
    same time.


    Any idea what would cause this problem?

    Thanks.

    Alan


    Function
    ---------------------------------------
    protected void RowChanged(object sender, System.EventArgs e)
    {
    // Add changed record ID's to ChangedRecordList for SaveRecord loop
    --->> Error line: DataGridItem dgi = (DataGridItem) (((Control)
    sender).NamingContainer);
    int recordID = int.Parse(dgi.Cells[0].Text);

    // Add to list if not already there
    if(!ChangedRecordIDList.Contains(recordID))
    {
    ChangedRecordIDList.Add(recordID);
    }
    }

    Stack trace
    ----------------------------------------
    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 167: {
    Line 168: // Add changed record ID's to ChangedRecordList for SaveRecord
    loop
    Line 169: DataGridItem dgi = (DataGridItem) (((Control)
    sender).NamingContainer);
    Line 170: int recordID = int.Parse(dgi.Cells[0].Text);
    Line 171:

    Source File: d:\inetpub\wwwroot\winfundssql\default.aspx.cs Line: 169


    Stack Trace:
    ----------------------------
    [InvalidCastException: Specified cast is not valid.]
    NetFunds.Values.RowChanged(Object sender, EventArgs e) in
    d:\inetpub\wwwroot\winfundssql\default.aspx.cs:169
    System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +108

    System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePo
    stDataChangedEvent() +26
    System.Web.UI.Page.RaiseChangedEvents() +115
    System.Web.UI.Page.ProcessRequestMain() +1081
     
    Alan Z. Scharf, Jul 11, 2005
    #2
    1. Advertising

  3. Re: Casting DataGridItem - Corrected Posting

    Hi Alan

    You can view the type of DataGridItem in Debug mode by adding to Control
    variable to quick watch then change the code to cast the control to the type
    quick watch is showing. I think that might help your cause.

    Regards
    Fiaz Ali Saleemi
    "Alan Z. Scharf" <> wrote in message
    news:...
    > agrids on six different pages which are editable. They each
    > draw from a different SQLServer table.
    >
    > 2. When data is changed in any rows, the Row ID gets added to an array,

    and
    > then a Stored Procedure updates the database table by looping through the
    > array of Row ID's.
    >
    > I'm using the RowChanged event to add to the array via the function below.
    >
    > 3. The grids populate OK when the paages are opened, so there is no

    problem
    > with connection..
    >
    > 4. However, when I edit data and press Save to run the update procedure, I
    > get 'Specified cast is not valid error 'on the line that is casting the
    > datagruid item.
    >
    > 5. All editable grids worked for months and then all stopped working at

    the
    > same time.
    >
    >
    > Any idea what would cause this problem?
    >
    > Thanks.
    >
    > Alan
    >
    >
    > Function
    > ---------------------------------------
    > protected void RowChanged(object sender, System.EventArgs e)
    > {
    > // Add changed record ID's to ChangedRecordList for SaveRecord loop
    > --->> Error line: DataGridItem dgi = (DataGridItem) (((Control)
    > sender).NamingContainer);
    > int recordID = int.Parse(dgi.Cells[0].Text);
    >
    > // Add to list if not already there
    > if(!ChangedRecordIDList.Contains(recordID))
    > {
    > ChangedRecordIDList.Add(recordID);
    > }
    > }
    >
    > Stack trace
    > ----------------------------------------
    > 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 167: {
    > Line 168: // Add changed record ID's to ChangedRecordList for SaveRecord
    > loop
    > Line 169: DataGridItem dgi = (DataGridItem) (((Control)
    > sender).NamingContainer);
    > Line 170: int recordID = int.Parse(dgi.Cells[0].Text);
    > Line 171:
    >
    > Source File: d:\inetpub\wwwroot\winfundssql\default.aspx.cs Line: 169
    >
    >
    > Stack Trace:
    > ----------------------------
    > [InvalidCastException: Specified cast is not valid.]
    > NetFunds.Values.RowChanged(Object sender, EventArgs e) in
    > d:\inetpub\wwwroot\winfundssql\default.aspx.cs:169
    > System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +108
    >
    >

    System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePo
    > stDataChangedEvent() +26
    > System.Web.UI.Page.RaiseChangedEvents() +115
    > System.Web.UI.Page.ProcessRequestMain() +1081
    >
    >
    >
    >
     
    Fiaz Ali Saleemi, Jul 12, 2005
    #3
  4. Re: Casting DataGridItem - Corrected Posting

    ks for your reply.

    I will try this and let you know.

    The strange thing is that it was working perfectly for months on all
    datagrids pulling from different tables!

    Alan





    "Fiaz Ali Saleemi" <> wrote in message
    news:...
    > Hi Alan
    >
    > You can view the type of DataGridItem in Debug mode by adding to Control
    > variable to quick watch then change the code to cast the control to the

    type
    > quick watch is showing. I think that might help your cause.
    >
    > Regards
    > Fiaz Ali Saleemi
    > "Alan Z. Scharf" <> wrote in message
    > news:...
    > > agrids on six different pages which are editable. They each
    > > draw from a different SQLServer table.
    > >
    > > 2. When data is changed in any rows, the Row ID gets added to an array,

    > and
    > > then a Stored Procedure updates the database table by looping through

    the
    > > array of Row ID's.
    > >
    > > I'm using the RowChanged event to add to the array via the function

    below.
    > >
    > > 3. The grids populate OK when the paages are opened, so there is no

    > problem
    > > with connection..
    > >
    > > 4. However, when I edit data and press Save to run the update procedure,

    I
    > > get 'Specified cast is not valid error 'on the line that is casting the
    > > datagruid item.
    > >
    > > 5. All editable grids worked for months and then all stopped working at

    > the
    > > same time.
    > >
    > >
    > > Any idea what would cause this problem?
    > >
    > > Thanks.
    > >
    > > Alan
    > >
    > >
    > > Function
    > > ---------------------------------------
    > > protected void RowChanged(object sender, System.EventArgs e)
    > > {
    > > // Add changed record ID's to ChangedRecordList for SaveRecord loop
    > > --->> Error line: DataGridItem dgi = (DataGridItem) (((Control)
    > > sender).NamingContainer);
    > > int recordID = int.Parse(dgi.Cells[0].Text);
    > >
    > > // Add to list if not already there
    > > if(!ChangedRecordIDList.Contains(recordID))
    > > {
    > > ChangedRecordIDList.Add(recordID);
    > > }
    > > }
    > >
    > > Stack trace
    > > ----------------------------------------
    > > 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 167: {
    > > Line 168: // Add changed record ID's to ChangedRecordList for SaveRecord
    > > loop
    > > Line 169: DataGridItem dgi = (DataGridItem) (((Control)
    > > sender).NamingContainer);
    > > Line 170: int recordID = int.Parse(dgi.Cells[0].Text);
    > > Line 171:
    > >
    > > Source File: d:\inetpub\wwwroot\winfundssql\default.aspx.cs Line: 169
    > >
    > >
    > > Stack Trace:
    > > ----------------------------
    > > [InvalidCastException: Specified cast is not valid.]
    > > NetFunds.Values.RowChanged(Object sender, EventArgs e) in
    > > d:\inetpub\wwwroot\winfundssql\default.aspx.cs:169
    > > System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +108
    > >
    > >

    >

    System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePo
    > > stDataChangedEvent() +26
    > > System.Web.UI.Page.RaiseChangedEvents() +115
    > > System.Web.UI.Page.ProcessRequestMain() +1081
    > >
    > >
    > >
    > >

    >
    >
     
    Alan Z. Scharf, Jul 12, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Perot
    Replies:
    0
    Views:
    386
    Paul Perot
    Dec 12, 2003
  2. Richard Loupatty

    Visibility of selectedindex of a datagriditem

    Richard Loupatty, Jan 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    791
    Richard Loupatty
    Jan 12, 2004
  3. =?Utf-8?B?U291cmkgQ2hhbGxh?=

    Accessing DataGridItem in ItemCreated Event.

    =?Utf-8?B?U291cmkgQ2hhbGxh?=, Aug 28, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,783
    Souri Challa
    Aug 28, 2004
  4. =?Utf-8?B?U2hhd24=?=

    Set DataGridItem dirty

    =?Utf-8?B?U2hhd24=?=, Sep 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    342
    =?Utf-8?B?U2hhd24=?=
    Sep 7, 2004
  5. Samuel Chowdhuri
    Replies:
    1
    Views:
    315
    Alvin Bruney
    Oct 20, 2003
Loading...

Share This Page