private void button18_Click(object sender, EventArgs e)
    {
        Form1 stForm = new Form1();
        DialogResult result = stForm.ShowDialog(this);

        if (result == DialogResult.Cancel)
            return;
        Inscrierea__pentru_burs_sau_contract media = new Inscrierea__pentru_burs_sau_contract();
        media.Media_MNDP = stForm.MNDP.Text;
        media.Media_MNEA = stForm.MNEA.Text;
        media.Media_Concurs = stForm.MediaConcurs.Text;

        db.Media.Add(media);
        db.SaveChanges();
        MessageBox.Show("Salvarea a avut loc cu succes!!!");
    }

请帮我解决以下错误:
  • media.Media_MNDP = stForm.MNDP.Text;
  • media.Media_MNEA = stForm.MNEA.Text;
  • media.Media_Concurs = stForm.MediaConcurs.Text;

  • 错误“无法将类型'string'隐式转换为'decimal'”

    最佳答案

    利用Decimal.TryParse(string, out val)
    例子 :

    decimal val;
    if(Decimal.TryParse(stForm.MNDP.Text, out val))
        media.Media_MNDP = val;
    

    最好使用tryparse方法,因为它避免引发运行时异常

    关于c# - 无法将类型 'string'隐式转换为 'decimal',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30210888/

    10-13 06:53