locked
Convert RRS feed

  • Question

  • User1608082067 posted
    Hello,

    I am getting a dataset from using an SQL Stored procedure.

    I am looping through each row and get the values to populate a class properties:

    ...
    MyClass.Value = Convert.ToInt32(dr("Value"))
    MyClass.Id = New Guid(Convert.ToString(dr("Id")))
    MyClass.Body = Convert.ToString(dr("Body"))
    ...
    

    My question is: do I need to use Convert or can I just do something like:

    ...
    MyClass.Value = dr("Value")
    MyClass.Id = dr("Id"))
    MyClass.Body = dr("Body")
    ...

    Thanks,

    Miguel 

    Wednesday, September 26, 2007 9:04 PM

Answers

  • User-1310972242 posted

    Values coming from a database are always passed as objects.  Yes, the Convert is necessary. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2007 10:37 PM
  • User844797000 posted

     Hi,

    converting is necessary. But you can use other methods of datareader like, to get the int32 value you can use dr.getInt32(0), dr.GetString(1) for string, etc.... 

    but the value must exist in the db.

    some if the methods are,

    dr.GetInt32(), dr.GetString(), dr.GetBoolean(), etc.... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 12:25 AM
  • User-221752185 posted

    No you dont have to use Convert, you can do it this way

      

    this._strValue = dr["Value"].ToString();
    this._intId = int.Parse(dr["Id"].ToString());
    this._strBody = dr["Body"].ToString();
    
    if your id is a guid then you can do this
    
    this._gId = new Guid(dr["id"].ToString());
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 12:40 AM
  • User-1509636757 posted

    Convert is mandatory as dr [i think its datareader] would return type as Object...

    which should be converted to appropriate type...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 12:57 AM
  • User396115320 posted

    So in my opinion Convert should be used since in a record some columns can be empty, right?

     

    If you have empty columns well, whats the result of Convert.ToString("") ?

    You could instead use nullable dataTypes:

    Int32? yourNullableInt = (Int32?)readrow["yourInt"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2007 8:20 AM
  • User-221752185 posted

    Quick note:

     comming to the conclusion of using "Convert" because of nullable types is incorrect, ToString will return an Empty String on a nullable type.

     What you can do is

     

      

    public class MyClass
    {
       private int _intID;
       private string _strValue;
       private string _strTitle;
    
       internal MyClass(SqlDataReader sd)
       {
          this._intID = (sd["ID"] == DBNull.Value)? 0 : int.Parse(sd["ID"].ToString());
          this._strValue = sd["Value"].ToString();
          this._strTitle = sd["Title"].ToString();
       }
    }
     

     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2007 10:09 AM

All replies

  • User-1310972242 posted

    Values coming from a database are always passed as objects.  Yes, the Convert is necessary. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2007 10:37 PM
  • User844797000 posted

     Hi,

    converting is necessary. But you can use other methods of datareader like, to get the int32 value you can use dr.getInt32(0), dr.GetString(1) for string, etc.... 

    but the value must exist in the db.

    some if the methods are,

    dr.GetInt32(), dr.GetString(), dr.GetBoolean(), etc.... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 12:25 AM
  • User-221752185 posted

    No you dont have to use Convert, you can do it this way

      

    this._strValue = dr["Value"].ToString();
    this._intId = int.Parse(dr["Id"].ToString());
    this._strBody = dr["Body"].ToString();
    
    if your id is a guid then you can do this
    
    this._gId = new Guid(dr["id"].ToString());
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 12:40 AM
  • User-1509636757 posted

    Convert is mandatory as dr [i think its datareader] would return type as Object...

    which should be converted to appropriate type...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2007 12:57 AM
  • User-221752185 posted

    Convert is mandatory as dr [i think its datareader] would return type as Object...

    which should be converted to appropriate type...

     

    Can we please verify, that by saying you must use "Convert" implies that you must use the .NET out of the box Convert methods, such as Convert.ToDateTime, etc..

    The answer to this question is NO you do not. You can "convert" the data types by using other other methods such as ToString() and int.Parse as I showed above. 

    Thursday, September 27, 2007 1:07 AM
  • User-1509636757 posted

    Can we please verify, that by saying you must use "Convert" implies that you must use the .NET out of the box Convert methods, such as Convert.ToDateTime, etc..

    The answer to this question is NO you do not. You can "convert" the data types by using other other methods such as ToString() and int.Parse as I showed above. 

     

    hi friend... thanx for Quick resonse...

    can you refer the original post.. which says 

    My question is: do I need to use Convert or can I just do something like: 


    and what i post is...

     

    Convert is mandatory as dr [i think its datareader] would return type as Object... 

    which should be converted to appropriate type...

     

    so what i wanted to say is..

    conversion is Must and Mandatory....

    [whether you use the .NET out of the box Convert methods, such as Convert.ToDateTime, etc..]

    OR

    ["convert" the data types by using other other methods such as ToString() and int.]

    that doesn't matter...

     

    [by the way... Convert.ToString() method can handle null values while .ToString() conversion method cannot... you can refer this Example]

    Thanx./. 

     

    hope it clears./. 

    Thursday, September 27, 2007 1:21 AM
  • User1608082067 posted

    Hi,

    So in my opinion Convert should be used since in a record some columns can be empty, right?

    Thanks,

    Miguel
     

    Friday, September 28, 2007 7:50 AM
  • User396115320 posted

    So in my opinion Convert should be used since in a record some columns can be empty, right?

     

    If you have empty columns well, whats the result of Convert.ToString("") ?

    You could instead use nullable dataTypes:

    Int32? yourNullableInt = (Int32?)readrow["yourInt"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2007 8:20 AM
  • User1608082067 posted

    Hi,

    So I should define my class properties as for example:

    Public Property Id() As System.Nullable(Of Guid) 
    Public Property Value() As System.Nullable(Of Int32)
    Public Property Title() As System.Nullable(Of String 

    And assign values as:

    MyClass.Id = New Guid(DirectCast(dr("MyGuid"), System.Nullable(Of Guid)))
    MyClass.Value = DirectCast(dr("MyInt"), System.Nullable(Of Int32)))
    MyClass.Title = DirectCast(dr("MyString"), System.Nullable(Of String)))

    And will not I have problems when trying to create a Guid from a db value where it is null?

    Thanks,

    Miguel 

     

    Friday, September 28, 2007 8:56 AM
  • User-221752185 posted

    Quick note:

     comming to the conclusion of using "Convert" because of nullable types is incorrect, ToString will return an Empty String on a nullable type.

     What you can do is

     

      

    public class MyClass
    {
       private int _intID;
       private string _strValue;
       private string _strTitle;
    
       internal MyClass(SqlDataReader sd)
       {
          this._intID = (sd["ID"] == DBNull.Value)? 0 : int.Parse(sd["ID"].ToString());
          this._strValue = sd["Value"].ToString();
          this._strTitle = sd["Title"].ToString();
       }
    }
     

     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 28, 2007 10:09 AM