Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a form using c#. I wish to have a direct connection with an access db that I have created i.e. If I enter assetID , the app should retrieve all data associated with rows corresponding to that ID and automatically update in the db the date when the button is clicked . Would someone be able to provide me a small code for the same?
Posted

Hi,

Below Code Will Show Asset Name in Label which ID same as textbox Value


C#
private void btnSubmit_Click(object sender, EventArgs e)
        {
            string filePath =@"D:\DB\Database1.mdb";
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filePath); 
            OleDbCommand cmd = new OleDbCommand("select * from TblAsset where AssetID="+txtAssetID.Text);
            con.Open();
           
            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                lblAssetName.Text = dr["AssetName"].ToString();
            }
            dr.Close();
            dr.Dispose();
            con.Close();

        }



Thanks
Siva Rm K
 
Share this answer
 
CP helps people to learn. We neither do their assignments/homeworks/projects nor supply source code.
what-have-you-tried[^]
If you encounter any difficulties with your coding, always consult Google first, everything else fails, then visit CP and ask questions related to specific issues.
Nevertheless, since you are here, you may want to learn and adapt from the following links:
step-by-step-connect-to-access-database-in-c-sharp[^]
Simple Movie Database in C# using Microsoft Access[^]
 
Share this answer
 
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {OleDbConnection mycon = new OleDbConnection();
	       mycon.ConnectionString =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dinesh\C#\GIS_Power\WindowsFormsApplication1\bin\Power_DB1.accdb";
           OleDbCommand command = new OleDbCommand();
           command.CommandText = "INSERT INTO Table1 (Emp_ID,Asset_ID)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
           mycon.Open();
           command.Connection = mycon;
           command.ExecuteNonQuery();
           mycon.Close();
            
            
        }
    }
}
 
Share this answer
 
Not here we won't, no.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 

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