473,749 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting into Access Database using C#....?

Hi,

I am trying to learn ADO.net and I keep running into
problems trying to insert data into my Access Database:
data.mdb.

here is my code:

<code>
// Database Variables
private string connectionStr = @"Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry
Path=;Jet OLEDB:Database Locking Mode=1;Data
Source=""C:\dat a.mdb"";Jet OLEDB:Engine
Type=5;Provider =""Microsoft.Je t.OLEDB.4.0"";J et
OLEDB:System database=;Jet OLEDB:SFP=False ;persist
security info=False;Exte nded Properties=;Mod e=Share Deny
None;Jet OLEDB:Encrypt Database=False; Jet OLEDB:Create
System Database=False; Jet OLEDB:Don't Copy Locale on
Compact=False;J et OLEDB:Compact Without Replica
Repair=False;Us er ID=Admin;Jet OLEDB:Global Bulk
Transactions=1" ;
private string selectStr = "Select id,
date, day, time, comments, timeout, status, process from
data";

private System.Data.Dat aSet myDataSet;
private System.Data.Ole Db.OleDbConnect ion
myConnection;
private
System.Data.Ole Db.OleDbDataAda pter myDataAdapter;
private
System.Data.Ole Db.OleDbCommand Builder myCmdBuilder;

private System.Data.Ole Db.OleDbCommand
mySelectCmd;

public Schedule()
{
this.InitilizeD b();
this.PopulateDa taSet();
}

private OleDbConnection ConnectDb()
{
return new OleDbConnection
(connectionStr) ;
}

public void InitilizeDb()
{
myDataAdapter = new
OleDbDataAdapte r();
myCmdBuilder = new
OleDbCommandBui lder(myDataAdap ter);
myDataSet = new DataSet();
}

private void PopulateDataSet ()
{
// get the connection object
myConnection = (OleDbConnectio n)
this.ConnectDb( );

// Initilize the Select Command
mySelectCmd = new OleDbCommand
(selectStr, myConnection);

// Define that the Select Command
is an SQL statement
mySelectCmd.Com mandType =
CommandType.Tex t;

try
{
myConnection.Op en();
myDataAdapter.S electCommand = mySelectCmd;

// Populate the DataSet
from the "data" table
myDataAdapter.F ill
(myDataSet,"dat a");
}
finally
{
myConnection.Cl ose();
}
}

public string InsertSchedule( string date,
string day, string time, string process,
string comments, string timeout,
string status)
{
// get the connection object
myConnection = (OleDbConnectio n)
this.ConnectDb( );

try
{
myConnection.Op en();

// create a new row to
insert the data into
DataRow newRow =
myDataSet.Table s["data"].NewRow();

// initilize the new row
newRow["id"] = 3;
newRow["date"] = date;
newRow["day"] = day;
newRow["time"] = time;
newRow["process"] =
process;
newRow["comments"] =
comments;
newRow["timeout"] =
timeout;
newRow["status"] = status;

// add the new row into
the dataset
myDataSet.Table s
["data"].Rows.Add(newRo w);

myDataAdapter.U pdate
(myDataSet, "data");

return "true";
}
catch(Exception e)
{
return e.Message + "\n\n"
+ e.StackTrace + "\n\n" + e.InnerExceptio n;
}
finally
{
myConnection.Cl ose();
}
}
</code>

the problem is when I try to insert into the database, I
keep getting this Exception Message:


<code>Syntax error in INSERT INTO statement.

at System.Data.Com mon.DbDataAdapt er.Update(DataR ow[]
dataRows, DataTableMappin g tableMapping)
at System.Data.Com mon.DbDataAdapt er.Update(DataS et
dataSet, String srcTable)
at ShutdownMgr.Sch edule.InsertSch edule(String date,
String day, String time, String process, String comments,
String timeout, String status) in c:\documents and
settings\azel\m y documents\visua l studio
projects\shutdo wnmgr\schedule. cs:line 99
</code>


I have tried different ways but none were successful. Is
anyone able to spot where I am going wrong? X|

thanks.
Nov 15 '05 #1
1 4142
Yeah, I never actually see a Insert statement either.

"Azel" <me@azellow.com > wrote in message
news:05******** *************** *****@phx.gbl.. .
Hi,

I am trying to learn ADO.net and I keep running into
problems trying to insert data into my Access Database:
data.mdb.

here is my code:

<code>
// Database Variables
private string connectionStr = @"Jet
OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry
Path=;Jet OLEDB:Database Locking Mode=1;Data
Source=""C:\dat a.mdb"";Jet OLEDB:Engine
Type=5;Provider =""Microsoft.Je t.OLEDB.4.0"";J et
OLEDB:System database=;Jet OLEDB:SFP=False ;persist
security info=False;Exte nded Properties=;Mod e=Share Deny
None;Jet OLEDB:Encrypt Database=False; Jet OLEDB:Create
System Database=False; Jet OLEDB:Don't Copy Locale on
Compact=False;J et OLEDB:Compact Without Replica
Repair=False;Us er ID=Admin;Jet OLEDB:Global Bulk
Transactions=1" ;
private string selectStr = "Select id,
date, day, time, comments, timeout, status, process from
data";

private System.Data.Dat aSet myDataSet;
private System.Data.Ole Db.OleDbConnect ion
myConnection;
private
System.Data.Ole Db.OleDbDataAda pter myDataAdapter;
private
System.Data.Ole Db.OleDbCommand Builder myCmdBuilder;

