Solved

Specified cast is not valid.

Posted on 2006-07-05
11
350 Views
Last Modified: 2010-04-16
Hi Guys,

I am just trying to update on a grid

but i keep geting the above error

can't really figure this out!!

full code
public void dgCompensation_Update(Object Sender, DataGridCommandEventArgs e)
            {
   
                  string employerId = e.Item.Cells[1].Text;
                  //string  destinationelements = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
//                  int deduction = Int32.Parse(((DropDownList)e.Item.Cells[2].Controls[1]).SelectedItem.Value);

                  string deduction = ((DropDownList)e.Item.Cells[2].Controls[1]).SelectedItem.Value;========highlighted
      
                  SqlConnection objConnection = new SqlConnection("Server=(10.217.1.85);Database=ADC11;Password=;Persist Security Info=True;User ID=sa;");
                  SqlCommand updateCommand = new SqlCommand();

                  updateCommand.Connection = objConnection;
                  updateCommand.CommandText = "update DestinationElements set DestinationElement = @DestinationElements, Deduction = @Deduction where EmployerId = @EmployerId";
                  updateCommand.Parameters.Add("@EmployerId", SqlDbType.NVarChar, 20).Value = employerId;
                  //updateCommand.Parameters.Add("@DestinationElements", SqlDbType.NChar, 50).Value = destinationelements;
                  updateCommand.Parameters.Add("@Deduction", SqlDbType.Int).Value = deduction;
               
                  try
                  {
                        objConnection.Open();
                        updateCommand.ExecuteNonQuery();
                      }
   
                  catch (SqlException SqlEx)
                  {
                        lblStatus.Text = SqlEx.Message.ToString();
                  }
   
                  catch (Exception ex)
                  {
                        lblStatus.Text = ex.Message.ToString();
                  }
   
                  finally
                  {
                        objConnection.Close();
                  }
   
                  dgCompensation.EditItemIndex = -1;
                  BindGrid();
   
            }
      
0
Comment
Question by:SirReadAlot
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 28

Accepted Solution

by:
ciuly earned 50 total points
ID: 17041916
the Value property isn't returning an object? if so, you need to cast to String. something like this:
string deduction = ((String)((DropDownList)e.Item.Cells[2].Controls[1]).SelectedItem.Value).toString();
0
 

Author Comment

by:SirReadAlot
ID: 17041950
will try this
0
 

Author Comment

by:SirReadAlot
ID: 17041967
it still gave the same error
0
 

Author Comment

by:SirReadAlot
ID: 17041989
this one works

   int deduction = Int32.Parse(((DropDownList)(e.Item.FindControl("DeductDropDownList"))).SelectedItem.Value);
0
 
LVL 28

Expert Comment

by:ciuly
ID: 17042029
well, yout declared it as string so I of course thought it was string :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Assisted Solution

by:WinterMuteUK
WinterMuteUK earned 150 total points
ID: 17042182
SirReadAlot,
The error is that this value:
> e.Item.Cells[2].Controls[1]).SelectedItem.Value

isn't a 'DropDownList'.

Please try this:
  DropDownList list = e.Item.Cells[2].Controls[1] as DropDownList;
  if(list == null){
      Console.WriteLine("e.Item.Cells[2].Controls[1] is: " + e.Item.Cells[2].Controls[1].GetType().ToString());
      return;
  }

and see what the actual type of that control is.

Cheers

Wint.
0
 
LVL 5

Assisted Solution

by:t_itanium
t_itanium earned 150 total points
ID: 17042487
this error because you are trying to cast a type of string to dropdown..

string deduction = ((DropDownList)e.Item.Cells[2].Controls[1]).SelectedItem.Value;

this should be

string deduction = (e.Item.Cells[2].Controls[1]).SelectedItem.Value.ToString();

using .ToString() you canneglect the casting because it returns string..

cheers
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17042950
t_itanium,

> this error because you are trying to cast a type of string to dropdown..
>
> string deduction = ((DropDownList)e.Item.Cells[2].Controls[1]).SelectedItem.Value;
No it isn't - the brackets mean that only e.Item.Cells[2].Controls[1] is being cast to a DropDownList, not the 'Value'. Depending on the type of 'Value' you may need to do the 'ToString()' though.

Wint.
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17043036
Assuming this is the web DropDownList then (http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.value.aspx) value is of string type anyhow, so no need for 'ToString()'.

Wint.
0
 
LVL 5

Expert Comment

by:t_itanium
ID: 17048348
ah sorry .i didnot get attentiion to the brackets.... you will need only .ToString()

cheers
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 150 total points
ID: 17050622
Sometimes, to make it easier to accomplish, and simpler to read, you can do it in steps:

DropDownList ddl = (DropDownList)e.Item.FindControl("DeductDropDownList");
int deduction = Int32.Parse(ddl.SelectedItem.Value);

Bob

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

705 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

15 Experts available now in Live!

Get 1:1 Help Now