Control Panel
Forum
Hosting Help
ASP.NET 4 & SQL 2008 R2 Hosting
Resources
Notice
Login
Members
Shared Hosting
|
Upgrade
|
Advertise
|
Tutorials
|
AdSense revenue sharing sites
|
Exam 70-680 Practice Tests
|
Silverlight games
Total
members
:
210368
Average new registrations per day (in last 7 days):
16
New Registration:
Open
Register Now
Home
»
Resources
»
How to add data in the MS Access database for ASP.NET application
ASP.NET
ADO.NET
Web Services
Remoting
Visual Studio 2005
Error Bank
Interview Questions
Tips & Tricks
XML
HTML
Jscript/Javascript
IIS
Windows
General
Submit Resource or code snippet
... and get
surprise gifts
Win Digital camera, ASP.NET Books, Free softwares!!
How to add data in the MS Access database for ASP.NET application
20 Jun, 2010
Author:
Alhadith hazro
Summary
How to add data in the MS Access database for ASP.NET application
.NET Classes used :
OdbcConnection
OdbcCommand
How to add data in the MS Access database for ASP.NET application
Following demo provides you the quickest way to learn how to save data in the mdb file while using asp.net. It requires only 2-or-3 lines of code, extra code is optional, which depends on you what you want to save. One varialbe of ConnectionString is and one for QueryString is required. And then the part of code under using statement for OdbcConnection class used for doing connection with access database.
Here is how I used above technique:
namespace DashingData
{
public class DashingDataUser
{
private void OnAddingData()
{
using (var db = new DashingContext()) // DashingContext is a LINQ-to-SQL Entity Class
{
int customerID; int.TryParse(txtCustomerID.Text, out customerID);
var customer = from c in db.CustomerTable
select c;
if (Enumerable.Any(customer, item => item.CustomerID.Equals(customerID.ToString())))
{
lblError.Text = Customer ID already exists, please try new one.;
return;
}
// Note: Connection string is Importatnt
string connectionString = string.Format(Driver={{Microsoft Access Driver (*.mdb)}};DBQ={0}, ~/App_Data/YourDataFile.mdb);
string fatherName = txtFatherName.Text;
string phoneNumber = txtPhoneNumber.Text;
string customerAddress = txtAddress.Text;
string customerName = txtCustomerName.Text;
// Note: QueryString is also important
string querystring =
string.Format(INSERT INTO CustomerTable(CustomerID, CustomerName, FatherName, PhoneNumber, CustomerAddress) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}'), customerID, customerName, fatherName, phoneNumber, customerAddress);
try
{
// Note: Original Code starts here
using (var connection = new OdbcConnection(connectionString))
{
var command = new OdbcCommand(querystring) { Connection = connection };
connection.Open();
command.ExecuteNonQuery();
}
lblSaved.Text = Record is saved successfully.;
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
}
}
}
I've created a DashingContext entity class. That serve as LINQ-to-SQL entity class. LINQ-to-SQL also works fine for MS Access database searching purpose, but CRUD Operations can't be done with LINQ-to-SQL for mdb file. You must explicitly call traditional OdbcConnection class methos for CRUD operations. Some types of MS Access don't match with CLR types, i.e. True/False type don't match with CLR Boolean type. So that, in that case, you must use LINQ-to-Dataset for rich-searching.
My mdb file contains a table, named: CustomerTable. I used it above.
Feedbacks about this page from members:
- No Feedbacks yet !! -
Submit Feedback
View All Feedbacks
Advertise
Privacy Policy
Terms of use
Contact Us
SpiderWorks Technologies Pvt Ltd.
All Rights Reserved.