1

I'm new to WPF. I get the following error: "The best overloaded message match for System.Data.Common.DbDataReader.GetInt32 has some invalid arguments"

My code is as follows:

private void comboBoxDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string constring = "Data Source=tcp:******.database.windows.net;Initial        Catalog=*****;Persist Security Info=True;User ID=*****;Password=******";

        string Query = "select * from Rewards where Name='" + comboBoxDisplay.Text + "' ;";
        SqlConnection conDataBase = new SqlConnection(constring);
        SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
        SqlDataReader myReader;

        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {

                 string sReid = myReader.GetInt32(0).ToString();
                string sName = myReader.GetString(1);



            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }`
user2631662
  • 855
  • 5
  • 12
  • 21

2 Answers2

1

GetXXX methods use the column number, not the name. You also shouldn't need to cast if you're calling the right method. Try this

while (myReader.Read())
        {

            string sReid = myReader.GetString(myReader.GetOrdinal("reID"));
            string sName = myReader.GetString(myReader.GetOrdinal("Name"));


        }
steveg89
  • 1,807
  • 2
  • 15
  • 29
  • Does not recognise GetOrdinal – user2631662 Jul 30 '13 at 09:28
  • I've modified my code to fix that. Sorry about the bug. Just a note: You should probably be wrapping this all in try/catch in case of errors (wrong column names, bad database format, etc). – steveg89 Jul 30 '13 at 11:12
  • Thanks! Do you know why I get the values of the previously selected item from the combo box being displayed in textbox instead of the actual selected item? – user2631662 Jul 30 '13 at 11:42
  • That's a common problem for people. See http://stackoverflow.com/questions/2961118/wpf-combobox-selectionchanged-event-has-old-value-not-new for an explanation of what's going on. Basically, the event isn't telling you what you think it is. – steveg89 Jul 30 '13 at 11:47
0

The System.Data.Common.DbDataReader.GetInt32 method is expecting an integer which is the ordinal of the column. There are no overloaded methods that take a string as a parameter.

myReader.GetInt32(2);  // gets the 3rd column  (zero based)
Mark Erickson
  • 767
  • 1
  • 7
  • 27