Programming for Everybody: Programming with c#: how to search data from access database and display it in textboxes with code

Programming with c#: how to search data from access database and display it in textboxes with code


 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 retrieve_data_c_message_box

{

    public partial class Form5 : Form

    {

        OleDbConnection conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\ddd.accdb");

        public Form5()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            conn1.Open();

            OleDbCommand cmd1 = new OleDbCommand("Select id,firstname,lastname,sum1 from table2 where id=@parm1", conn1);

            cmd1.Parameters.AddWithValue("@parm1", search1.Text);

            OleDbDataReader reader1;

            reader1 = cmd1.ExecuteReader();

         if(   reader1.Read())

            {

                textBox1.Text = reader1["id"].ToString();

                textBox2.Text = reader1["firstname"].ToString();

                textBox3.Text = reader1["lastname"].ToString();

                textBox4.Text = reader1["sum1"].ToString();

            }

         else

            {

                MessageBox.Show("No Data found");

            }


            conn1.Close();

        }


        private void button2_Click(object sender, EventArgs e)

        {

            conn1.Open();

            OleDbCommand cmd1 = new OleDbCommand("Select id,firstname,lastname,sum1 from table2 where firstname=@parm1", conn1);

            cmd1.Parameters.AddWithValue("@parm1", search1.Text);

            OleDbDataReader reader1;

            reader1 = cmd1.ExecuteReader();

            if (reader1.Read())

            {

                textBox1.Text = reader1["id"].ToString();

                textBox2.Text = reader1["firstname"].ToString();

                textBox3.Text = reader1["lastname"].ToString();

                textBox4.Text = reader1["sum1"].ToString();


            }

            else

            {

                MessageBox.Show("No data found");

            }

            conn1.Close();

        }

    }

}


No comments:

Post a Comment