System.Data.Datarow C# error

后端 未结 6 614
傲寒
傲寒 2021-01-19 04:06

So I\'m in the verge of creating my first C# system. I use my vb.net system to use as my reference.

This is the code on my vb.net system:

Dim value A         


        
相关标签:
6条回答
  • 2021-01-19 04:31

    Try this...

    int value = int.Parse(dt.Rows[0]["TransID"]);
    
    0 讨论(0)
  • 2021-01-19 04:39

    You could also try the Field Method on DataRow which would look like this:

    int value = dr.Rows[0].Field<int>("TransID");
    

    http://msdn.microsoft.com/en-us/library/system.data.datarowextensions.field

    0 讨论(0)
  • 2021-01-19 04:42

    The line should read

    int value = double.Parse(dt.Rows[0]["TransID"].ToString());
    

    dt.Rows[0] returns the first row, dt.Rows[0]["TransID"] returns the value of the "TransID" column as object from the first row. As Parse only takes strings, not objects, you need ToString() as well.

    To avoid ToString you could also use the following, which is even better:

    int value = (int)Convert.ToDouble(dt.Rows[0]["TransID"]);
    
    0 讨论(0)
  • 2021-01-19 04:45
    int value = int.Parse(dt.Rows[0]["TransID"]);
    

    or you can also use column index if you know it:

    Assuming that required column is 3rd column in the datatable:

      int value = int.Parse(dt.Rows[0][2]);
    
    0 讨论(0)
  • 2021-01-19 04:48

    Try this:

    int value = double.Parse(dt.Rows[0]["TransID"]);
    
    0 讨论(0)
  • 2021-01-19 04:54
    dt.Rows[0]["TransID"]
    

    This should be working.

    0 讨论(0)
提交回复
热议问题