Hello,
I am still learning C# and have got into a muddle I am trying to put
together a program that will search the database, and display the
results in a grid. I am really struggling with this, was wondering if
anyone can help.
Here is the code. (I get the meesage that UNREACHABLE CODE DETECTED and I have no idea what I have done wrong
This is the search function I am using :
private void btnSearch_Click(object sender, EventArgs e)
{
// This section is the bit that connects to the database and fills the dataAdapter
// m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\database\contacts.mdb";
//m_cnADONetConnection.Open();
if (m_dtContacts.Rows.Count == 0)// this will search the dataTable
and if there are no records, it will display nothing
{
txtContactName.Text = "NO RECORDS FOUND";
txtState.Text = " NO RECORDS FOUND";
return;
{
m_daDataAdapter = new OleDbDataAdapter("Select * from Contacts " +
"where " + comboBox1.Text + " like'%" + txtSearch.Text+ "%'",
m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtContacts);
//once the entry has been found, fill the fields with the data found
txtContactName.Text = m_dtContacts.Rows[m_rowPosition]["ContactName"].ToString();
txtState.Text = m_dtContacts.Rows[m_rowPosition]["State"].ToString();
}
}
}
The rest of the code is here.. including the search function
namespace Database_Application1
{
public partial class Form1 : Form
{
//declare a new Connection
OleDbConnection m_cnADONetConnection = new OleDbConnection();
OleDbDataAdapter m_daDataAdapter; // Declare new DataAdapter
DataTable m_dtContacts = new DataTable(); // This is a new dataTable (It will hold the schools info
int m_rowPosition = 0; // this will determine the position of the current record
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//object Apppath = Application.StartupPath;
//object DatabasePath = Apppath + "\\..\\contacts.mdb;";
// This section is the bit that connects to the database and fills the dataAdapter
m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\database\contacts.mdb";
m_cnADONetConnection.Open();
m_daDataAdapter = new OleDbDataAdapter("Select * From Contacts", m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtContacts);
//Add the data into a DATA GRID
dataGridView1.DataSource = m_dtContacts;
this.ShowCurrentRecord();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
m_cnADONetConnection.Close();
m_cnADONetConnection.Dispose();
}
//Declare a sub that will show the current record.
private void ShowCurrentRecord()
{
if (m_dtContacts.Rows.Count == 0)// this will search the dataTable
and if there are no records, it will display nothing
{
txtContactName.Text = " ";
txtState.Text = " ";
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
DataRow drNewRow = m_dtContacts.NewRow();
drNewRow["ContactName"] = txtNewContactName.Text;
drNewRow["State"] = txtNewState.Text;
m_dtContacts.Rows.Add(drNewRow);
m_daDataAdapter.Update(m_dtContacts);
m_rowPosition = m_dtContacts.Rows.Count - 1;
this.ShowCurrentRecord();
label1.Text = "Data Added";
}
private void button2_Click(object sender, EventArgs e)
{
m_rowPosition = 0;
this.ShowCurrentRecord();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
// This section is the bit that connects to the database and fills the dataAdapter
// m_cnADONetConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\database\contacts.mdb";
//m_cnADONetConnection.Open();
if (m_dtContacts.Rows.Count == 0)// this will search the dataTable
and if there are no records, it will display nothing
{
txtContactName.Text = "NO RECORDS FOUND";
txtState.Text = " NO RECORDS FOUND";
return;
{
m_daDataAdapter = new OleDbDataAdapter("Select * from Contacts " +
"where " + comboBox1.Text + " like'%" + txtSearch.Text+ "%'",
m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtContacts);
//once the entry has been found, fill the fields with the data found
txtContactName.Text = m_dtContacts.Rows[m_rowPosition]["ContactName"].ToString();
txtState.Text = m_dtContacts.Rows[m_rowPosition]["State"].ToString();
}
}
}
private void btnMoveNext_Click(object sender, EventArgs e)
{
if (m_rowPosition < m_dtContacts.Rows.Count - 1)
{
m_rowPosition++;
this.ShowCurrentRecord();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Add("State");
}
}
}
Loading
No RubbishPosted Aug 30, 2007, 10:07 AM
Thanks I have actually got it working now,altrhough I have been unable to successfully clear the DataGrid. When it finds the searched item it inserts it into the grid, all the way at the bottom of the list.
I have decided to abandon using the datagrid and instead use a ListView.. Which is better because it is readonly... But of course nothing is simple.. I am having some probelsm with that now which I was was hoping I could get some help with. I am trying to fill it with DATA from a DataTable but to No Avail. This is what I have in the code./. Its some code I found online and have adapted it slighly.
private void LoadList(){
// Get the table from the data set DataTable dtable = m_dtContacts.Tables["Contacts"]; // Clear the ListView controllistView1.Items.Clear();
// Display items in the ListView control for (int i = 0; i < dtable.Rows.Count; i++){
DataRow drow = dtable.Rows[i]; // Only row that have not been deleted if (drow.RowState != DataRowState.Deleted){
// Define the list items ListViewItem lvi = new ListViewItem(drow["Contact"].ToString());lvi.SubItems.Add(drow[
"ID"].ToString());lvi.SubItems.Add(drow[
"FirstName"].ToString());lvi.SubItems.Add(drow[
"State"].ToString()); // Add the list items to the ListViewlistView1.Items.Add(lvi);
}
}
}
This gives me the error message of
'System.Data.DataTable' does not contain a definition for 'Tables'
Basically what I am hoping to acheive is to be able to display data in the Grid when the program loads, and then to display found data in the ListView when its been found.
Thanks
Jan MontanoPosted Aug 29, 2007, 11:47 PM
It should be...
private void btnSearch_Click(object sender, EventArgs e)
{
}