none
How to convert string type to int? RRS feed

  • Question

  •  

    I got an error while making dropdown list in mvc 

    Conversion of string into int. How to convert?

    Code:

    ord.OrderId = rdr["OrderId"].ToString();

    Error:

    Cannot implicity convert type 'string' to 'int'.



    • Edited by Tanzeel23 Sunday, April 14, 2019 11:23 AM
    Sunday, April 14, 2019 11:23 AM

Answers

  • You can use Convert.ToInt32:

    ord.OrderId = Convert.ToInt32(rdr["OrderId"]);

    However, be aware that an error will occur at runtime if the value cannot be converted to an integer. Apart from the obvious things that cannot be converted, such as "abcd", be aware that a null or blank value or DBNull cannot be converted into an int (they will not convert to a zero; they will throw an exception).

    If you are sure that your column always contains data of type integer (and not just something that can be converted to an integer), then you can use a cast like this:

    ord.OrderId = (int)rdr["OrderId"];


    Sunday, April 14, 2019 12:47 PM
    Moderator
  • Here are three different ways to get the value safely.

    public class DataOperations
    {
        private string ConnectionString;
    
        public DataOperations()
        {
            ConnectionString = "TODO";
        }
        public Item ReadItem(int pIdentifier)
        {
            var resultItem = new Item();
            var selectStatement = 
                "SELECT OrderId,OrderDate FROM YourTable";
    
            using (var cn = new SqlConnection {ConnectionString = ConnectionString})
            {
                using (var cmd = new SqlCommand {Connection = cn})
                {
                    cmd.CommandText = selectStatement;
    
                    cn.Open();
    
                    var reader = cmd.ExecuteReader();
    
                    if (reader.HasRows)
                    {
                        reader.Read();
                        //
                        // Option 1 is OrderId is not an int in the table
                        //
                        if (int.TryParse(reader["OrderId"].ToString(),out _))
                        {
                            resultItem.OrderId = reader.GetInt32(0);
                        }
    
                        // option 2 test and if the value is an int use it
                        if (int.TryParse(reader["OrderId"].ToString(), out var orderId))
                        {
                            resultItem.OrderId = orderId;
                        }
    
                        // Option 3 if it is an int simple use GetInt32
                        resultItem.OrderId = reader.GetInt32(0);
    
                    }
                }
            }
    
            return resultItem;
        }
    
    }
    
    public class Item
    {
        public int OrderId { get; set; }
        public DateTime OrderDate { get; set; }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Tanzeel23 Sunday, April 14, 2019 4:27 PM
    Sunday, April 14, 2019 1:06 PM
    Moderator

All replies

  • You can use Convert.ToInt32:

    ord.OrderId = Convert.ToInt32(rdr["OrderId"]);

    However, be aware that an error will occur at runtime if the value cannot be converted to an integer. Apart from the obvious things that cannot be converted, such as "abcd", be aware that a null or blank value or DBNull cannot be converted into an int (they will not convert to a zero; they will throw an exception).

    If you are sure that your column always contains data of type integer (and not just something that can be converted to an integer), then you can use a cast like this:

    ord.OrderId = (int)rdr["OrderId"];


    Sunday, April 14, 2019 12:47 PM
    Moderator
  • Here are three different ways to get the value safely.

    public class DataOperations
    {
        private string ConnectionString;
    
        public DataOperations()
        {
            ConnectionString = "TODO";
        }
        public Item ReadItem(int pIdentifier)
        {
            var resultItem = new Item();
            var selectStatement = 
                "SELECT OrderId,OrderDate FROM YourTable";
    
            using (var cn = new SqlConnection {ConnectionString = ConnectionString})
            {
                using (var cmd = new SqlCommand {Connection = cn})
                {
                    cmd.CommandText = selectStatement;
    
                    cn.Open();
    
                    var reader = cmd.ExecuteReader();
    
                    if (reader.HasRows)
                    {
                        reader.Read();
                        //
                        // Option 1 is OrderId is not an int in the table
                        //
                        if (int.TryParse(reader["OrderId"].ToString(),out _))
                        {
                            resultItem.OrderId = reader.GetInt32(0);
                        }
    
                        // option 2 test and if the value is an int use it
                        if (int.TryParse(reader["OrderId"].ToString(), out var orderId))
                        {
                            resultItem.OrderId = orderId;
                        }
    
                        // Option 3 if it is an int simple use GetInt32
                        resultItem.OrderId = reader.GetInt32(0);
    
                    }
                }
            }
    
            return resultItem;
        }
    
    }
    
    public class Item
    {
        public int OrderId { get; set; }
        public DateTime OrderDate { get; set; }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Tanzeel23 Sunday, April 14, 2019 4:27 PM
    Sunday, April 14, 2019 1:06 PM
    Moderator