Click here to Skip to main content
15,885,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi am getting this error as specified cast is not valid.can any one suggest me how to solve this..
C#
else if ((objDFPropTemp.dfFieldType == "money") || (objDFPropTemp.dfFieldType == "decimal"))
                  {
                      if (objDFPropTemp.dfFieldValue != null)
                      {
                          txt.Text = ((decimal)objDFPropTemp.dfFieldValue).ToString();//this line am getting error.
                      }
                  }
                  else
                      txt.Text = (string)objDFPropTemp.dfFieldValue;
              }

thanks
Posted
Comments
Zoltán Zörgő 6-Jun-12 2:06am    
I have the same (unanswered) question as with you other post: What is the type of objDFPropTemp object and it's dfFieldValue property?
ythisbug 6-Jun-12 2:27am    
DynaControlsPL objDFPropTemp = (DynaControlsPL)gListDynamicControls[i];

and dfFiedlValue is column name from dynamicform..am using dynamic in my class

Don't cast :
C#
txt.Text = objDFPropTemp.dfFieldValue.ToString();
 
Share this answer
 
Comments
VJ Reddy 6-Jun-12 2:12am    
Good answer. 5! for speed.
Mehdi Gholam 6-Jun-12 2:34am    
Thanks VJ!
ythisbug 6-Jun-12 2:14am    
thanks
but if i do as u say i will get error for other line

if ((objDFPropTemp.dfFieldValue != null) && ((int)objDFPropTemp.dfFieldValue > 0))
for this line also same error i will get
VJ Reddy 6-Jun-12 2:28am    
Convert dfFieldValue to int. Please see update to my answer.
Maciej Los 6-Jun-12 18:06pm    
Good answer, my 5!
It looks like, object property objDFPropTemp.dfFieldValue is not a valid decimal datatype value - hence the error.

What you are trying to do is unbox. It will be valid only if the dfFieldValue underlying datatype is decimal. If the value of the field objDFPropTemp.dfFieldValue is a valid decimal then you can use a Convert.ToDecimal method.


As of now, since you want a string equivalent of it to be assigned to a text, just do:
C#
txt.Text = objDFPropTemp.dfFieldValue.ToString();
 
Share this answer
 
Comments
VJ Reddy 6-Jun-12 2:13am    
Good answer and 5! for speed.
Maciej Los 6-Jun-12 18:07pm    
Good answer, my 5!
As seen from the question the objDFPropTemp.dfFieldType is being tested for equality with "money" or "decimal" from which it can be inferred that objDFPropTemp.dfFieldType is of string type.

So the statement (decimal)objDFPropTemp.dfFieldValue will throw error as string cannot be cast to decimal.

Further this block of code will be executed only if (decimal)objDFPropTemp.dfFieldValue is either "money" or "decimal" in which case there is no point in converting this string to decimal.

So I think the following statement may be used
C#
if (objDFPropTemp.dfFieldValue != null)
    txt.Text = objDFPropTemp.dfFieldValue.ToString();

instead of
C#
else if ((objDFPropTemp.dfFieldType == "money") || (objDFPropTemp.dfFieldType == "decimal"))
      {
          if (objDFPropTemp.dfFieldValue != null)
          {
              txt.Text = ((decimal)objDFPropTemp.dfFieldValue).ToString();//this line am getting error.
          }
      }
      else
          txt.Text = (string)objDFPropTemp.dfFieldValue;
  }


[Update]in response to the comment from OP

If objDFPropTemp.dfFieldValue holds a numeric value in string format and it is required to check for its value then it can be converted to numeric value using Int32.TryParse as it will not throw error if the string value is null or not in the correct format, and then can be checked as shown below
C#
int val;
(Int32.TryParse(bjDFPropTemp.dfFieldValue, out val) && val > 0)
 
Share this answer
 
v2
Comments
Sandeep Mewara 6-Jun-12 2:14am    
Yep! 5!
VJ Reddy 6-Jun-12 2:15am    
Thank you, Sandeep :)
ythisbug 6-Jun-12 2:39am    
as u said i tried like this

else if ((objDFPropTemp.dfFieldType == "money") || (objDFPropTemp.dfFieldType == "decimal"))
{
if (objDFPropTemp.dfFieldValue != null)
{
//txt.Text = ((decimal)objDFPropTemp.dfFieldValue).ToString();
txt.Text = objDFPropTemp.dfFieldValue.ToString();
}
}
else
txt.Text = (string)objDFPropTemp.dfFieldValue;
}

//but again in another line am getting error .error is same cast is not valid

if (objDFPropTemp.dfFieldType == "int")
{


// if ((objDFPropTemp.dfFieldValue != null) && ((int)objDFPropTemp.dfFieldValue > 0))
if((objDFPropTemp.dfFieldValue != null) && ((int)objDFPropTemp.dfFieldValue > 0))//this line am getting error now
{
if (objDFPropTemp.dfControlValue.Contains("["))
{
DataSet ds = new DataSet();
string varSubField = string.Empty;
string varField = string.Empty;

varSubField = objDFPropTemp.dfControlValue.Substring(objDFPropTemp.dfControlValue.IndexOf("[") + 1, objDFPropTemp.dfControlValue.IndexOf("]") - objDFPropTemp.dfControlValue.IndexOf("[") - 1);
varField = objDFPropTemp.dfControlValue.Substring(0, objDFPropTemp.dfControlValue.IndexOf("["));
string controlTable = objDFPropTemp.dfControlTable;
int id = (int)objDFPropTemp.dfFieldValue;
SISTextBoxPopupBLL sISTextBoxPopupBLL = new SISTextBoxPopupBLL();
ds = sISTextBoxPopupBLL.GetTextBoxDataByID(id, controlTable);

if (ds != null)
{
if (ds.Tables[0].Rows.Count == 1)
{
string fieldText = ds.Tables[0].Rows[0][varField].ToString();
int fieldValue = Convert.ToInt32(ds.Tables[0].Rows[0][varSubField]);
txt.Text = fieldText.ToString();//((int)(objDFPropTemp.dfFieldValue)).ToString();
txt.Attributes.Add("ID", fieldValue.ToString());
}
}
}
else
{
txt.Text = ((int)(objDFPropTemp.dfFieldValue)).ToString();

}

}
}
else if (objDFPropTemp.dfFieldType == "datetime")
{
if (objDFPropTemp.dfFieldValue != null)
{
txt.Text = ((DateTime)objDFPropTemp.dfFieldValue).ToShortDateString();
}
}
else if (objDFPropTemp.dfFieldType == "bit")
{
if (objDFPropTemp.dfFieldValue != null)
{

txt.Text = ((bool)(objDFPropTemp.dfFieldValue)).ToString();
}
}
else if ((objDFPropTemp.dfFieldType == "money") || (objDFPropTemp.dfFieldType == "decimal"))
{
if (objDFPropTemp.dfFieldValue != null)
{
//txt.Text = ((decimal)objDFPropTemp.dfFieldValue).ToString();
txt.Text = objDFPropTemp.dfFieldV
ythisbug 6-Jun-12 2:44am    
int val;
(Int32.TryParse(bjDFPropTemp.dfFieldValue, out val) && val > 0)

where to declare this..thanks
Mehdi Gholam 6-Jun-12 2:34am    
5'ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900