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