Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Error occured Unable to cast object of type 'System.Double' to type '_Default'.

C#
[Serializable]
  public partial class _Default : System.Web.UI.Page
  {
      BinaryFormatter obj1 = new BinaryFormatter();

          public Int64 AccNo;
          public double TranAmt;
          public string custName;
          FileStream fsrw;


      protected void Page_Load(object sender, EventArgs e)
      {

      }
      protected void btnSerilize_Click(object sender, EventArgs e)
      {
          try
          {
              AccNo = Convert.ToInt64(TextBox1.Text);
              custName = TextBox2.Text;
              TranAmt = Convert.ToDouble(TextBox3.Text);
              fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Create, FileAccess.Write);
              obj1.Serialize(fsrw, TranAmt);
              obj1.Serialize(fsrw, AccNo);
              obj1.Serialize(fsrw, custName);
              fsrw.Flush();
              fsrw.Close();

              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('Serilization Succeded');");
              Response.Write("</script>");
              TextBox1.Text = string.Empty;
              TextBox2.Text = string.Empty;
              TextBox3.Text = string.Empty;
          }
          catch (Exception ex)
          {
              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('" + ex.Message + "')");
              Response.Write("</script>");
          }
      }
      protected void btnDeserilize_Click(object sender, EventArgs e)
      {
          try
          {
              _Default obj2 = new _Default();
              fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Open, FileAccess.Read);
              obj2=(_Default)obj1.Deserialize(fsrw);
              fsrw.Flush();
              fsrw.Close();
              TextBox1.Text = obj2.AccNo.ToString();
              TextBox2.Text = obj2.custName.ToString();
              TextBox3.Text = obj2.TranAmt.ToString();
              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('Deserilization Succeded')");
              Response.Write("</script>");
          }
          catch (Exception ex)
          {
              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('" + ex.Message + "')");
              Response.Write("</script>");
          }
      }
}




Error raised when im trying to deserialize and assigning to the object
here is the Code where im getting error

obj2=(_Default)obj1.Deserialize(fsrw);
Posted
Updated 17-Dec-15 1:12am
v6
Comments
Richard MacCutchan 17-Dec-15 7:31am    
The first item you serialized is a double value, so the deserialization will see that first. And a double is not the same as your _Default object.

1 solution

Make a new class, even a child class if you like, that has the three items that you intend to track.

Also, using is your friend; memory leaks are not.
C#
[Serializable]
public class MyTrackedItems
{
   public Int64 AccNo;
   public double TranAmt;
   public string custName;
}


Then change your page:
C#
public partial class _Default : System.Web.UI.Page
{
   private BinaryFormatter obj1 = new BinaryFormatter(); 
   public MyTrackedItems itemInstance = new MyTrackedItems();

   protected void Page_Load(object sender, EventArgs e){}

   protected void btnSerilize_Click(object sender, EventArgs e)
   {
      try
      {
          itemInstance.AccNo= Convert.ToInt64(TextBox1.Text);
          itemInstance.custName = TextBox2.Text;
          itemInstance.TranAmt = Convert.ToDouble(TextBox3.Text);
          using(var fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Create, FileAccess.Write))
          {
             obj1.Serialize(fsrw, itemInstance);
             fsrw.Flush();
             fsrw.Close();
          }
          Response.Write("<script type='text/javascript'>");
          Response.Write("alert('Serilization Succeded');");
          Response.Write("</script>");
          TextBox1.Text = string.Empty;
          TextBox2.Text = string.Empty;
          TextBox3.Text = string.Empty;
       }
       catch (Exception ex)
       {
           Response.Write("<script type='text/javascript'>");
           Response.Write("alert('" + ex.Message + "')");
           Response.Write("</script>");
       }
   }
   protected void btnDeserilize_Click(object sender, EventArgs e)
   {
      try
      {
         MyTrackedItems obj2 = new MyTrackedItems();
         using(var fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Open, FileAccess.Read))
         {
            obj2=(MyTrackedItems)obj1.Deserialize(fsrw);
            fsrw.Flush();
            fsrw.Close();
          }
         TextBox1.Text = obj2.AccNo.ToString();
         TextBox2.Text = obj2.custName.ToString();
         TextBox3.Text = obj2.TranAmt.ToString();
         Response.Write("<script type='text/javascript'>");
         Response.Write("alert('Deserilization Succeded')");
         Response.Write("</script>");
       }
       catch (Exception ex)
       {
          Response.Write("<script type='text/javascript'>");
          Response.Write("alert('" + ex.Message + "')");
          Response.Write("</script>");
       }
   }
}
 
Share this answer
 

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