Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

please help me with a sample code I can use to generate automated queries for each of the records in my table. I would like to have the command as a button click event so that when I choose a record ID, say from a comboBox or record selector, a query is automatically created comprising of all the details of the queries record.

Thanks
Posted
Updated 14-May-10 22:20pm
v2

Hi as per your query i have made a code for you in C# which will use the access database,
Now what happens in this code is that i am loading the employees id in a combobox on a form load and then the user has to select a particular id from the combobox and click a button. On button click user will get the name of an employee in a textbox

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb; //make sure u add this namespace

namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database1.mdb;Persist Security Info=False");
            con.Open();
            string query = "Select name1 from mytable where id='"+comboBox1.Text+"'";
            OleDbCommand cmd = new OleDbCommand(query,con);
            OleDbDataReader dr = cmd.ExecuteReader();
            if(dr.Read()==true)
            {
                textBox1.Text = dr[0].ToString();
            }
            else
            {
                MessageBox.Show("No such record");
            }
            con.Close();
            con.Dispose();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database1.mdb;Persist Security Info=False");
            con.Open();
            string query = "select id from mytable";
            OleDbCommand cmd = new OleDbCommand(query, con);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            con.Close();
            con.Dispose();

        }
    }
}

Do rate my answer once you find it useful
Thanks & Regards
Radix :)
 
Share this answer
 
On using access database with C#, see the link: Google Search: access database with C#[^].

katumbamartin wrote:
would like to have the command as a button click event so that when i choose an a record id, say from a comboBox or record selector, a query is automatically created comprising of all the details of the queries record.


You need to pass the record id as the parameter to the database. So our query will be like:
SQL
SELECT * FROM Table_Name WHERE [Id] = @RecordID

where id is the primary key field name and @RecordID will be the parameter which you will pass from your program.
* this is a simple sql query

These are very basic things and so I would suggest you to read some books or articles/tutorials on the web before you actually start coding.
Hope you take that in a good spirit. :thumbsup:
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900