Hey,
Basically I have a text box to search data from my SQL DB from, and I want the information to then display in a listview underneath. I have been trying for a while now and still can't get it to work, but on the button click it should display the data from the textbox in the listview via a selective query.
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
private void searchCoffee_Load(object sender, EventArgs e)
{
myDataSet = new DataSet();
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\UCP\C And ASPNET\Coffee.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
string strSQL = "SELECT * FROM CoffeeTypes";
myDataAdapter = new System.Data.SqlClient.SqlDataAdapter(strSQL, cn);
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dtable = myDataSet.Tables["CoffeeTypes"];
//Display items in the listview ctrl.
foreach (DataRow drow in myDataSet.Tables)
{
DataRow dRow = dtable.Rows[10];
//define the list items
ListViewItem lvi = new ListViewItem(dRow["CoffeeID"].ToString());
lvi.SubItems.Add(dRow["coffeeName"].ToString());
lvi.SubItems.Add(dRow["coffeePrice"].ToString());
lvi.SubItems.Add(dRow["coffeeStrength"].ToString());
lvi.SubItems.Add(dRow["Origin"].ToString());
lvi.SubItems.Add(dRow["QuantityInStock"].ToString());
//add the list items to the listview
listView1.Items.Add(lvi);
}
}