i'm using C# and MS Access database..i want to search name or serial no using LIKE statement..but i don't know how to do it..and then after i search it will view the match that i search for..let's say i want to search a letter start with 'C', an then it will view all name or serial no that start with 'C'..i want to show it in gridview but i don't know how to do that:(..to search that i try to use textbox and a button search..after i click on button search it will view the match for the word that i enter..like in daniweb when we search a certain word it'll view by grid..i try to do like that but i don't know how to start..is there anyone that can suggest me any solving method..i'll appreciate that..:)

Recommended Answers

All 13 Replies

for that first create aconnection with the database. Then create one Adapter and fire a querry in your adpter and fill the dataset with adapter and give your gridview datasource that dataset.
e.g.

OleDbConnection con=new OleDbConnection ("Connection String");
Con.OPen();
string str="Select [name] from [tabel] Where [name] like '%" + textbox1.Text + "%'";
OleDbDataAdapter da=new OleDbDataAdapter (str,con);
Dataset ds=new Dataset();
da.Fill(ds,"table");
mygrid.DataSource=ds;
''mygrid.DataMember="table" ''sorry i'm not sure about this property
''but other then all the code is right
con.close();

try it.

You should add ds.Clear() to overwrite the records every time you click search.

commented: Good use of gained knowledge :) +1

can u completed the code above?i don't know where to put that..

yes @angelicOne you are righ.Thanks
put this line whrn you create a instace of Dataset.i.e.
Dataset ds=new dataset();
ds.Clear();
then write all the code as i wriiten there and remove the line no. 5

So,under the search button i should put this code?
did i need to put gridview?
can u tell me further pritesh2010?

yes first put the gridview on your form. then on button click event write the code.

''mygrid.DataMember="table" ''sorry i'm not sure about this property
''but other then all the code is right

how about this code?

Line 26:         string str="SELECT * FROM tabelRegister WHERE StaffName LIKE '%" + TextBox1.Text + "%'";
Line 27:         OleDbDataAdapter da=new OleDbDataAdapter (str,con);
Line 28:         Dataset ds = new Dataset();
Line 29:         ds.Clear();
Line 30:         da.Fill(ds,"tableRegister");

i got error there..it state..
CS0246: The type or namespace name 'Dataset' could not be found (are you missing a using directive or an assembly reference?)

at th general decalration define the
OleDbConnection con=new OleDbConnection();
OleDbDataAdapter da=new OleDbDataAdapter ();
DataSetc ds=new Dataset();
i think you did'nt add the namspace of Syste.Data Or System.Data.OleDb

OleDbConnection con = new OleDbConnection();
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\priteshv\\My Documents\\db1.mdb;Persist Security Info=False";
        con.Open();
        string str = "Select * from [Name] Where [name] like '%" + TextBox1.Text + "%'";
        ////AccessDataSource1.SelectCommand = str;
        OleDbDataAdapter da = new OleDbDataAdapter(str, con);
        DataSet ds = new DataSet();
        ds.Clear();
        da.Fill(ds, "Name");
        GridView1.DataSource = ds;
        GridView1.DataMember = "Name";
        GridView1.DataBind();
        con.Close();

see this i had created and which is work very good.

DataSet

C# (as with most languages) is case sensitive :twisted:

Hope that helps :)

thanks pristesh2010 and lusiphur..i'll try implement your code..if any error i'll post again..:)

protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("App_Data\\dnis2010.mdb");
        string str = "SELECT * FROM tableRegister WHERE StaffName LIKE '%" + TextBox1.Text + "%'";

        OleDbConnection con = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
        con.Open();
        
        OleDbDataAdapter da=new OleDbDataAdapter (str,con);
        DataSet ds = new DataSet();
        ds.Clear();
        da.Fill(ds,"tableRegister");
        GridView1.DataSource = ds;
        GridView1.DataMember = "tableRegister";
        GridView1.DataBind();

        con.Close();
    }

this is your code that i try..but nothing being view..no error..

protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("App_Data\\dnis2010.mdb");
        OleDbConnection conn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
        conn.Open();
        string str = "SELECT * FROM tableRegister WHERE StaffName LIKE '%" + TextBox1.Text + "%'";
        OleDbDataAdapter da=new OleDbDataAdapter (str,con);
        DataSet ds = new DataSet();
        ds.Clear();
        da.Fill(ds,"tableRegister");
        GridView1.DataSource = ds;
        GridView1.DataMember = "tableRegister";
        GridView1.DataBind();

        conn.Close();
    }

now after altering some code it working well..:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.