List Area method

mustish1
mustish1 used Ask the Experts™
on
Can you please tell me why this routine is not showing values in the text box when i click in the text area?
screenshot.JPG
      private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox i = sender as ListBox;
            if (i.SelectedIndex != -1)
            {
                listBox1.SelectedIndex = i.SelectedIndex;
                listBox2.SelectedIndex = i.SelectedIndex;
                txtID.Text = listBox1.SelectedItem.ToString();
                txtName.Text = listBox2.SelectedItem.ToString();               
            }
        
        }

Open in new window

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 InsertUpdateDel
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Lenovo\Documents\Visual Studio 2012\Projects\InsertUpdateDel\InsertUpdateDel\Database1.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader sd;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
                cmd.Connection = cn;
                loadlist();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtID.Text != "" & txtName.Text != "")
            {
                cn.Open();
                cmd.CommandText = "Insert into info(id, name) values ('" +txtID.Text+"', '" +txtName.Text+"')";
                cmd.ExecuteNonQuery();
                cmd.Clone();
                MessageBox.Show("Record Inserted", "Programming");
                cn.Close();
                txtID.Text="";
                txtName.Text="";
                loadlist();
            }
        }
        private void loadlist()
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            cn.Open();
            cmd.CommandText = "select * from info";
            sd = cmd.ExecuteReader();
            if (sd.HasRows)
            {
                while (sd.Read())
                {
                    listBox1.Items.Add(sd[0].ToString());
                    listBox2.Items.Add(sd[1].ToString());
                }
        }
        cn.Close();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox i = sender as ListBox;
            if (i.SelectedIndex != -1)
            {
                listBox1.SelectedIndex = i.SelectedIndex;
                listBox2.SelectedIndex = i.SelectedIndex;
                txtID.Text = listBox1.SelectedItem.ToString();
                txtName.Text = listBox2.SelectedItem.ToString();               
            }
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtID.Text != "" & txtName.Text != "")
            {
                cn.Open();
                cmd.CommandText = "delete from info where id='" + txtID.Text + "' and name='" + txtName.Text + "'";
                cmd.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show("Record Deleted", "Prg");
                loadlist();
                txtID.Text = "";
                txtName.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtID.Text != "" & txtName.Text != "" & listBox1.SelectedIndex !=-1)
            {
                cn.Open();
                cmd.CommandText = "update info set id='" +txtID.Text+"', name='"+txtName.Text+"' where id='"+listBox1.SelectedItem.ToString()+"' and name='"+listBox2.SelectedItem.ToString()+"'";
                cmd.ExecuteNonQuery();
                cn.Close();
                MessageBox.Show("Record Updated", "Prg");
                loadlist();
                txtID.Text = "";
                txtName.Text = "";
            }

        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I'm assuming that by "click on the text area" you meant change the selection of one of the listboxes.

You could try using GetItemText() like this:
txtID.Text = listBox1.GetItemText(listbox1.SelectedItem);

However, you seem to be trying to be using synchronized list boxes to simulate a data table. In other words, you're reinventing the wheel

You should look at using the DataGridView control with a DataTable filled by your query. It will likely be what you are trying to do with your application, and it even supports different row states and committing so you can build a simple CRUD application with very little code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial