Unable to pass an object of type "System.Int32" to enter "System.String" in DataReader.GetString ()

I tried to add data from a database to acombobox.

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

I get the following exception:

Unable to pass object of type "System.Int32" to enter "System.String"

What am I missing here?

+6
source share
3 answers

Obviously your column is not of type string . Apparently this is an int . Therefore use:

 dr.getInt32(1).ToString() 

or even

 dr.GetValue(1).ToString() 

which should be larger than roubst to enter changes to the database.

As some general advice, I try to follow at least:

  • Choose only what you need. This is mainly due to performance considerations and the reason that you must explicitly specify column names, thereby getting at least a reasonable error if you are incompatible with your schema.
  • Access fields using their names, for example

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

    Such a thing can also be well solved using the 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 telling which line in the code the exception comes from) may be useful to others who are trying to help you. As you can see from wild guesses, what could be a criminal. I assume the stack trace looks something like this:

 SqlDataReader.GetString YourCode.YourMethod 

and that GetString looks something like this:

 public string GetString(int index) { return (string) GetValue(index); } 
+15
source

There is no int type in your column. To avoid this, you can use column names 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); } 
+1
source

Good. His decision ...

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 sqlcommand to one value and changed the column number for dr.getstring () to 0 .. it worked. Thanks guys for the help. I expect more because I'm only half in my project.

0
source

Source: https://habr.com/ru/post/956694/


All Articles