?
Solved

Specified cast is not valid.

Posted on 2006-07-05
11
Medium Priority
?
400 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 600 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 600 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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

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