Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString()

后端 未结 4 503
Happy的楠姐
Happy的楠姐 2020-12-11 04:21

I was trying to add data from a database to acombobox.

        try
        {
            SqlCeCommand com = new SqlCeCommand(\"select * from Category_Master\         


        
相关标签:
4条回答
  • 2020-12-11 04:41

    Use Column name in your query then specify that column name into the reader.

    SqlCeCommand com = new SqlCeCommand("select catg from Category_Master", con);
                    SqlCeDataReader dr = com.ExecuteReader();
                    while(dr.Read()){
                        string name = dr("catg").ToString();
                        cmbProductCategory.Items.Add(name);
                    }
    
    0 讨论(0)
  • 2020-12-11 04:46

    Your column doesn't have the type string. Apparently it's int. So use:

    dr.getInt32(1).ToString()
    

    or even

    dr.GetValue(1).ToString()
    

    which should be more roubst to type changes in the database.

    As some sort of general advice I try to follow at least:

    • Select only what you need. This has mostly performance reasons and the reason that you have to state the column names explicitly, thereby getting at least a sensible error if you change your schema incompatibly.
    • Access the fields using their names, e.g.

      dr.GetGuid(dr.GetOrdinal("id"))
      

      Such a thing can also be nicely solved by an extension method:

      public T GetFieldValue<T>(this DbDataReader reader, string columnName)
      {
          return reader.GetFieldValue<T>(reader.GetOrdinal(columnName));
      }
      

    Side note: Including stack traces (or at least saying which line in your code the exception comes from) can be helpful to others trying to help you. As you can see from the wild guesses what the culprit could be. My guess would be that the stack trace looks somewhat like this:

    SqlDataReader.GetString
    YourCode.YourMethod
    

    and that GetString looks more or less like this:

    public string GetString(int index)
    {
        return (string) GetValue(index);
    }
    
    0 讨论(0)
  • 2020-12-11 04:55

    Ok OK. Its solved....

    Here is the code..

            try
            {
                SqlCeCommand com = new SqlCeCommand("select CategoryName from Category_Master", con);
                SqlCeDataReader dr = com.ExecuteReader();
                while(dr.Read()){
                    string name = dr.GetString(0);
                    cmbProductCategory.Items.Add(name);
                }
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    

    I changed the sqlcommand to a single value and changed column number for dr.getstring() to 0.. it worked. Thank you guys for the help.. I expect more because i am only half way in my project..

    0 讨论(0)
  • 2020-12-11 04:59

    Your column doesn't seem to have type int. To avoid things like this, you can use the columnnames instead of indexes.

    try
    {
        SqlCeCommand com = new SqlCeCommand("select * from Category_Master", con);
        SqlCeDataReader dr = com.ExecuteReader();
        while(dr.Read()){
            string name = dr["yourColumnName"].ToString();
            cmbProductCategory.Items.Add(name);
        }
    }
    catch(Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    
    0 讨论(0)
提交回复
热议问题