user19117818
user19117818

Reputation:

C# Cannot implicitly convert type 'string' to 'int'

I get Error when try to set value for (a.Hinta ) to (HintaTextB.Text) in third code block.

    public bool saveAutoIntoDatabase(Auto newAuto)
    {
        bool palaute = false;

        if (connectDatabase())
        {
            string sql = "INSERT INTO [Auto] (Hinta, Rekisteri_paivamaara, Moottorin_tilavuus, Mittarilukema, VaritID)" +
                " VALUES (@Hinta, @Rekisteri_paivamaara, @Moottorin_tilavuus, @Mittarilukema, @VaritID)";

            cmd = new SqlCommand(sql, dbYhteys);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@Hinta",SqlDbType.Decimal).Value = newAuto.Hinta;
            cmd.Parameters.Add("@Rekisteri_paivamaara", SqlDbType.DateTime).Value = newAuto.Rekisteri_paivamaara;
            cmd.Parameters.Add("@Moottorin_tilavuus", SqlDbType.Decimal).Value = newAuto.Moottorin_tilavuus;
            cmd.Parameters.Add("@Mittarilukema", SqlDbType.Int).Value = newAuto.Mittarilukema;

            cmd.Parameters.Add("@VaritID", SqlDbType.Int).Value = newAuto.VaritID;

            
                
                cmd.ExecuteNonQuery();


            disconnectDatabase();
        }
            
           return palaute;

        


    }

// Controller class

    public bool saveAuto(model.Auto newAuto)
    {
        bool didItGoIntoDatabase = dbModel.saveAutoIntoDatabase(newAuto);
        return didItGoIntoDatabase;
    }

I get Error whene try to set value for (a.Hinta ) to (HintaTextB.Text) Error,Cannot implicitly convert type 'string' to 'int'.

    private void saveButton_Click_1(object sender, EventArgs e)
    {

        Auto a = new Auto(0, 0, DateTime.Today, 0, 0, 0, 0, 0, 0);                        
        a.Hinta = HintaTextB.Text;
        a.Mittarilukema = mittarilukemaTextB.Text;            
        a.Rekisteri_paivamaara= dateTimePicker1.Value;
        a.VaritID = variComboBox.SelectedItem;
        bool aa = (logiikka.saveAuto(a));
    }

Upvotes: 1

Views: 92

Answers (1)

Amr Azab
Amr Azab

Reputation: 41

your first two blocks of codes is ok
but in saveButton_Click_1 you call save with the default object values then assign object values to the control
you need first to assign controls values to the object then call save

a.Mittarilukema = mittarilukemaTextB.Text  

then call save

Upvotes: 1

Related Questions