944,061 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5402
  • C# RSS
Sep 13th, 2005
0

Get data from Access instead of Notepad (URGENT)

Expand Post »
Hi

I want to get data from Access instead of notepad.
Here are the codes to get data from notepad by entering IC number (equivalent to ID number). The records are stored in 'Folder'.


public void btnGo_Click_1(object sender, System.EventArgs e)// -->User can select the file by entering the IC Nunber in textbox and program will check for file existence and null file
{
enable();
/** [Retrieve IC Number from tboxEnterICNo] **/
strFileName2 = tboxEnterICNo.Text;
path = @"Folder\"+strFileName2+".txt";
/** [Check for file existence] **/
if(File.Exists("Folder\\"+strFileName2+".txt"))// -->If file exists, open DisplayGraph form and display the graph
{
displaygraph.names = path;// -->copies the path to DisplayGraph form
displaygraph.ShowDialog();// -->Opens DisplayGraph form to display graph
}//End of if-condition
else
{
MessageBox.Show("File does not exist! Please create a new profile.", "Invalid File Type", MessageBoxButtons.OK, MessageBoxIcon.Error );
}// -->End of else-condition
}// -->End of btnGo_Clicks module


How should i go about to keep the records in Access and also specify retrieving of the queried data from an Access table instead?

I need some help about what to do and how the codes look like.
Thank you very much!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
c#dummie is offline Offline
38 posts
since Jul 2005
Feb 20th, 2009
0

Re: Get data from Access instead of Notepad (URGENT)

a little sample to help you out,
but you'd better read some ADO.Net tutorials

c# Syntax (Toggle Plain Text)
  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Text;
  8.  
  9. using System.IO;
  10.  
  11. //
  12.  
  13. // namespaces needed fo data access
  14.  
  15. //
  16.  
  17. using System.Data;
  18.  
  19. using System.Data.OleDb;
  20.  
  21.  
  22.  
  23. namespace msaccess
  24.  
  25. {
  26.  
  27. class Program
  28.  
  29. {
  30.  
  31. static void Main(string[] args)
  32.  
  33. {
  34.  
  35. //
  36.  
  37. // Initialization
  38.  
  39. //
  40.  
  41.  
  42.  
  43. string ic_number = "648937693";
  44.  
  45.  
  46.  
  47. string path = Directory.GetCurrentDirectory();
  48.  
  49. path += "\\Folder\\MyDatabase.mdb";
  50.  
  51.  
  52.  
  53. // Connection object
  54.  
  55. var cn = new OleDbConnection(
  56.  
  57. "Provider=Microsoft.Jet.OLEDB.4.0;" +
  58.  
  59. "Data Source=" + path);
  60.  
  61.  
  62.  
  63. // sql command object, that will select your data
  64.  
  65. var cmd = new OleDbCommand(
  66.  
  67. "select MyField1, MyField2 from MyTable where ICNumber = @icnumber", cn);
  68.  
  69.  
  70.  
  71. // this will insert "648937693" in @icnumber in SQL select statement
  72.  
  73. cmd.Parameters.AddWithValue("@icnumber", ic_number);
  74.  
  75.  
  76.  
  77. // table adapter
  78.  
  79. // it can execute sql commands
  80. // and fill table objects with data from database
  81.  
  82. var adapter = new OleDbDataAdapter(cmd);
  83.  
  84.  
  85.  
  86. var table = new DataTable();
  87.  
  88.  
  89.  
  90. // here adapter opens connection
  91.  
  92. // retrieves data with select command
  93.  
  94. // and stores it in table object
  95.  
  96. adapter.Fill(table);
  97.  
  98.  
  99.  
  100. // iterate through rows of data
  101.  
  102. foreach (DataRow row in table.Rows)
  103.  
  104. {
  105.  
  106. // do your stuff
  107.  
  108. string s = string.Format(
  109. "MyField1: {0} MyField2: {1}",
  110. row["MyField1"], row["MyField2"]);
  111.  
  112. Console.WriteLine(s);
  113.  
  114. }
  115.  
  116.  
  117.  
  118. // wait
  119.  
  120. Console.ReadLine();
  121.  
  122. }
  123.  
  124. }
  125.  
  126. }

good luck
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kamisori is offline Offline
5 posts
since Feb 2009
Feb 20th, 2009
0

Re: Get data from Access instead of Notepad (URGENT)

oh ****, this post is 4 years ago)))))
i'm an idiot
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kamisori is offline Offline
5 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Dynamic Form Letters
Next Thread in C# Forum Timeline: Connect to access database on a shared folder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC