Solved

Specified cast is not valid.

Posted on 2006-07-05
11
376 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:
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 57
User Authentication using Digital Certificate 2 46
Media.Imaging 1 18
PrintingFoundIt(Almost!(Damn!)) 1 27
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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