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(Obje ct 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.Ce lls[2].Con trols[1]). SelectedIt em.Value);
string deduction = ((DropDownList)e.Item.Cell s[2].Contr ols[1]).Se lectedItem .Value;=== =====highl ighted
SqlConnection objConnection = new SqlConnection("Server=(10. 217.1.85); Database=A DC11;Passw ord=;Persi st 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.A dd("@Emplo yerId", SqlDbType.NVarChar, 20).Value = employerId;
//updateCommand.Parameters .Add("@Des tinationEl ements", SqlDbType.NChar, 50).Value = destinationelements;
updateCommand.Parameters.A dd("@Deduc tion", SqlDbType.Int).Value = deduction;
try
{
objConnection.Open();
updateCommand.ExecuteNonQu ery();
}
catch (SqlException SqlEx)
{
lblStatus.Text = SqlEx.Message.ToString();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message.ToString();
}
finally
{
objConnection.Close();
}
dgCompensation.EditItemInd ex = -1;
BindGrid();
}
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(Obje
{
string employerId = e.Item.Cells[1].Text;
//string destinationelements = ((TextBox)e.Item.Cells[2].
// int deduction = Int32.Parse(((DropDownList
string deduction = ((DropDownList)e.Item.Cell
SqlConnection objConnection = new SqlConnection("Server=(10.
SqlCommand updateCommand = new SqlCommand();
updateCommand.Connection = objConnection;
updateCommand.CommandText = "update DestinationElements set DestinationElement = @DestinationElements, Deduction = @Deduction where EmployerId = @EmployerId";
updateCommand.Parameters.A
//updateCommand.Parameters
updateCommand.Parameters.A
try
{
objConnection.Open();
updateCommand.ExecuteNonQu
}
catch (SqlException SqlEx)
{
lblStatus.Text = SqlEx.Message.ToString();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message.ToString();
}
finally
{
objConnection.Close();
}
dgCompensation.EditItemInd
BindGrid();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it still gave the same error
ASKER
this one works
int deduction = Int32.Parse(((DropDownList )(e.Item.F indControl ("DeductDr opDownList "))).Selec tedItem.Va lue);
int deduction = Int32.Parse(((DropDownList
well, yout declared it as string so I of course thought it was string :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
t_itanium,
> this error because you are trying to cast a type of string to dropdown..
>
> string deduction = ((DropDownList)e.Item.Cell s[2].Contr ols[1]).Se lectedItem .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.
> this error because you are trying to cast a type of string to dropdown..
>
> string deduction = ((DropDownList)e.Item.Cell
No it isn't - the brackets mean that only e.Item.Cells[2].Controls[1
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.
Wint.
ah sorry .i didnot get attentiion to the brackets.... you will need only .ToString()
cheers
cheers
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER