TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Auto Search Data From Database in C# .Net
Pintoo Yadav
Jan 07
2015
Code
3.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
AutoSearch.rar
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.OleDb;
namespace
ClientManagementApp
{
public
partial
class
SearchClient : Form
{
DataSet dstResults =
new
DataSet();
DataView myView;
public
SearchClient()
{
InitializeComponent();
}
public
static
OleDbConnection conection()
{
OleDbConnection con =
new
OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb.accdb;"
);
if
(con.State == ConnectionState.Closed)
con.Open();
return
con;
}
private
void
txtSearch_KeyUp(
object
sender, KeyEventArgs e)
{
string
outputInfo =
""
;
string
[] keyWords = txtSearch.Text.Split(
' '
);
foreach
(
string
word
in
keyWords)
{
if
(outputInfo.Length == 0)
{
outputInfo =
"(BankName LIKE '%"
+ word +
"%' OR ContactPersonName LIKE '%"
+
word +
"%' OR ContactNumber LIKE '%"
+ word +
"%')"
;
}
else
{
outputInfo +=
" AND (BankName LIKE '%"
+ word +
"%' OR ContactPersonName LIKE '%"
+
word +
"%' OR ContactNumber LIKE '%"
+ word +
"%')"
;
}
}
myView.RowFilter = outputInfo;
}
private
void
SearchClient_Load(
object
sender, EventArgs e)
{
////OleDbConnection con = conection();
////OleDbCommand cmd;
//////DataSet ds=new DataSet();
////DataTable ds = new DataTable();
////string str = "select * from tbl_ClientDetails";
////cmd = new OleDbCommand(str, con);
////OleDbDataAdapter da = new OleDbDataAdapter(cmd);
//////da.Fill(ds, "default");
////da.Fill(ds);
////dataGridView1.DataSource = ds;
////con.Close();
ReadData(
"SELECT * FROM tbl_ClientDetails"
,
ref
dstResults,
"BankName"
);
myView = ((DataTable)dstResults.Tables[
"BankName"
]).DefaultView;
dataGridView1.DataSource = myView;
}
public
void
ReadData(
string
strSQL,
ref
DataSet dstMain,
string
strTableName =
"default"
)
{
try
{
string
connectionString = @
" Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClientMgmtDb.accdb;"
;
// OleDbConnection cnn = conection();
OleDbConnection cnn =
new
OleDbConnection(connectionString);
OleDbCommand cmd =
new
OleDbCommand(strSQL, cnn);
cnn.Open();
OleDbDataAdapter da =
new
OleDbDataAdapter(cmd);
da.Fill(dstMain, strTableName);
da.Dispose();
cnn.Close();
}
catch
(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private
void
txtSearch_TextChanged(
object
sender, EventArgs e)
{
txtSearch.BackColor = System.Drawing.Color.Yellow;
}
private
void
dt_PaymentRecvdDate_KeyUp(
object
sender, KeyEventArgs e)
{
string
outputInfo =
""
;
string
[] keyWords = dt_PaymentRecvdDate.Text.Split(
' '
);
foreach
(
string
word
in
keyWords)
{
if
(outputInfo.Length == 0)
{
outputInfo =
"(PayementRecievedDate LIKE '%"
+ word +
"%' OR ContactPersonName LIKE '%"
+
word +
"%' OR InstalledDate LIKE '%"
+ word +
"%')"
;
}
else
{
outputInfo +=
" AND (PayementRecievedDate LIKE '%"
+ word +
"%' OR ContactPersonName LIKE '%"
+
word +
"%' OR InstalledDate LIKE '%"
+ word +
"%')"
;
}
}
myView.RowFilter = outputInfo;
}
}
}
C#
Auto Search Data
Database