title: "System.InvalidCastException: Specified cast is not valid" - DateTime
body: <p>I am updating certain fields in my website, where I get the following error : </p>
  
  <blockquote>
    <p>System.InvalidCastException: Specified cast is not valid.</p>
  </blockquote>
  
  <p>And the code where the error is this(as shown in the error message) :</p>
  
  <blockquote>
    <p>Line 77:                 productObj.QuantityInStock =
    dt.Rows[0]["QuantityInStock"].ToString();</p>
    
    <p>Line 78:productObj.MinQuantity = dt.Rows[0]["MinQuantity"].ToString();</p>
    
    <p>Line 79:productObj.DateUpdated =(DateTime)dt.Rows[0]["DateUpdated"]; <strong>//This is where error comes</strong> </p>
    
    <p>Line 80:productObj.DateCreated = (DateTime)dt.Rows[0]["DateCreated"]; </p>
    
    <p>Line 81: }</p>
  </blockquote>
  
  <p>My update query is like this : </p>
  
  <pre><code>public int Update(Product obj)
          {
              string query = "update tblProduct set ProdName=@pname,ProdDescription=@pdesc,ProdSize=@psize,ProdPrice=@pprice,QuantityInStock=@qis,MinQuantity=@mq WHERE ProductID=@pid";
  
              List&lt;SqlParameter&gt; lstParams = new List&lt;SqlParameter&gt;();
              lstParams.Add(new SqlParameter("@pid", obj.ProductID));
              lstParams.Add(new SqlParameter("@pname", obj.ProdName));
              lstParams.Add(new SqlParameter("@pprice", obj.ProdPrice));
              lstParams.Add(new SqlParameter("@pdesc", obj.ProdDescription));
              lstParams.Add(new SqlParameter("@psize", obj.ProdSize));
              lstParams.Add(new SqlParameter("@qis", obj.QuantityInStock));
              lstParams.Add(new SqlParameter("@mq", obj.MinQuantity));
  
              return DBUtility.ModifyData(query, lstParams);
          }
  </code></pre>
  
  <p>The <strong><code>Product</code></strong> class looks like this :</p>
  
  <pre><code>public class Product : IModel
      {
          public int ProductID;
          public string ProdName;
          public string ProdDescription;
          public string ProdSize;
          public string ProdPrice;
          public int CompanyID;
          public string ProdPhoto;
          public string QuantityInStock;
          public string MinQuantity;
          public int CategoryID;
          public DateTime DateCreated;
          public DateTime DateUpdated;
      }
  </code></pre>
  
  <p><strong>Error seems to be in this method :</strong></p>
  
  <pre><code>public Product SelectByID(int ID)
          {
              string query = "select * from tblProduct where ProductID=@pid";
              List&lt;SqlParameter&gt; lstParams = new List&lt;SqlParameter&gt;();
              lstParams.Add(new SqlParameter("@pid", ID));
  
              DataTable dt = DBUtility.SelectData(query, lstParams);
  
              Product productObj = new Product();
              if (dt.Rows.Count &gt; 0)
              {
                  productObj.ProdName = dt.Rows[0]["ProdName"].ToString();
                  productObj.ProdDescription = dt.Rows[0]["ProdDescription"].ToString();
                  productObj.CompanyID = Convert.ToInt32(dt.Rows[0]["CompanyID"]);
                  productObj.ProductID = Convert.ToInt32(dt.Rows[0]["ProductID"]);
                  productObj.CategoryID = Convert.ToInt32(dt.Rows[0]["CategoryID"]);
                  productObj.ProdSize = dt.Rows[0]["ProdSize"].ToString();
                  productObj.ProdPrice = dt.Rows[0]["ProdPrice"].ToString();
                  productObj.QuantityInStock = dt.Rows[0]["QuantityInStock"].ToString();
                  productObj.MinQuantity = dt.Rows[0]["MinQuantity"].ToString();
                  productObj.DateUpdated = (DateTime)dt.Rows[0]["DateUpdated"]; //This where error comes
                  productObj.DateCreated = (DateTime)dt.Rows[0]["DateCreated"];
              }
  
              return productObj;
          }
  </code></pre>
  
  <p>In my database too I have mentioned the <em>data-type</em> as <strong><code>DateTime</code></strong>, still why is this error coming ?</p>
  
  <p>My method where I update looks like this : </p>
  
  <pre><code>protected void btnUpdate_Click(object sender, EventArgs e)
          {
              Product prodObj = new ProductLogic().SelectByID(Convert.ToInt32(Request.QueryString["ID"]));
  
              prodObj.ProdName = txtName.Text;
              prodObj.ProdPrice = txtPrice.Text;
              prodObj.ProdDescription = txtDesc.Text;
              prodObj.MinQuantity = txtMinQty.ToString();
              prodObj.QuantityInStock = txtInStock.ToString();
              prodObj.ProductID = Convert.ToInt32(Request.QueryString["ID"]); 
  
              int updateResult = new ProductLogic().Update(prodObj);
  
              if (updateResult &gt; 0)
              {
                  lblUpdateRes.Text = "Detail(s) updated successfully!";
                  lblUpdateRes.ForeColor = System.Drawing.Color.Green;
              }
              else
              {
                  lblUpdateRes.Text = "There was some error while updating your detail(s). Please try again later!";
                  lblUpdateRes.ForeColor = System.Drawing.Color.Red;
              }
  
          }
  </code></pre>
  
  <p>Any ideas/suggestions ?</p>
  
  <p><strong>EDIT : 1</strong> Here is an image of my table :</p>
  
  <p><a href="https://i.stack.imgur.com/dJXyE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dJXyE.png" alt="enter image description here"></a></p>