Link to home
Start Free TrialLog in
Avatar of SirReadAlot
SirReadAlot

asked on

Specified cast is not valid.

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();
   
            }
      
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SirReadAlot
SirReadAlot

ASKER

will try this
it still gave the same error
this one works

   int deduction = Int32.Parse(((DropDownList)(e.Item.FindControl("DeductDropDownList"))).SelectedItem.Value);
well, yout declared it as string so I of course thought it was string :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
ah sorry .i didnot get attentiion to the brackets.... you will need only .ToString()

cheers
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial