2
Answers

Problem with database search

No Rubbish

No Rubbish

17y
2.1k
1
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");


}


}

}
Answers (2)