6
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!!!");
    }

Please help me with my error at line with:

  1. media.Media_MNDP = stForm.MNDP.Text;
  2. media.Media_MNEA = stForm.MNEA.Text;
  3. media.Media_Concurs = stForm.MediaConcurs.Text;

error" Cannot implicitly convert type 'string' to 'decimal'"

Cristina
  • 133
  • 1
  • 2
  • 11
  • Not sure if its simply for demonstration purpose, if not be informed that you should not have classes named like 'form1' and stuff – LuckyLikey May 13 '15 at 09:46

3 Answers3

7

make use of Decimal.TryParse(string, out val)

Example :

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

better to use tryparse method because it avoid runtime exception to be thrown

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

Try like this

 media.Media_MNDP = Decimal.Parse(stForm.MNDP.Text);

For emtpy textbox that will throw error .

To make error free try this

decimal demo;

if(Decimal.TryParse(stForm.MNDP.Text,out demo)){
  media.Media_MNDP = Decimal.Parse(stForm.MNDP.Text);
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
  • It is not necessary to use `Decimal.Parse(text)`, the variable `demo` has already the right value. See [Pranay Rana's answer](http://stackoverflow.com/a/30210991/2029849) – abto May 29 '15 at 11:32
1

Add try-catch when use Parse

try{
media.Media_MNDP = Decimal.Parse(stForm.MNDP.Text);
media.Media_MNEA = Decimal.Parse(stForm.MNEA.Text);
media.Media_Concurs = Decimal.Parse(stForm.MediaConcurs.Text);
}
catch(Exception ex){
}
anhtv13
  • 1,636
  • 4
  • 30
  • 51
  • incorrect. bad usage. use TryParse. At this point in your code, any one of them can throw an Exception...but which one? you wont be able to figure it out during runtime. – Ahmed ilyas May 13 '15 at 09:40
  • Similar maybe but not the same. Tryparse takes away the bloated code you have written. why re-invent the wheel? – Ahmed ilyas May 13 '15 at 09:41
  • The question is "convert type 'string' to 'decimal' ". My solution works – anhtv13 May 13 '15 at 09:43
  • @ANguyen just because a solution works doesn't mean it is a good approach to that solution. – deathismyfriend May 13 '15 at 09:45
  • the first comment is "incorrect". How it is incorrect? It may not be the best but it's not incorrect. – anhtv13 May 13 '15 at 09:47
  • There is nothing wrong with this approach. What if you don't care which Parse failed? You may need to know if a exception happen, not the exception. – George Apr 06 '17 at 15:47