How can i use ListBox 's SelectedIndexChanged Event

This is a discussion on How can i use ListBox 's SelectedIndexChanged Event within the C# Programming forums, part of the General Programming Boards category; hi guys, i have a problem with the listbox 's event.( i m working on C# ASP.NET ) When the ...

  1. #1
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71

    Question How can i use ListBox 's SelectedIndexChanged Event

    hi guys, i have a problem with the listbox 's event.( i m working on C# ASP.NET )
    When the page loaded the person's name and surname is listed on the list box. What i want is when clicked the lisxtbox's random element , the informations is related to the element will be listed in textboxes.

    my code is below ( to look for the values comes or not, i only wrote txtEvtel.text )
    Code:
    String i;
        String kisi_ad;
        protected void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection con = dbConnection.GetConnectionObject();
            OleDbCommand cmd = new OleDbCommand("SELECT kisi_ad,kisi_soyad FROM kisi where k_id=@kid", con);
            cmd.Parameters.AddWithValue("@kid", Session["k_id"]);
            con.Open();
            OleDbDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                i = (String)(rdr.GetValue(0)+" "+ rdr.GetValue(1));
                kisi_ad = (String)(rdr.GetValue(0));
            }
            lbName.Items.Add(i);
            con.Close();
        }
        protected void lbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            OleDbConnection conn = dbConnection.GetConnectionObject();
            OleDbCommand cm = new OleDbCommand("SELECT * FROM kisi WHERE kisi_ad=@pad", conn);
            cm.Parameters.AddWithValue("@pad", kisi_ad);
            OleDbDataReader rd = cm.ExecuteReader();
            while (rd.Read())
            {
                txtEvTel.Text = rd["kisi_evtel"].ToString();
            }
            conn.Close();
        }
    thanks..

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    So associate something with each element in the list. You can use the tag property for this to add an object (perhaps with a name or ID that you can query SQL with for further information). On your index changed event get this info from the tag and make your query (and fill the resulting textboxes with your data).

  3. #3
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    Code:
    String i;
        String kisi_ad;  // kisi_ad is the name of the variable related to persons name
        protected void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection con = dbConnection.GetConnectionObject();
            OleDbCommand cmd = new OleDbCommand("SELECT kisi_ad,kisi_soyad FROM kisi where k_id=@kid", con);
            cmd.Parameters.AddWithValue("@kid", Session["k_id"]);
            con.Open();
            OleDbDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                i = (String)(rdr.GetValue(0)+" "+ rdr.GetValue(1));
                kisi_ad = (String)(rdr.GetValue(0));  // here i filled the kisi_ad with the name field from the database
            }
            lbName.Items.Add(i);
            con.Close();
        }
        protected void lbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            OleDbConnection conn = dbConnection.GetConnectionObject();
            OleDbCommand cm = new OleDbCommand("SELECT * FROM kisi WHERE kisi_ad=@pad", conn); //i take the query with kisi_ad which is the name column of the person table
            cm.Parameters.AddWithValue("@pad", kisi_ad);
            OleDbDataReader rd = cm.ExecuteReader();
            while (rd.Read())
            {
                txtEvTel.Text = rd["kisi_evtel"].ToString(); //AT THE END I SEND THE kisi_evtel field TO THE TEXTBOX
            }
            conn.Close();
        }
    but it does not work what must i do?

  4. #4
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71
    i solved the problem.
    firstly i forgot to open the db connection in lbName_SelectedIndexChanged, i opened it.
    second the listbox's autopostback was false, i made it true.
    But there is another problem now.When i clicked the element of listbox's textboxes are filled but the element will doubled.How can i fixed it?

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Page_Load is called every time the page is loaded - Initial creation, when item is clicked, etc...
    The simplest solution might be:
    Code:
    protected void PageLoad(...)
    {
      if(!IsPostback)
      {
        ...
      }
    }
    Then the code will only be run at first page load (the item click is considered a postback).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71

    Exclamation

    thanks Magos , it s ok.
    I need more help, my problem is when i clicked the listbox's element always come constant values ( these values are from one record ).I want every element's information comes when i clicked them.

    my code is

    Code:
    //this is the drop down list of groups there are 3 group, when i clicked each of them related people comes to the list box
    protected void ddlGrup_SelectedIndexChanged(object sender, EventArgs e)
        {
            String grup = ddlGrup.SelectedItem.ToString();
            lbName.Items.Clear();
    
            lblAd.Text = "";
            lblSoyad.Text = "";
            lblMeslek.Text = "";
            txtUnvan.Text = "";
            lblEvtel.Text = "";
            lblIstel.Text = "";
            lblCep1.Text = "";
            lblCep2.Text = "";
            lblEvAdres1.Text = "";
            lblEvAdres2.Text = "";
            lblSehir.Text = "";
            lblIsAdres1.Text = "";
            lblIsAdres2.Text = "";
            lblEmail.Text = "";
    
            switch (grup)
            {
                case "Aile":
                    OleDbConnection con = dbConnection.GetConnectionObject();
                    OleDbCommand cmd = new OleDbCommand("select kisi_ad,kisi_soyad from kisi where k_id=@kid and grup=@gname", con);
                    cmd.Parameters.AddWithValue("@kid", Session["k_id"].ToString());
                    cmd.Parameters.AddWithValue("@gname", "Aile");
                    con.Open();
                    OleDbDataAdapter adapterr = new OleDbDataAdapter(cmd);
                    DataTable dat = new DataTable();
                    adapterr.Fill(dat);
                    int rCount;
    
                    for (rCount = 0; rCount <= dat.Rows.Count - 1; rCount++)
                    {
                        lbName.Items.Add(dat.Rows[rCount]["kisi_ad"] + " " + dat.Rows[rCount]["kisi_soyad"]);
                        lbName.Visible = true;
                    }
                    con.Close();
                    break;
         
                case "Arkadaş":
                    OleDbConnection conn = dbConnection.GetConnectionObject();
                    OleDbCommand cmdd = new OleDbCommand("select kisi_ad,kisi_soyad from kisi where k_id=@kid and grup=@gname", conn);
                    cmdd.Parameters.AddWithValue("@kid", Session["k_id"].ToString());
                    cmdd.Parameters.AddWithValue("@gname", "Arkadaş");
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(cmdd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    int RCount;
    
                    for (RCount = 0; RCount<=dt.Rows.Count - 1; RCount++)
                    {
                        lbName.Items.Add(dt.Rows[RCount]["kisi_ad"] + " " + dt.Rows[RCount]["kisi_soyad"]);
                        lbName.Visible = true;
                    }
                    conn.Close();
                    break;
                    
                case "İş Arkadaşı":
                    OleDbConnection connn = dbConnection.GetConnectionObject();
                    OleDbCommand cmddd = new OleDbCommand("select kisi_ad,kisi_soyad from kisi where k_id=@kid and grup=@gname", connn);
                    cmddd.Parameters.AddWithValue("@kid", Session["k_id"].ToString());
                    cmddd.Parameters.AddWithValue("@gname", "İş Arkadaşı");
                    connn.Open();
                    OleDbDataAdapter adapterrr = new OleDbDataAdapter(cmddd);
                    DataTable dta = new DataTable();
                    adapterrr.Fill(dta);
                    int rcount;
    
                    for (rcount = 0; rcount <= dta.Rows.Count - 1; rcount++)
                    {
                        lbName.Items.Add(dta.Rows[rcount]["kisi_ad"] + " " + dta.Rows[rcount]["kisi_soyad"]);
                        lbName.Visible = true;
                    }
                    connn.Close();
                    break;
            }
            
        }
    
    
    //This part is the list box's 
    
    protected void lbName_SelectedIndexChanged1(object sender, EventArgs e)
        {
            getKisiAd();
            OleDbConnection conn = dbConnection.GetConnectionObject();
            OleDbCommand cm = new OleDbCommand("SELECT * FROM kisi WHERE kisi_ad=@pad", conn);
            cm.Parameters.AddWithValue("@pad", kisi_ad);
            conn.Open();
            OleDbDataReader rd = cm.ExecuteReader();
            while (rd.Read())
            {
                lblAd.Text = rd["kisi_ad"].ToString();
                lblSoyad.Text = rd["kisi_soyad"].ToString();
                lblMeslek.Text = rd["kisi_meslek"].ToString();
                lblEvtel.Text = rd["kisi_evtel"].ToString();
                lblIstel.Text = rd["kisi_istel"].ToString();
                lblCep1.Text = rd["kisi_cep1"].ToString();
                lblCep2.Text = rd["kisi_cep2"].ToString();
                lblEvAdres1.Text = rd["kisi_evadres"].ToString();
                lblEvAdres2.Text = rd["kisi_evadres2"].ToString();
                lblSehir.Text = rd["kisi_sehir"].ToString();
                lblIsAdres1.Text = rd["kisi_isadres"].ToString();
                lblIsAdres2.Text = rd["kisi_isadres2"].ToString();
                lblEmail.Text = rd["kisi_email"].ToString();
    
            }
            conn.Close();
        }

  7. #7
    Registered User Aga^^'s Avatar
    Join Date
    Aug 2008
    Posts
    71

    Angry can not take the value

    i can not take the value of a string. I assigned the value in the
    Code:
    kisi_ad = dat.Rows[rCount]["kisi_ad"].ToString();
    but i returns null
    the code is below. i am waiting your answers..

    Code:
    //here i take the group name
    String kisi_ad="";
    
    protected void ddlGrup_SelectedIndexChanged(object sender, EventArgs e)
        {
            cblNames.Items.Clear();
    
            String grup = ddlGrup.SelectedItem.ToString();
    
            switch (grup)
            {
                case "Aile":
                    OleDbConnection con = dbConnection.GetConnectionObject();
                    OleDbCommand cmd = new OleDbCommand("select kisi_ad,kisi_soyad from kisi where k_id=@kid and grup=@gname", con);
                    cmd.Parameters.AddWithValue("@kid", Session["k_id"].ToString());
                    cmd.Parameters.AddWithValue("@gname", "Aile");
                    con.Open();
                    OleDbDataAdapter adapterr = new OleDbDataAdapter(cmd);
                    DataTable dat = new DataTable();
                    adapterr.Fill(dat);
                    int rCount;
    
                    for (rCount = 0; rCount <= dat.Rows.Count - 1; rCount++)
                    {
                        cblNames.Items.Add(dat.Rows[rCount]["kisi_ad"] + " " + dat.Rows[rCount]["kisi_soyad"]);
                        kisi_ad = dat.Rows[rCount]["kisi_ad"].ToString();
                    }
                    con.Close();
                    break;
         
                case "Arkadaş":
                    OleDbConnection conn = dbConnection.GetConnectionObject();
                    OleDbCommand cmdd = new OleDbCommand("select kisi_ad,kisi_soyad from kisi where k_id=@kid and grup=@gname", conn);
                    cmdd.Parameters.AddWithValue("@kid", Session["k_id"].ToString());
                    cmdd.Parameters.AddWithValue("@gname", "Arkadaş");
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(cmdd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    int RCount;
    
                    for (RCount = 0; RCount<=dt.Rows.Count - 1; RCount++)
                    {
                        cblNames.Items.Add(dt.Rows[RCount]["kisi_ad"] + " " + dt.Rows[RCount]["kisi_soyad"]);
    
                        kisi_ad = dt.Rows[RCount]["kisi_ad"].ToString();
                    }
                    conn.Close();
                    break;
                    
                case "İş Arkadaşı":
                    OleDbConnection connn = dbConnection.GetConnectionObject();
                    OleDbCommand cmddd = new OleDbCommand("select kisi_ad,kisi_soyad from kisi where k_id=@kid and grup=@gname", connn);
                    cmddd.Parameters.AddWithValue("@kid", Session["k_id"].ToString());
                    cmddd.Parameters.AddWithValue("@gname", "İş Arkadaşı");
                    connn.Open();
                    OleDbDataAdapter adapterrr = new OleDbDataAdapter(cmddd);
                    DataTable dta = new DataTable();
                    adapterrr.Fill(dta);
                    int rcount;
    
                    for (rcount = 0; rcount <= dta.Rows.Count - 1; rcount++)
                    {
                        cblNames.Items.Add(dta.Rows[rcount]["kisi_ad"] + " " + dta.Rows[rcount]["kisi_soyad"]);
                        kisi_ad = dta.Rows[rcount]["kisi_ad"].ToString();
    
                    }
                    connn.Close();
                    break;
            }    
        }
    
    // and there i assigned the values from db to labels by using checkboxlist
    protected void cblNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(kisi_ad);
            OleDbConnection connect = dbConnection.GetConnectionObject();
            OleDbCommand command = new OleDbCommand("SELECT * FROM kisi WHERE kisi_ad=@pad", connect);
            command.Parameters.AddWithValue("@pad", kisi_ad);
            connect.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);
            DataTable dat = new DataTable();
            adapter.Fill(dat);
            int r;
            for (r = 0; r < dat.Rows.Count - 1; r++)
            {
                lblAd.Text = dat.Rows[r]["kisi_ad"].ToString();
                lblSoyad.Text = dat.Rows[r]["kisi_soyad"].ToString();
                lblMeslek.Text = dat.Rows[r]["kisi_meslek"].ToString();
                lblEvtel.Text = dat.Rows[r]["kisi_evtel"].ToString();
                lblIstel.Text = dat.Rows[r]["kisi_istel"].ToString();
                lblCep1.Text = dat.Rows[r]["kisi_cep1"].ToString();
                lblCep2.Text = dat.Rows[r]["kisi_cep2"].ToString();
                lblEvAdres1.Text = dat.Rows[r]["kisi_evadres"].ToString();
                lblEvAdres2.Text = dat.Rows[r]["kisi_evadres2"].ToString();
                lblSehir.Text = dat.Rows[r]["kisi_sehir"].ToString();
                lblIsAdres1.Text = dat.Rows[r]["kisi_isadres"].ToString();
                lblIsAdres2.Text = dat.Rows[r]["kisi_isadres2"].ToString();
                lblEmail.Text = dat.Rows[r]["kisi_email"].ToString();
            }
            connect.Close();
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in DropDownList's SelectedIndexChanged Event
    By Aga^^ in forum C# Programming
    Replies: 2
    Last Post: 07-17-2009, 08:30 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. How to cast a ListBox item to an int for a switch statment?
    By Swaine777 in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2004, 08:52 PM
  4. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 07:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21