Hello, l have a table in sql server containing some information such first name,last name, address etc. I want to display the items for a particular row when l search in a textbox. I am running into error messages. Any help here will be appreciated. Here is my code, its in C#.

namespace AddressBook
         {
        public partial class Form1 : Form
           {
            SqlCeCommand cmd;
            SqlCeConnection cn;
            SqlCeDataAdapter da;
            SqlCeDataReader dr;
        public Form1()
        {

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cn = new SqlCeConnection(@"Data Source=C:\Users\Jim\Documents\Visual Studio 2012\Projects\AddressBook\AddressBook\AddBook.sdf");
            cn.Open();
            cmd = new SqlCeCommand("INSERT INTO AddressBook (FirstName, LastName, Telephone, Address, City, Country ) VALUES (@FirstName, @LastName, @Telephone,             @Address, @City, @Country)", cn);

            cmd.Parameters.AddWithValue("@FirstName", txtfirstname.Text);
            cmd.Parameters.AddWithValue("@LastName", txtlastname.Text);
            cmd.Parameters.AddWithValue("@Telephone", txttelephone.Text);
            cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
            cmd.Parameters.AddWithValue("@City", txtcity.Text);
            cmd.Parameters.AddWithValue("@Country", txtcountry.Text);
            cmd.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("Saved");

        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            //cn.Open();
            cmd = new SqlCeCommand (" select * from AddressBook where ID like '"+txtsearch+"'");
            dr = cmd.ExecuteReader();
            if (dr.HasRows) {
                while (dr.Read()) {
                    listBox1.Items.Add(dr[0].ToString());
                }
               }

              }
            }
         }

What are your error messages? Btw. indentation of your code is a bit weird, which makes hard to read.

hi, the error im getting is : ExecuteReader: Connection property has not been initialized.

It is very obvious you have no conection to the DB.
Why is line 35 commented out?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.