Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
2
Answers
Problem with database search
No Rubbish
17y
2.4k
1
Reply
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");
}
}
}
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
Creating a table in VB.NET and Adding it to an Access Database
Default and Cancel actions/buttons on a .NET Windows Form