private System.Data.Ole Db.OleDbCommand
mySelectCmd;

public Schedule()
{
this.InitilizeD b();
this.PopulateDa taSet();
}

private OleDbConnection ConnectDb()
{
return new OleDbConnection
(connectionStr) ;
}

public void InitilizeDb()
{
myDataAdapter = new
OleDbDataAdapte r();
myCmdBuilder = new
OleDbCommandBui lder(myDataAdap ter);
myDataSet = new DataSet();
}

private void PopulateDataSet ()
{
// get the connection object
myConnection = (OleDbConnectio n)
this.ConnectDb( );

// Initilize the Select Command
mySelectCmd = new OleDbCommand
(selectStr, myConnection);

// Define that the Select Command
is an SQL statement
mySelectCmd.Com mandType =
CommandType.Tex t;

try
{
myConnection.Op en();
myDataAdapter.S electCommand = mySelectCmd;

// Populate the DataSet
from the "data" table
myDataAdapter.F ill
(myDataSet,"dat a");
}
finally
{
myConnection.Cl ose();
}
}

public string InsertSchedule( string date,
string day, string time, string process,
string comments, string timeout,
string status)
{
// get the connection object
myConnection = (OleDbConnectio n)
this.ConnectDb( );

try
{
myConnection.Op en();

// create a new row to
insert the data into
DataRow newRow =
myDataSet.Table s["data"].NewRow();

// initilize the new row
newRow["id"] = 3;
newRow["date"] = date;
newRow["day"] = day;
newRow["time"] = time;
newRow["process"] =
process;
newRow["comments"] =
comments;
newRow["timeout"] =
timeout;
newRow["status"] = status;

// add the new row into
the dataset
myDataSet.Table s
["data"].Rows.Add(newRo w);

myDataAdapter.U pdate
(myDataSet, "data");

return "true";
}
catch(Exception e)
{
return e.Message + "\n\n"
+ e.StackTrace + "\n\n" + e.InnerExceptio n;
}
finally
{
myConnection.Cl ose();
}
}
</code>

the problem is when I try to insert into the database, I
keep getting this Exception Message:


<code>Syntax error in INSERT INTO statement.

at System.Data.Com mon.DbDataAdapt er.Update(DataR ow[]
dataRows, DataTableMappin g tableMapping)
at System.Data.Com mon.DbDataAdapt er.Update(DataS et
dataSet, String srcTable)
at ShutdownMgr.Sch edule.InsertSch edule(String date,
String day, String time, String process, String comments,
String timeout, String status) in c:\documents and
settings\azel\m y documents\visua l studio
projects\shutdo wnmgr\schedule. cs:line 99
</code>


I have tried different ways but none were successful. Is
anyone able to spot where I am going wrong? X|

thanks.
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3435
by: Jared Evans | last post by:
I developed a console application that will continually check a message queue to watch for any incoming data that needs to be inserted into MS SQL database. What would be a low-cost method I could use inside this console application to make sure the MS SQL database is operational before I perform the insert?
3
16520
by: James Alba | last post by:
Hey all, I am accessing an ms access database using .NET and C#. Like so, /* Create the database connection. */ connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Name); connection.Open(); And all is good.
11
4379
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store procedure(assume there are many columns in the table). I need to insert data into two separate tables, the relation between these two tables is 1 row of data in table1 could have multiple rows in table2 related to table1, but if the data insertion into...
15
4111
by: yzlin04 | last post by:
Hello, I'm new in vb.net. I have a problem in inserting a new row to a MS Access database table. There is no error message comes out when i run my program, but when i check my MS Access table, there is no record inserted. So, i don't know which line is incorrect. Here is my code: Dim desc, itemID As String itemID = txtItemID.Text desc = txtItemDesc.Text
3
3367
by: Surya | last post by:
Dear All, I have problem on inserting a record to database..Although it looked easy.. i have caught up with following issue .. please go ahead and help me to find solution I Need to insert records to a data base,for which i am using Typed dataSet.(I used DataSet Designer to create one) Then created a table adapter in which i have written query for INSERT , & set Database Direct property to true.(I didnt use a seperate methods for...
0
1655
by: SL Culby | last post by:
Hello everyone, I have a project where I pull SQL Server data put it into a dataset and now I have to put the dataset data into an Access Database. The dataset currently is over 2000 row, so looping through and inserting one row at a time is very expensive. Does anyone know a fast way with low overhead to accomplish this task. I have been working in this direction (code below) however the code inserts 0 rows into the access database. ...
18
4048
by: hotflash | last post by:
Hi Mark et All, I understand that you created a very professional document and a similar issue forum out there regarding to Inserting the checkbox value into MS Access using ASP however; I am so new to this stuff and have tried couple times but no luck. Sorry to waste your time again, but would appreciate if you can help me out. I have a form that has a checkbox name of ExpeditedRequest. I can't send the unchecked or checked value to the...
6
11762
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the contents of the form is inserted into the MS Access database. The Customer table in the database already has 30 records (with CustomerIDs 1 - 30) in it (from when the database was first created). The CustomerID field in the database is an...
9
1560
by: Bunty | last post by:
I am not beginer of this language. I've worked on this language for 2 or 3 months.But now i have a problem to inserting values in the database.It gives me error that DATABASE NOT SELECTED. I am using wampserver.Please help me for this prob.
2
3119
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate record. My code inserts records one by one using addnew-updatebatch. If there is a duplicate in the db, it will display "already exists" and if it is not in the db it will display "record added". Below is my asp code I found on the net and...
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.