Im trying to add a search box onto my winform. Im using SqlCE.
Im trying to get the sqlceDataAdapter to take a string variable, but when I press the button to search it tells my that the stored procedure is not valid, WHEN IT IS. Here is the code:
private void button1_Click(object sender, EventArgs e)
{
string search = "\"SELECT * FROM people\"";
con = new SqlCeConnection("Data Source = DBtest.sdf;Persist Security Info=False");
con.Open();
MessageBox.Show(con.State.ToString());
SqlCeDataAdapter a = new SqlCeDataAdapter(search, con);
DataTable t = new DataTable(); a.Fill(t); dataGridView1.DataSource = t;
}
}
Ideally I wanted to add a textbox where the user can type in a last name and it will search my database and display it.. My db is a SDF file..
Any help will be appreciated.
Loading
Rakesh JhaPosted Oct 5, 2010, 9:34 PM
use below code, it will help you.
string search = "SELECT * FROM people where LastName like @LName+'%'";
con = new SqlCeConnection("Data Source = DBtest.sdf;Persist Security Info=False");
con.Open();
SqlCeDataAdapter a = new SqlCeDataAdapter(search, con);
a.SelectCommand.Parameters.Add("@LName", SqlDbType.NVarChar, 100).Value = textBox1.Text;
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
con.Close();
a.Dispose();
t.Dispose();
hope it will help you.
GuyPosted Oct 5, 2010, 9:55 PM
You are the man! Thanks for the reply. I will try it now. Thanks again brother!