Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to link table in data entry form (window form).Data entry form create on window form in visual studeo 2008 and table create in ms access 2007
Posted

1 solution

You want to retrieve data from a database and output it unto a windows form control or what?..If thats what you want to do then you'd have to do the following..

1. first create your table in an Access database (either 2003/2007).

2. Now you can use the following code to get the data you need
//Add the following namespaces to the calling class
using System.Data;
using System.Data.OLEdb;
using System.Windows.Forms;
..

class DB
{

public void getData([form controls here as parameters eg.] Textbox tbxFname, Textbox tbxLname)
{
   OLEdbConnection conn=new OLEdbConnection();
   conn.ConnectionString="Provider=Microsoft.JET.4.0; Data Source=[database name here].mdb(if access 2003)/.accdb(if access 2007)";

   
   OLEdbCommand cmd=new OLEdbCommand();
   cmd.Connection=conn;
   cmd.CommandType=CommandType.Text;
   cmd.CommandText="select * from [table name here] where [specify condition here]";

   OLEdbDataReader rd;
   rd=cmd.ExecuteReader();

   while(rd.Read())
   {
     tbxFname.Text=(rd[0].Value).toString();
     tbxLname.Text=(rd[1].Value).toString();
     .....
     // other controls will be linked to various reader values and also the numbers o,1 etc signify the field position in the database


   }
conn.Close();
cmd.Dispose();


}
}


To call the code on the form,
1. First create an object to the class in the form_load event, Example :

DB _object = new DB();
_object.getData([input argument list here]);

// but make sure arguments reference the form controls


And when you build the Project I think it should work after you make the right changes!
 
Share this answer
 
Comments
sariqkhan 15-Dec-12 10:43am    
+5

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