Search code examples
c#winformsms-access-2007ms-access-2003

DB Access search textbox


StudentID > Number

StudentName > Text

StudentCNIC > Text

StudentDOB > Date/Time

I am trying to create a search textbox where the results are show in of the text boxes. I have a named a button calling it FindBtn where user enters of either StudentID, Student Name or Student CNCI (just pain numbers). The results are then shown in the textboxes...StudIDTb (displaying Student ID), StudNameTb (displaying Student Name), StudCNCITb (displaying Student CNCI) and StudDOBTb (displaying Students DOB).

A lot of examples I have seen on the internet uses gridview but I am using textboxes to show results. The ones I have found of interest are...

here here here and here

    public Form1()
    {
        InitializeComponent();
        //Connection String for Access 2003. 
        myCon = new OleDbConnection(@" Provider=Microsoft.Jet.OLEDB.4.0;Data 
        Source=C....\StudDB.mdb ");
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        this.studentsTableAdapter.Fill(this.studDBDataSet.Students);
    }

In the FindBtn function have done the following....

    private void FindBtn_Click(object sender, EventArgs e)
    {
        string title = textBox1.Text.ToString();
        if (title != "")
        {
            string queryString = "SELECT * FROM Students + title;
            //I need help here
            StudIDTb.Text = queryString;
            StudNameTb.Text = queryString;
            StudCNCITb.Text = queryString;
            StudDOBTb.Text = queryString;
        }
        else
            MessageBox.Show("Please try again\nError: ");
    }

The results shows ....

SELECT *
FROM Students 103

in each of those textboxes and exe freezes so obviously it is wrong. Please can someone help me here, thanks in advance.

UPDATE 1 I have included OleDbCommand, CommandType and opening connection just before string variable which prevents the screen from freezing but it does not solve the problem

UPDATE 2 Found something I like but it is not working Here

Update 3

As C Sharp Corner, kindly provided a piece of code....

 private void btnFind_Click(object sender, EventArgs e)
    {
        string title = textBox1.Text.ToString();
        string queryString = "SELECT * FROM Students" + title;
        OleDbCommand command = new OleDbCommand();
        command.CommandText = queryString;

        command.Connection = myCon;
        myCon.Open();

        OleDbDataReader dr = command.ExecuteReader();
        while (dr.Read())
        {
             StudIDTb.Text += String.Format("Student ID:                
             {0}\n",dr["StudID"].ToString());
            StudNameTb.Text += String.Format("StudentName ID: {0}\n", 
            dr["StudentName"].ToString());
            StudCNCITb.Text += String.Format("StudentCNIC: {0}\n", 
            dr["StudentCNIC"].ToString());
            StudDOBTb.Text += String.Format("StudentCNIC: {0}\n", 
            dr["StudentCNIC"].ToString());
        }
        myCon.Close();

I am receiving a error after amending formatting....

"The Microsoft Jet database engine cannot find the input table or query 'Students101'. Make sure it exists and that its name is spelled correctly".

I am looking at it now to find out what it means


Solution

  • Here's an example that should work (also if you included oledbcommand, commandtype etc update your code too so we can see what you changed):

    OleDbCommand command = new OleDbCommand();
    command.CommandText = queryString;
    command.Connection = myCon;
    myCon.Open();
    OleDbDataReader dr = command.ExecuteReader();
    while(dr.Read())
    {
       StudIDTb.Text += dr["StudID"].ToString();
       StudNameTb.Text += dr["StudName"].ToString();
       StudCNCITb.Text += dr["StudCNCI"].ToString();
       StudDOBTb.Text += dr["StudDOB"].ToString();
    }
    myCon.Close();