1

I have a button code that passes data from a connectionString to a listBoxDat:

private void btnClick_Click(object sender, RoutedEventArgs e)
    {
        tbTitle.Text = "ADO.Net";
        listBoxData.Background = Brushes.LemonChiffon;

        string cs = ConfigurationManager.ConnectionStrings["crams"].ConnectionString;
        List<string> titles = new List<string>();

        using (SqlConnection conn = new SqlConnection(cs))
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "Select filenum FROM dbo.Complaint";
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                titles.Add(reader.GetString());
            }
            listBoxData.ItemsSource = titles;
        }


    }

My error list keeps saying that there is No overload for method 'Get sTring' takes 0 arguments. I am not sure what this means!

EPV
  • 61
  • 1
  • 9

2 Answers2

1

SqlDataReader.GetString methods expects an int parameter for index, specify 0 in your case sine your are only selecting a single field.

titles.Add(reader.GetString(0));
Habib
  • 219,104
  • 29
  • 407
  • 436
0

The line of code:

reader.GetString()

must supply the index of the column for which you want to retrieve data from.

reader.GetString(0)

For column 0.

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16