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()


        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");
            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);


        private void btnsearch_Click(object sender, EventArgs e)
            cmd = new SqlCeCommand (" select * from AddressBook where ID like '"+txtsearch+"'");
            dr = cmd.ExecuteReader();
            if (dr.HasRows) {
                while (dr.Read()) {

Re: How to display sql server database table into listbox using C# 80 80

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

Re: How to display sql server database table into listbox using C# 80 80

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

Re: How to display sql server database table into listbox using C# 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.