Code error : Cannot implicitly convert type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sappyjosh
    New Member
    • Jul 2010
    • 2

    #1

    Code error : Cannot implicitly convert type

    Code:
    public void Update_Stock(Inseed Is)
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
    
                try
                {
                    string str = "Select CurrStock from StockInMaster where Name = '" + Is.ProductName+ "'";
                    Int64 Units = 0;
    
                    SqlCommand cmd = new SqlCommand(str, con);
                    con.ConnectionString = cnstr;
                    con.Open();
    
                    SqlDataReader dr = cmd.ExecuteReader();
    
                    while (dr.Read())
                    {
                        if (dr["CurrStock"].ToString() == "0")
                        {
                            Units = 0;
                        }
                        else
                        {
                            Units = Convert.ToInt64(dr[0]) - Is.Quantity; // Error is in this line(Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)	
                        }
                    }
    
    
                    string str1 = "Update StockInMaster set CurrStock = " + Units +
                           " where Name = '" + Is.ProductName + "'";
    
    
    
                    SqlCommand cmd1 = new SqlCommand(str1,con);
                    cmd1.ExecuteNonQuery();
                    cmd1.Dispose();
                    con.Close();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }
            }
    Thanks in advance
  • iohos
    Banned
    New Member
    • Jul 2010
    • 45

    #2
    chk d data sequencing step 37- step 40.......

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      What data type is Is.Quantity? Is it a double? If so, I believe when you do a math operation between a double and an int, the result automatically becomes a double.

      As the line suggests, you need a cast on that line to make the result an integer. You can either cast Is.Quantity to an integer, or you can cast the final result to an integer... or in your case, Int64 :)

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by sappyjosh
        Code:
        public void Update_Stock(Inseed Is)
                {
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
        
                    try
                    {
                        string str = "Select CurrStock from StockInMaster where Name = '" + Is.ProductName+ "'";
                        Int64 Units = 0;
        
                        SqlCommand cmd = new SqlCommand(str, con);
                        con.ConnectionString = cnstr;
                        con.Open();
        
                        SqlDataReader dr = cmd.ExecuteReader();
        
                        while (dr.Read())
                        {
                            if (dr["CurrStock"].ToString() == "0")
                            {
                                Units = 0;
                            }
                            else
                            {
                                Units = Convert.ToInt64(dr[0]) - Is.Quantity; // Error is in this line(Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)	
                            }
                        }
        
        
                        string str1 = "Update StockInMaster set CurrStock = " + Units +
                               " where Name = '" + Is.ProductName + "'";
        
        
        
                        SqlCommand cmd1 = new SqlCommand(str1,con);
                        cmd1.ExecuteNonQuery();
                        cmd1.Dispose();
                        con.Close();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message.ToString());
                    }
                }
        Thanks in advance
        Can you post the class Inseed it seems that the Is.Quantity is making the problem

        Comment

        Working...