Solved

Specified cast is not valid.

Posted on 2006-07-05
11
381 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 9 44
PrintingFoundIt(Almost!(Damn!)) 1 30
C# Gridview 1 48
placing a checked in the checklistbox based on the value in database table. 3 20
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 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