Subscribe to Subscribers

Forums » .NET » .NET »

Retrrieve data fro MS Access


Posted Date: 06 Jul 2012      Posted By:: ptina     Member Level: Bronze    Member Rank: 1585     Points: 5   Responses: 3



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.SqlClient;
using System.Data.Odbc;

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

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
System.Data.Odbc.OdbcConnection conn =
new System.Data.Odbc.OdbcConnection();
// TODO: Modify the connection string and include any
// additional required properties for your database.
conn.ConnectionString = "FIL=MS Access;DSN=Particulars";
try
{
conn.Open();
//display.Text="Successful !!!";

// Process data here.

OdbcCommand command = conn.CreateCommand();

command.CommandText = "SELECT * FROM Student WHERE name=''";

OdbcDataReader mySqlDataReader = command.ExecuteReader();
while (mySqlDataReader.Read())
{
display.Text = display.Text + "mySqlDataReader[\" AdmNo\"] = " + mySqlDataReader["AdmNo"];
display.Text = display.Text + "\n" + "mySqlDataReader[\" Name\"] = " + mySqlDataReader["Name"];
}

conn.Close();
}
catch (Exception ex)
{
//MessageBox.Show("Failed to connect to data source");
display.Text = "Fail !!!";
}
finally
{
conn.Close();
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{

}
}
}
i have a datatable in MS Access with field name "name" & "Admin No" (particulars of 4 persons)
how to retrieve the the exact Admin no ; if type out a particulat persons' name.
what control needed in the form (label, txtboxes, buttons)?
what is the codings needed to retrieve the exact data for the above-mentioned prgm?




Responses

#679288    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 07/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

use gridview to display details and textbox, button get details from user refer below code sample


using System.Data;
using System.Data.OleDb;

public partial class GridHypher : System.Web.UI.Page
{
OleDbConnection con = new OleDbConnection(@"access connection string");
OleDbCommand cmd;
OleDbDataAdapter da;
DataTable dt = new DataTable();


protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new OleDbCommand("select * from emp where name='" + TextBox1.Text + "'", con); //here pass that name
da = new OleDbDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}


Regards
N.Ravindran
Your Hard work never fails


 
#679310    Author: SonyMadhu      Member Level: Gold      Member Rank: 45     Date: 08/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi ptina,


Refer below..

Dim strcon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\sample.accdb;;Persist Security Info=False"
Dim con As New OleDbConnection(strcon)
Dim cmd As New OleDbCommand()
Dim da As New OleDbDataAdapter()
Dim dt As New DataTable()
Try
con.Open()
cmd = New OleDbCommand("select * from Table1", con)
da = New OleDbDataAdapter(cmd)
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Catch ex As Exception

End Try




Hope this will help you

Regards,
Madhu
Be so hapy wen others look at you,they become hapy too


 
#679357    Author: Paritosh Mohapatra      Member Level: Gold      Member Rank: 6     Date: 08/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

You may use the following code to get the data from the table and display it in a DataGridView:


private void button1_Click(object sender, EventArgs e)
{
DataTable dt = GetData("SELECT * FROM user", "user");
dataGridView1.DataSource = dt;
dataGridView1.DataMember = "user";
}

public OleDbConnection GetAccessCon(string strFilePath)
{
OleDbConnection accesscon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" +
"Extended Properties=Excel 8.0;");
return accesscon;
}

public DataTable GetData(string query, string tablename)
{
OleDbConnection con = GetAccessCon(@"C:\temp\List.mdb");
con.Open();
OleDbDataAdapter OleDa = new OleDbDataAdapter(query, con);
System.Data.DataTable dt = new System.Data.DataTable();
OleDa.Fill(dt);
dt.TableName = tablename;
con.Close();
return dt;
}



Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM





 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Difference between Webform and mvc in .Net?
Previous : Microsoft certification (70-516)
Return to Discussion Forum
Post New Message
Category:

Related Messages

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.