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);
        }`

2 Answers 2

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"));


        }
4
  • Does not recognise GetOrdinal Jul 30, 2013 at 9: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, 2013 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? Jul 30, 2013 at 11:42
  • That's a common problem for people. See stackoverflow.com/questions/2961118/… for an explanation of what's going on. Basically, the event isn't telling you what you think it is.
    – steveg89
    Jul 30, 2013 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)
1
  • This got rid of the error but when I select from the combo box the values do not display in textboxes. Jul 30, 2013 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.