Solved

Specified cast is not valid.

Posted on 2006-07-05
11
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 28

Accepted Solution

by:
2266180 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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:2266180
ID: 17042029
well, yout declared it as string so I of course thought it was string :)
0
 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

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