none
LINQ to DataSet-pub (Un-Typed) in VC# 2008 Express: ListView.SelectedIndexChanged does not exist in the current content? RRS feed

  • Question

  • Hi all,

    In my VIsual C# 2008 Express, I executed the following code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace scWinFrmLinqToDatasetUnTypedAnneLee439
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            DataSet pubsDataSet = new DataSet();
            SqlDataAdapter authorsDataAdapter = new SqlDataAdapter();
            DataTable authorsTable;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.CreateDataProviderObjects();
                this.FillAuthorsTable();
                authorsTable = pubsDataSet.Tables["Authors"];
                this.LoadAuthorIdComboBox();
                this.LoadAuthorLastNameComboBox();
                this.LoadAuthorFirstNameComboBox();
                this.DisplayAuthor();
            }
    
            private void CreateDataProviderObjects()
            {
                string connectionString =
                    "Data Source=NAB-WK-02554356\\SQLSCOTT;Initial Catalog=pubs;" +
                    "Integrated Security=True";
                SqlConnection pubsConnection =
                    new SqlConnection(connectionString);
    
                SqlCommand authorsCommand = new SqlCommand();
                authorsCommand.Connection = pubsConnection;
                string authorsSelect =
                    "SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract " +  
                    "FROM authors";
                authorsCommand.CommandText = authorsSelect;
                authorsDataAdapter.SelectCommand = authorsCommand;
            }
    
            private void FillAuthorsTable()
            {
                try
                {
                    authorsDataAdapter.Fill(pubsDataSet, "Authors");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Database error # " + ex.Number +
                        ": " + ex.Message, ex.GetType().ToString());
                }
            }
    
            private void LoadAuthorIdComboBox()
            {
                cboAuthorID.SelectedIndexChanged -= cboAuthorID_SelectedIndexChanged;
    
                var selectedAuthorIDs = (from author in authorsTable.AsEnumerable()
                                         orderby author.Field<String>("au_id")
                                         // where author.Field<String>("au_id") == "CA"
                                         select author.Field<String>("au_id")).Distinct();
    
                cboAuthorID.Items.Clear();
                foreach (var authorID in selectedAuthorIDs)
                {
                    cboAuthorID.Items.Add(authorID);
                }
                cboAuthorID.SelectedIndex = 0;
    
                cboAuthorID.SelectedIndexChanged += cboAuthorID_SelectedIndexChanged;
            }
    
            private void LoadAuthorLastNameComboBox()
            {
     
            }
    
            private void LoadAuthorFirstNameComboBox()
            { 
            
            }
    
            private void DisplayAuthor()
            { 
            
            }
    
    
        }
    }
    I got 2 errors:
    Error 1 The name 'cboAuthorID_SelectedIndexChanged' does not exist in the current context C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\scWinFrmLinqToDatasetUnTypedAnneLee439\scWinFrmLinqToDatasetUnTypedAnneLee439\Form1.cs 67 49 scWinFrmLinqToDatasetUnTypedAnneLee439
    Error 2 The name 'cboAuthorID_SelectedIndexChanged' does not exist in the current context C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\scWinFrmLinqToDatasetUnTypedAnneLee439\scWinFrmLinqToDatasetUnTypedAnneLee439\Form1.cs 81 49 scWinFrmLinqToDatasetUnTypedAnneLee439

    Please help and advise me how to solve them.

    Thanks,
    Scott Chang
    Thursday, November 5, 2009 8:59 PM

Answers

  • Open form in designer and select combobox on that form. On the right side of the Visual Studio Designer you should see properties of the combobox control and icon for the events. Click on that icon and find SelectIndexChange event. Just double click on it and event handler will be created. Then copy your existing code into event handler


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Scott_Chang Tuesday, November 10, 2009 12:32 PM
    Tuesday, November 10, 2009 11:07 AM
    Moderator

All replies

  • Based on code you posted I do not see any declaration for the 'cboAuthorID_SelectedIndexChanged'  method. That's why it complains. You need to declare method that will work as event handler for SelectIndexChanged
    Val Mazur (MVP) http://www.xporttools.net
    Friday, November 6, 2009 11:28 AM
    Moderator
  • Hi Val Mazur, Thanks for your response.

    1) How can I declare the the 'cboAuthorID_SelectedIndexChanged'  method that will work as event handler for SelectIndexChanged? 

    2) I tried to mimic someone's project that uses the SelectedIndexChanged method for a ComboBox that is in the Form1.cs(Design). Do you think I have to set up something in the Properties of the ComboBox to link the SelectedIndexChanged method?

    Please help, advise and respond.

    Thanks,
    Scott Chang   
    Monday, November 9, 2009 1:18 PM
  • Open form in designer and select combobox on that form. On the right side of the Visual Studio Designer you should see properties of the combobox control and icon for the events. Click on that icon and find SelectIndexChange event. Just double click on it and event handler will be created. Then copy your existing code into event handler


    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Scott_Chang Tuesday, November 10, 2009 12:32 PM
    Tuesday, November 10, 2009 11:07 AM
    Moderator